简体   繁体   中英

How to update UI from one form to another form

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?

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 .

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM