Click here to Skip to main content
15,887,979 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When i click a button on form 1 that has the same tag as the text in a label in form 2 i want it to output the number 1 in a label in form 2

What I have tried:

<pre>if (BtnGlobal.Text == Form2 frm = new Form2(label1.Text))
                {
                    if (String.IsNullOrEmpty(Form2 frm = new Form2(label1.Text)))
                    {
                        Form2 frm = new Form2(label1.Text) = "1".ToString();
                    }
                    else if (Form2 frm = new Form2(label1.Text).contains(int))
                    {
                        int i = Form2 frm = new Form2(label1.Text);
                        i + 1;
                    }
                }
Posted
Updated 10-Aug-20 23:12pm
v2

You are creating a lot of new forms there. Does that code even compile? What is this meant to do
C#
'1'.ToString();
Did you mean
C#
"1";
Have a read of the series of tips posted by OriginalGriff here - first one Transferring information between two forms, Part 1: Parent to Child[^] with links to the rest in that article.
 
Share this answer
 
When you create a new form:
C#
if (String.IsNullOrEmpty(Form2 frm = new Form2(label1.Text)))

C#
Form2 frm = new Form2(label1.Text) = '1'.ToString();
Or
C#
else if (Form2 frm = new Form2(label1.Text).contains(int))
Or
C#
int i = Form2 frm = new Form2(label1.Text);
You do just that: create a brand spanking new form that has nothing at all to do with any existing version. And because of scope rules it never could as it is no longer available after teh following close curly bracket.
So this code:
C#
if (String.IsNullOrEmpty(Form2 frm = new Form2(label1.Text)))
{
    Form2 frm = new Form2(label1.Text) = '1'.ToString();
}
does nothing useful at all!
You create a new form in your if condition, which will always have the default value youset in teh designer. So if that's an empty string the test will always pass, regardless of what your label contains on any visible form.
That is then "thrown away".
You then create another new form and ... well, it won't compile, so it doesn't matter what it does really - create a character, convert it to a string, assign it to new form (assuming there is a constructor that accepts a string parameter, and that label1 exists in the current class / form, and then try to assign that to a Form2 variable before it all goes out of scope and is thrown away?

You need to stop guessing what you are doing, and start thinking. "Hope and pray" is not a viable coding strategy!

As Chill has said, communicating between forms isn't that difficult, the full list of my tips on the subject is here:

The form that creates an instance of another:
C#
MyForm mf = new MyForm();
mf.Show();
Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]

But tio be brutally honest, on the basis of your code sample you aren't ready for that yet - you have a lot to learn before you get to creating and handling events!
 
Share this answer
 
Comments
Luc Pattyn 11-Aug-20 5:16am    
Brutally honest? you're way too kind, another career path would be advisable.
OriginalGriff 11-Aug-20 5:26am    
Probably involving the Magic Words: "Do you want fries with that?" :sigh:
ThePersonWhoCodes 11-Aug-20 5:33am    
you got a good point there, this is more of a thing i wanted to try out, im not a software programmer so your fine. my friend bet me i couldn't make a POS system.
OriginalGriff 11-Aug-20 5:42am    
The problem is that you are trying to do something that is way outside your experience level - and that means that you have real no idea just how complicated this is, or what you need to know to do it.
And throwing code together at random isn't going to get what you want.
You'll need to know how to:
Communicate with the user
Handle a database
Probably read a barcode, and convert that to a product
Get the price.
Add that to a "current sale list"
When it's finished, totalize it, handle payment, generate an invoice and receipt, remove all products sold from stock, handle reorders as necessary, ...
Oh, and "handle payment" opens it's own can of high-security worms: dealing with real money means dealing with good quality security and also external card service providers!

It's a lot to know, not a project to "just try out"! :laugh:
If you really want to do this, get a book (or better go on a course) and do all the (boring) exercises - they are there to get the ideas just covered firmly into your brain, and missing them out means you don't learn as well, if at all. (People learn by doing, not by looking at results.)
Give it a try!
ThePersonWhoCodes 11-Aug-20 5:55am    
Its a POS system that isnt supposed to be used in practice so it doesn't need to have the barcode scanner and none of the handle payment stuff for a book store, just buttons on a screen that adds items to a "current sale list".
As for everything else
I have a database of items that is expandable easily
A "current sale list"
A totalize system
Login Page
reciept "exists"
Discounts
Subtotal, Total and GST
Working Keypad (Not that the keypad matters)
what do you mean by reorders
all in sub 250 lines of code (come to think of it probably is still a lot)

as for the get a book part, i did SDD course for 3.5 years in high school so a while ago, which basically covered the "boring "excercises", i have done it, uses many if else and button senders but works so far. So this is the only thing holding me back

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900