Click here to Skip to main content
15,908,581 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
one windows application using 2 forms like form1 and form2.

first form designed two textbox only. another one form designed one button.

now the second form how to access first form controls of textbox.


that is, button_click()
C#
{
string a=textbox1.text;
string b=textbox2.text;
console.writeline(a);
console.writeline(b);
}
Posted
Updated 12-Oct-11 3:23am
v3

First u have to make or create a variable for the first form
ex:
form1 obj=new form1();

now
str=obj.textbox1.text;

like this we can use just try
 
Share this answer
 
v2
Simple answer: Don't.

When you do that, you tie the design of the two forms together. You cannot make a change to the way one form works, without considering the effects on the other. This is against the principles of OOP, and has a detrimental effect on reliability.

Instead, set up public properties and / or events to transfer the data instead. That way the internal working of the form do not get exposed to the outside world.

The best way to do it is to create an event in the appropriate form which indicates that new data is available, and subscribe to that event in the other form. That way, if the second form is interested in teh data, it can use it, or not handle the event if it is not going to use the data.
 
Share this answer
 
Make control public.
and use in Form2.cs as
C#
Form1 f1=new Form1();
string s1=f1.TextBox1.Text;

or
use properties if you do'nt want to make control public.
 
Share this answer
 
v3
Comments
murugeshchinnu 12-Oct-11 9:38am    
i am sorry, textbox1 not available here.
uspatel 13-Oct-11 0:46am    
textbox1 should have public access specifier.check it.
murugeshchinnu 13-Oct-11 1:02am    
thanks patel your answer useful for my developing application.
thank you so much.
you can make control as PUBLIC to access it anywhere

or
otherForm.Controls["nameOfControl"]
 
Share this answer
 
v2

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