Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The scenario is: im clicking on a label (of form1),a form(form2)is opening with a textbox,label and a button. now whatever user types in texttbox of form2 should appear on label of form1.this should be done on button click..

its urgent...
Posted
Updated 26-May-10 19:11pm
v2

Create an event, e.g. UpdateText in form2 that is opened from form1.

When the button is clicked in form2, raise the event

in the eventhandler in form1 (form2_UpdateText), you update the label on form1.
 
Share this answer
 
You really don't need to make the child form aware of the parent form. And it's generally bad practice to allow the child to make modifications to the parent.

Generally, you want the parent to change itself based on feedback from the child. As Johnny suggested, events are one way to do this.

If, however, you are also closing your form on the button click...say the form is only there to change the label, then you don't need to worry about events at all.

On form2 (which I seriously hope you have a better name for in your code), create a property called NewText or something similar. That property should be read-only and should just return the text within your textbox. Then, when the button is clicked, call this.DialogResult = DialogResult.Ok and hide that form.

On form1 (again, I hope you've named it better than that), show form2 using showdialog and if it returns an OK value, then you use the property on form2 to change the label.

Like so:

C#
//Form1 method
private void label1_Click(object sender, EventArgs e)
{
  Form2 newForm = new Form2();
  if (newForm.ShowDialog() == DialogResult.OK)
  {
    label1.Text = newForm.NewText;
  }
}

//Form2 methods
public Form2()
{
  InitializeComponent();

  this.DialogResult = DialogResult.Cancel;
}

public void button1_Click(object sender, EventArgs e)
{
  this.DialogResult = DialogResult.OK;
  this.Hide();
}
public string NewText {
  get { return textBox1.Text; }
}


This doesn't require events and it doesn't require the child form to know about its parent. But, it only works if you're also hiding the form on the button click.
 
Share this answer
 
Expose a property on form1 that changes the label text and everytime a button in form2 is clicked, update this property with the new value.
 
Share this answer
 
Comments
saloni15 27-May-10 3:58am    
i tried this before but its not working can u give me code snippet for it...
In addition to exposing a property, as suggested by Abhinav, you also need to make Form2 aware of Form1 so that it can access the property.

The easiest way to do this is to pass the reference in a new Constructor for Form2.

You can find details on how to do this in many, many articles on MSDN and the web in general.
 
Share this answer
 
Comments
William Winner 27-May-10 12:34pm    
Reason for my vote of 3
as I've been told in the past, telling Form2 of Form1 is unnecessary

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