1

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

我收到错误消息:
“对象引用未设置为对象的实例。”

4

1 回答 1

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;
于 2012-12-12T09:52:46.707 回答