Click here to Skip to main content
16,012,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello There
I have a C# application which contain two forms. I want to fetch the value of ComboBox from another form and save it into a variable in different form.

For this, I changed Modifiers property of ComboBox to Public.
Create an object of the form
C#
Form1 frmParent = new Form1();

and assign the value of ComboBox into a string vaiable in child form
C#
string s = "";
s= frmParent.cmbTransType.Text;

After I debug the program, the value of string variable is not updated and it show blank.

Need Suggestion
Regards
Posted
Updated 1-Jun-13 21:25pm
v3

1 solution

Do not do it like that!
Firstly, it won't work, and secondly, even if it did, it's a poor practice!

I'm not sure of the relationship between your two forms exactly, but it is covered in one of these three short Tips:
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[^]
 
Share this answer
 
Comments
Mayank Topiwala 2-Jun-13 4:31am    
Thanks for the info OriginalGriff.
I have one more doubt. Suppose I want to check in a Parent form if the datagridview control of the child form has any records. So how to do this ?
OriginalGriff 2-Jun-13 5:02am    
It's exactly the same as the Child to Parent, except without the Event - you just need a property in the Child which the Parent accesses. That way, the Parent needs to know nothing about how the child does it. Call it "UserCount" (or whatever your DGV contains) and let it fetch the number from the DGV (just give it a Getter, but no Setter)
Mayank Topiwala 2-Jun-13 5:09am    
Confuse ! :(
OriginalGriff 2-Jun-13 5:29am    
Which bit? :laugh:
Mayank Topiwala 2-Jun-13 5:43am    
when i initialize the child form then all its controls are reset so i couldn't get the number of records of the DGV in parent form.

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