Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data passing

Status
Not open for further replies.

merlinv

Programmer
Jun 11, 2001
32
US
I would like to pass data back and forth between forms.

An example would be good.

Thanks.

Merlin
 
Merlin,

You can add arguments to the constructor. Look for the function in your code that has the "InitializeComponent()" function call, and that is your constructor. so, if you have a "Form2" and a "Form1", and you want to use a button on "Form1" called "button1". You might have something like this:

Code:
//button on form1
button1_Click(object sender, System.EventArgs e)
{
    Form2 f = new Form2();
    f.Show();
    this.Hide();
}

==============
//Constructor on form 2
Form2()
{
    InitializeComponent();
}

This will create a new instance of form2.

If you wanted to pass a string to Form2 to populate a label with you would change the constructor, or add an overloaded.

So now same form and button names, with the addition of "label1" on form 2

Code:
//button on form1
button1_Click(object sender, System.EventArgs e)
{
    string LabelText = "this will show up in the label";

    Form2 f = new Form2(LabelText);
    f.Show();
    this.Hide();
}

==============
//Constructor on form 2
Form2(string LabelText)
{
    InitializeComponent();
    
    label1.Text = LabelText;
}

Good Luck,
Kevin


- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top