如何将动态生成的组合框中的值传递给下一个表格的组合框?

时间:2012-12-12 09:49:15

标签: c# winforms

我正在制作一个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;
         }

我收到错误:
“你调用的对象是空的。”

1 个答案:

答案 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;