Adrian Brown
Adrian Brown

Reputation: 133

Set label text on a form from the value of a combobox on another form c#

I have Form1 which has a comboBox with various options. Form2 is opened from Form1 via a button click.

I want a label on Form2 to have its text set to the value of the comboBox on Form1.

I come from VBA so tried

this.label1.Text = Form1.comboBox1.Text;

But this doesn't work. Whats the simplest way of doing this?

Upvotes: 2

Views: 587

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

Just pass combobox text to constructor of Form2:

public Form2(string something)
{
   InitializeComponent();
   this.label1.Text = something; // initialize label text
}

When opening Form2:

using(var form2 = new Form2(comboBox1.Text)) // pass
{
   form2.ShowDialog();
}

Upvotes: 3

Related Questions