Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys..
so I have 2 forms in my project appropriately named form1 and form2.

form1 contains a button.
form2 contains a textbox.

My question is how do i get to display text to the textbox in form 2 from form 1?

for example when the button in form1 is clicked , i would it to display a text such as "hi" in the textbox that is in form2.

hope this makes sense and thank you for your time.


Abz
Posted
Comments
[no name] 14-Jul-13 20:01pm    
Well that is going to depend on how you are instantiating form 2. If you are instantiating form 2 on the button click then simply pass the text to form 2 through its constructor.

1 solution

On your Form1 add an event on your button which calls the Form2

C#
private void button1_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2("Hi");
    form2.ShowDialog();

}


On your Form2 add a constructor that will accept a string, that will output in your textbox

C#
public Form2(string text)
{
    InitializeComponent();
    textBox1.Text = text;
}
 
Share this answer
 
Comments
1Future 14-Jul-13 20:23pm    
thank you!
berrymaria 14-Jul-13 20:27pm    
your welcome :)

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