Windows Develop Bookmark and Share   
 index > Windows Forms General > passing values between 2 forms
 

passing values between 2 forms

Hello, I have two forms. The first form (Form1) contains a textbox and button. The second one (Form2) contains a combo and button. when the button on the form1 is pressed, form2 will be active. Then the user should choose some value from the combobox and press the button in the form2. When this button is pressed, the selected value from the combobox should be displayed in the textbox in the first form, and the second form should close itself. The problem is displaying the selected text in the textbox in the first form. Please help Thanks
Barbi Rio  Friday, September 04, 2009 10:58 AM
// in form1

Form2 form2 = new Form2(textBox1.Text);
if (form2.ShowDialog(this) == DialogResult.OK)
    textBox2.Text = form2.Selection;

// in form2

private string selection;
public string Selection { get { return selection; } }

button1_Click(object sender, EventArgs e)
{
    selection = comboBox1.SelectedText;
    // may need to use comboBox1.SelectedItem.ToString(), I forget

    DialogResult = DialogResult.OK; // will close form for you
}
ScottyDoesKnow  Friday, September 04, 2009 3:59 PM
a little entry inside the search box of the forum gives you the answer

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/8e28380c-a57a-4a3d-98b3-91166ce089ea/
Christoph Wagner  Friday, September 04, 2009 11:47 AM
you can simply use form2.combobox1.selectedtext, to get the selected text and display the same in form1...
Vicky
Vikram Gorla  Friday, September 04, 2009 1:11 PM

Another method uses the delegate:

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/12d4df45-d9ba-4fe3-9af0-8c7ca31681d0

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Friday, September 11, 2009 7:31 AM
Assign the selected text property to a static variable in the common class. when form2 closes access that property to get value and show it in form1
Hassan Mehmood  Friday, September 11, 2009 7:38 AM
a little entry inside the search box of the forum gives you the answer

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/8e28380c-a57a-4a3d-98b3-91166ce089ea/
Christoph Wagner  Friday, September 04, 2009 11:47 AM
you can simply use form2.combobox1.selectedtext, to get the selected text and display the same in form1...
Vicky
Vikram Gorla  Friday, September 04, 2009 1:11 PM
// in form1

Form2 form2 = new Form2(textBox1.Text);
if (form2.ShowDialog(this) == DialogResult.OK)
    textBox2.Text = form2.Selection;

// in form2

private string selection;
public string Selection { get { return selection; } }

button1_Click(object sender, EventArgs e)
{
    selection = comboBox1.SelectedText;
    // may need to use comboBox1.SelectedItem.ToString(), I forget

    DialogResult = DialogResult.OK; // will close form for you
}
ScottyDoesKnow  Friday, September 04, 2009 3:59 PM

Another method uses the delegate:

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/12d4df45-d9ba-4fe3-9af0-8c7ca31681d0

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Friday, September 11, 2009 7:31 AM
Assign the selected text property to a static variable in the common class. when form2 closes access that property to get value and show it in form1
Hassan Mehmood  Friday, September 11, 2009 7:38 AM

You can use google to search for other answers

Custom Search

More Threads

• Customized MessageBox Buttons?
• get Current textBox
• dotNetFx35SP1 issues
• Reading an XML file
• ToolStrip doesn't scale buttons in the overflow menu
• Screen of current windows form
• Cannot display image in menuItem Programmatically can you help
• GroupBox Painting Problem
• How can i create a drag-and-drop hierarchical grouping ?
• How to show a ABOUT Form????????