我正在制作一个Windows窗体项目,并且难以将动态生成的控件值传递给另一个窗体的正常控件值。
我的代码是:
int c = 0;
int p = 0;
private void button1_Click(object sender, EventArgs e)
{
panel1.VerticalScroll.Value = VerticalScroll.Minimum;
// panel1.HorizontalScroll.Value = HorizontalScroll.Minimum;
ComboBox txtRun3 = new ComboBox();
txtRun3.Name = "txtDynamic" + c++ ;
txtRun3.Location = new Point(30, 18 + (30 * c))
panel1.Controls.Add(txtRun3);
txtRun3.Focus();
}
private void button2_Click(object sender, EventArgs e)
{
Form4 f4 = new Form4();
Button bt = sender as Button;
ComboBox cb2 = bt.Tag as ComboBox;
f4.Combo.Text = bt.Text;
}
我收到错误:
“你调用的对象是空的。”
答案 0 :(得分:3)
在Form4
public ComboBox Combo
{
get
{
return this.comboBox1;
}
set
{
this.comboBox1 = value;
}
}
然后你可以这样访问它:
Form4 f4 = new Form4();
Button bs = (Button) sender;
f4.Combo.Text = bs.Text;