shariq_khan
shariq_khan

Reputation: 681

How do I pass a value from dynamic generated combobox to the next form's combobox?

I am making a windows form project and facing difficulty in passing the dynamically generated control value to the other form's normal control value.

My code is:

        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;
         }

I am getting the error:
"Object reference not set to an instance of an object."

Upvotes: 1

Views: 449

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460118

Provide a public property in the Form4

public ComboBox Combo
{
    get
    {
        return this.comboBox1;
    }
    set 
    {
        this.comboBox1 = value;
    }
}

Then you can access it in this way:

Form4 f4 = new Form4();
Button bs = (Button) sender;
f4.Combo.Text = bs.Text;

Upvotes: 3

Related Questions