Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have two window forms. eg. form1 and form2.
I want to set the label value in the form2 with the value from the text box in form1.

How can I set the values.
Posted
Updated 8-Nov-10 21:53pm
v2
Comments
ariesangle 9-Nov-10 4:39am    
Thank you all for help me out.

You need to add a method (or a property) to the form that you want to access. You can then access these method/property from the first form. Don't use public controls - this is not the best solution.
 
Share this answer
 
Comments
Rajesh Anuhya 9-Nov-10 3:56am    
Good Answer
ariesangle 9-Nov-10 3:57am    
thank you all
Hiya,

In form 2 create a property for your label:


E.g.
press: <ctrl><space>
go to: prop
press: <tab> twice
This will create a property:

E.g.
C#
// Property for Label1
public string MyLabel1
{
  get { return label1.Text; }
  set { label1.Text = value; }
}


You can access and set this property from form1 as follows:

C#
private void button1_Click(object sender, EventArgs e)
{
  Form2 Form2 = new Form2();
  Form2.MyLabel1 = textBox1.Text;
  Form2.Show();
}


Hope this helps! ;-)
 
Share this answer
 
v2
Comments
ariesangle 9-Nov-10 4:38am    
Hi,
Thank you for your answer. It works out and very helpful to me.
R. Erasmus 9-Nov-10 4:44am    
Its a pleasure!
Glad I could be of help ;)
Rajesh Anuhya 9-Nov-10 4:45am    
Good Call
Hi..

first u set the 2 forms to PUBLIC.
and same as the LABEL also.

Create the Object for Form1.

Ex:
--
Form2Label.Text = ObjForm1.Form1label.Text








Regards,
Saran
 
Share this answer
 
Comments
JF2015 9-Nov-10 4:44am    
You should avoid making the whole control public. In most cases it is more usefull to access a single property.

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