【发布时间】:2016-03-07 17:40:38
【问题描述】:
我有 Form1,其中包含一个组合框,其中显示了一些保存在数据库中的数字,它还包含一个按钮 (butn2),单击时会弹出另一个表单和另一个按钮 (butn1),它会更新数据库中的组合。
在此表单(Form2,某种子表单)上,我尝试通过创建Form1 的对象来更新按钮单击时前一个表单(父表单)的组合框的数据。
但是当我打开并看到组合框时,它仍然显示相同的数据(它没有更新)。
是否可以将 UI 从组合框从一种形式更新到另一种形式?我的代码是
Form1代码:
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代码:
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.
如何通过点击 Form2 按钮更新 Form1 的这个组合框?
【问题讨论】:
-
您可能会在此处找到一些提示:stackoverflow.com/a/5647064/93623 在该答案中,将
SomeClass视为您的第二种形式。 -
您在 form2 代码中创建新的 form1 obj,您需要通过 Application.OpenForm 集合通过迭代表单或 Form frm123=Application.OpenForms["form1"]; 来获取 form1 的对象。 ,还要确保组合框修饰符是内部的或公共的。
标签: c# forms winforms button combobox