don t

don t

  • NA
  • 3
  • 4.5k

How to pass the data of my selection from SubForm to the MainForm

Sep 20 2012 7:25 PM
I have two forms. MainForm and SubForm.  SubForm has a combobox. Select button on MainForm and the SubForm opens. I select one of the options on the combobox click ok button and SubForm closes. I want to pass the data of my selection to the MainForm.  I know this is simple but I keep messing it up somehow. Please show code with help thanks.

MainForm code is

string newrecordtype = null;  formSubForm newSubForm = new formSubForm(newrecordtype);
newSubForm.ShowDialog();
MessageBox.Show(newrecordtype.ToString());

SubForm code is

 public partial class FormNewRecordTypeSelection : Form

{
 public string newrecordtype = null;
 public FormNewRecordTypeSelection(newrecordtype)
 {
 InitializeComponent();
 }

 public
 void buttonOK_Click(object sender, EventArgs e)
 {
 object selectedIndex = comboBox1.SelectedIndex;
 Object selectedItem = comboBox1.SelectedItem;
 MessageBox.Show("Selected Item Text: " + selectedItem.ToString() + "\n" +
 "Index: " + selectedIndex.ToString());  newrecordtype  = selectedItem.ToString();
 }


Answers (2)