Hi guys I'm new here and willing to help in the future. Right now I'm developing a program, and I need to store the data selected in a combobox in a global variable, and use it in another form.

Basically I have a combobox where you select a company, these companies are loaded in the combo from a previous data base.
I want that when the user clicks the accept button, the selected company name that comes from the database gets stored in a variable.
It already does it, but only with the company that first appears in the combobox, because when the form loads this is preselected, and the button only hides and shows the next form.
I want that when the button is clicked, the selected data gets stored.

I have this already:

 DataTable ds = new DataTable();
            sda.Fill(ds); 
            myConnection.Close(); //Cierra la conexion
            cmbempre.DataSource = ds;
            cmbempre.DisplayMember = "cod_empresa";
            cmbempre.ValueMember = "cod_empresa";
            GlobalClass.GlobalVar1 = cmbempre.SelectedValue.ToString();

Skipping the conection string and adapter, and this is setted in the public partial class

Recommended Answers

All 3 Replies

myComboBox.SelectedItem gets the value that you have selected from the comboBox. If you want to pass this value to another form you simply need to instanciate a new instance of the 2nd form in the 1st form and do the bellow:

Form2 frm2 = new Form2();

// This is done inside of a method
frm2.textBox1.Text = ComboBox.SelectedItem.ToString();

Thanks for the response Chris, but it still doesen't works, because it takes the previously selected value, not the one that is selected once I click the accept button...

Thanks anyway :)

In that case, you must be doing something wrong. Chris's advice is correct. You create an instance of the form and after that you can pass values to any controls in the form.
Might be time to post up some code so we can have proper look.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.