简体   繁体   English

如何将UI从一种形式更新为另一种形式

[英]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. 我有一个Form1,其中包含一个组合框,其中显示了一些保存在数据库中的数字,它还包含一个按钮( butn2 ),该按钮在单击时会弹出另一个表单,另一个按钮( butn1 )从数据库中更新组合。 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 在此表单(Form2,某种子表单)上,我尝试通过创建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: Form1代码:

public Form1()
{
  InitializeComponent(); 
}

Form1.Designer.cs: 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: Form2代码:

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? 如何从Form2按钮单击更新Form1的此组合框?

Let's suppose that the name of your first Form is Foobar. 让我们假设您的第一个Form的名称是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对象,您需要:

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 . 说明: obj1null初始化。 A cycle is created to find the Form you want to find, the end sign being either completed iteration or the Form being found. 创建一个循环来查找您要查找的Form ,结束符号要么是完成的迭代,要么是找到的Form If the Form is found, then obj1 is initialized. 如果找到了Form ,则obj1被初始化。 After the cycle, if obj1 was initialized, then you can use it, its members and methods, including, but not limited to button_Save_Click . 循环之后,如果obj1已初始化,则可以使用它,它的成员和方法,包括但不限于button_Save_Click

You also can show form2 with parent form1 您还可以显示带有父form1的form2

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. 然后,您可以通过form2的Owner属性访问form1。

this.butn2.Click += new System.EventHandler(Owner.button_Save_Click);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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