Windows Develop Bookmark and Share   
 index > Windows Forms General > Pass variable between forms
 

Pass variable between forms

I have read a lot of post concerning this and I still can not get this to work.

I'm using VS.Net 2003 (the company is) and need to pass a string between forms.

Ok, I have Form1 that will open Form2.

Form2 newForm = new Form2();

newForm.ShowDialog();

When on Form2 and the selections are done, I need to send a string back to Form1 then Form2 closes.

I have been trying to get this to work for a few days now.

If someone has a sample, and I mean a complete example, not just a snippet, I need to see where I'm going wrong.

I've seen snippet upon snippet concerning this and there are variables in these snippets that I can't see how they are being declared.

Thanks,

Zath

Zath  Thursday, March 27, 2008 5:31 PM

Hi,

example for Form1

Code Snippet

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

Form2 frm = new Form2();

frm.Form2Text = "Before";

// assign variables to Form2 before calling ShowDialog

// ShowDialog will return control after Form2 is closed

if (frm.ShowDialog() == DialogResult.OK)

// you still can access object frm

// it is closed but not disposed

label1.Text = frm.Form2Text;

else

label1.Text = "operation canceled";

// when you leave the method object frm will go out of scope

// and will be disposed by Garbage Collector

// If want to access frm from other methods

// or keep it alive, you have to define a class variable for it

}

}

and for Form2

Code Snippet

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

}

public string Form2Text

{

get { return this.textBox1.Text; }

set { this.textBox1.Text = value; }

}

private void buttonOK_Click(object sender, EventArgs e)

{

this.DialogResult = DialogResult.OK;

}

private void buttonCancel_Click(object sender, EventArgs e)

{

this.DialogResult = DialogResult.Cancel;

}

}

The type of Form2Text could be every object you want to pass to form2.

The two buttons in Form2 allows the user to pass status info to Form1. If you assign something to DialogResult of the Form the from will close.

This code sample only works for ShowDialog() and not Show().

regards

Philipp

Philipp Merz  Thursday, March 27, 2008 5:54 PM

Hi,

example for Form1

Code Snippet

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

Form2 frm = new Form2();

frm.Form2Text = "Before";

// assign variables to Form2 before calling ShowDialog

// ShowDialog will return control after Form2 is closed

if (frm.ShowDialog() == DialogResult.OK)

// you still can access object frm

// it is closed but not disposed

label1.Text = frm.Form2Text;

else

label1.Text = "operation canceled";

// when you leave the method object frm will go out of scope

// and will be disposed by Garbage Collector

// If want to access frm from other methods

// or keep it alive, you have to define a class variable for it

}

}

and for Form2

Code Snippet

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

}

public string Form2Text

{

get { return this.textBox1.Text; }

set { this.textBox1.Text = value; }

}

private void buttonOK_Click(object sender, EventArgs e)

{

this.DialogResult = DialogResult.OK;

}

private void buttonCancel_Click(object sender, EventArgs e)

{

this.DialogResult = DialogResult.Cancel;

}

}

The type of Form2Text could be every object you want to pass to form2.

The two buttons in Form2 allows the user to pass status info to Form1. If you assign something to DialogResult of the Form the from will close.

This code sample only works for ShowDialog() and not Show().

regards

Philipp

Philipp Merz  Thursday, March 27, 2008 5:54 PM

Thanks!

The whole time I was missing this line on Form2:

this.DialogResult = DialogResult.OK;

Zath

Zath  Thursday, March 27, 2008 6:08 PM

You can use google to search for other answers

Custom Search

More Threads

• PDF?
• Large text files on textbox\richtextbox
• DataGridViewComboBoxColumn - Display vs. Actual (data) value
• Clearing a Windows Forms panel containing a DirectX movie
• Application Settings Upgrade
• ListView column creation race condition?
• Context Menu Question
• Backgroundworker question
• ListView Questions
• Error: At least one recipient is required, but none were found.