我有两个表单,我想从Form2中重新加载Form1中的组合框项目。我设置Form1为Form2的MdiParent,如下所示:
Form2 f2 = new Form2();
f2.MdiParent = this;
f2.Show(); 如何从Form2中访问Form1控件?
发布于 2013-03-07 21:42:50
尝尝这个,
String nameComboBox = "nameComboBoxForm1"; //name of combobox in form1
ComboBox comboBoxForm1 = (ComboBox)f2.MdiParent.FindControl(nameComboBox);发布于 2013-03-07 21:45:12
在Form1上,您需要定义如下属性:
Public ComboBox.ObjectCollection MyComboboxitems{
get{ return {the Combox's name}.Items}
}然后在Load事件处理程序中的Form2上:
{name of form2 combobox}.Items = ((Form1)Me.MdiParent).MyComboboxitems;这是为了不在表单1上公开组合框的所有属性,只显示您想要的属性。
在代码示例中,将{...}替换为实际的对象名称。
发布于 2013-03-07 22:20:55
您可以在Form1中声明一个公共静态列表,并将其设置为form1combobox的数据源,如下所示。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.IsMdiContainer = true;
}
public static List<string> list;
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.MdiParent = this;
frm2.Show();
comboBox11.DataSource = list;
}
}在form2的load事件中,将声明的form1列表设置为引用具有form2.combobox项的新实例化列表。
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
List<string> list = new List<string> { "a", "b", "c" };
private void Form2_Load(object sender, EventArgs e)
{
comboBox1.DataSource = list;
Form1.list = list;
}
}https://stackoverflow.com/questions/15272105
复制相似问题