I\'m doing a winform which consist a listbox (listbox10) with some items on it. When I doubleclick an item, it will show another form (Form3) which consist a textbox (textbo
Supposing the textbox1 is a control inside msgForm3 then the right syntax should be
msgForm3.textBox1.Text = listBox10.SelectedItem.ToString();
This requires changing the property Modifiers from Private to Public for the textBox1 using the Form Designer property window.
I don't recommended this change. It is better to pass the new value inside the constructor of msgForm3 like this
msgForm3 = new Form3(listBox10.SelectedItem.ToString());
msgForm3.Show();
and then in the constructor of msgForm3 add
public Form3(string initialValue)
{
InitializeComponent();
this.textBox1.Text = initialValue;
}
or you can add a public method to your Form3 class like this
public void InitText(string initialValue)
{
this.textBox1.Text = initialValue;
}
and call in this way
msgForm3 = new Form3();
msgForm3.InitText(listBox10.SelectedItem.ToString());
msgForm3.Show();
The textBox1
you are accessing is not on msgForm3
. to access the one on msgForm3
use (as the other answers have mentioned)
msgForm3.textBox1.Text = listBox10.SelectedItem.ToString();
but since all form controls are private
by default you can either change its protection level inside Form3.Designer.cs
to public
(or internal
) :
public System.Windows.Forms.TextBox textBox1;
or add the text that should go in textbox1
as a parameter in Form3 constructor :
public Form3(string text)
{
InitializeComponent();
this.textBox1.Text = text;
}
and when your create an instance of Form3 use
msgForm3 = new Form3(listBox10.SelectedItem.ToString());
This is how you can access the member of the other form.
msgForm3 = new Form3();
msgForm3.textBox1.Text = listBox10.SelectedItem.ToString();
msgForm3.Show();
EDIT:
From the Form3.Designer.cs
file just change protection level from private
to public
.
Replace
private System.Windows.Forms.TextBox textBox1;
To
public System.Windows.Forms.TextBox textBox1;