1

I have Form1 which contains a combobox which show some number saved in Database and it also contain a button(butn2) which on click popups a another form and another button(butn1) which updates the combo from database. Here on this form (Form2, Child form of some sort)i try to updat the data of the combobox of previous form(parent one) on button click by creating object of Form1

But when i open and see the combobox it still show the same data(it is not updated).

Is it possible to update the UI from combobox from one form to another ? My code is

Form1 code:

public Form1()
{
  InitializeComponent(); 
}

Form1.Designer.cs:

Button butn1;
Button butn2;
ComboBox cmb1;
private void InitializeComponent()
 {
  cmb1 = new ComboBox();
  butn1 = new Button();
 }
this.butn1.Click += new System.EventHandler(this.button_Save_Click);
this.butn2.Click += new System.EventHandler(this.button_Save_Click2);

public void button_Save_Click(object sender, System.EventArgs e)
{
  UpdateComboBoxFromMySQL.InsertdataInCombo(this.cmb1 ); //Here i add data in combox through database, the code is correct i verfied it
}
public void button_Save_Click2(object sender, System.EventArgs e)
 {
    Form2 frm2 = new Form2();
    frm2.show();
 }

Form2 code:

Button butn2 = new Button();
//first i add some data to database, which are added i have seen the table-columns by opening DB. Now i want to update the Combobox from that data
Form1 obj1 = new Form();
this.butn2.Click += new System.EventHandler(obj1 .button_Save_Click); //It calls the function button_Save_Click, i saw on debugging but still it do not update the data.

How to update this combobox of Form1 from Form2 button click?

struggling
  • 535
  • 1
  • 10
  • 25
  • You may find some pointers here: http://stackoverflow.com/a/5647064/93623 In that answer, think of `SomeClass` as your second form. – Fredrik Mörk Dec 03 '15 at 07:53
  • Possible duplicate of [How to access a form control for another form?](http://stackoverflow.com/questions/4822980/how-to-access-a-form-control-for-another-form) – Fᴀʀʜᴀɴ Aɴᴀᴍ Dec 03 '15 at 07:54
  • you are creating new form1 obj in form2 code, you will need to get object of form1 through Application.OpenForm collection by iteracting the forms or Form frm123=Application.OpenForms["form1"]; , also make sure that combobox modifer is internal or public. – Sohail Anjum Dec 03 '15 at 07:55

2 Answers2

1

Let's suppose that the name of your first Form is Foobar. In that case, instead of

Form1 obj1 = new Form();
this.butn2.Click += new System.EventHandler(obj1 .button_Save_Click);

which creates a new Form object, you need this:

Form obj1 = null;
for (int i = ((obj1 == null) && (Application.OpenForms.Count - 1)); i >= 0; i--)
{
    if (Application.OpenForms[i].Name == "Foobar")
        obj1 = Application.OpenForms[i];
}
if (obj1 != null)
{
    this.butn2.Click += new System.EventHandler(obj1 .button_Save_Click);
}

Explanation: obj1 is initialized with null. A cycle is created to find the Form you want to find, the end sign being either completed iteration or the Form being found. If the Form is found, then obj1 is initialized. After the cycle, if obj1 was initialized, then you can use it, its members and methods, including, but not limited to button_Save_Click.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Combobox in parent form is still not updated ? , I have changed my child form code according to you, but still do not update combo box of parent form with updated database. – struggling Dec 03 '15 at 08:59
1

You also can show form2 with parent form1

public void button_Save_Click2(object sender, System.EventArgs e)
 {
    Form2 frm2 = new Form2();
    frm2.Show(this);
 }

Then you can access to form1 through the Owner property of form2.

this.butn2.Click += new System.EventHandler(Owner.button_Save_Click);
melya
  • 578
  • 5
  • 24