-2

how can I display members in comboBox in form2 from form1 comboBox?

Form form1 = Application.OpenForms["Form1"];

private void BookSetupForm_Load(object sender, EventArgs e)
{
    comboBox1.DataSource = form1.comboBox1;
}

The question is different as I want to transfer comboBox values not textboxes values and uses a different approach.

Patrick
  • 217
  • 1
  • 5
  • 19

1 Answers1

1

Since the ComboBox on your Form 1 is not public you would need an extra field or property as transfer object:

public partial class Form1 : Form
{

    public ComboBox comboTransfer;

    public Form1()
    {
        InitializeComponent();

        // example combobox
        this.comboBox1.Items.AddRange(new string[] { "1", "2", "3" });
        // reroute the content
        this.comboTransfer = comboBox1;
    }
}

In Form 2 you could access then this transfer object

public partial class Form2 : Form
{
    Form1 Form_1;
    public Form2()
    {
        InitializeComponent();

        Form_1 = Application.OpenForms["Form1"] as Form1;
    }

    private void BookSetupForm_Load(object sender, EventArgs e)
    {
        // access here your transfer object
        this.comboBox1.DataSource = Form_1.comboTransfer.Items;
    }
}

this is just one way of doing it (first that came to my mind)

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76