C# Text don't display on another form after double clicking an item in listbox

后端 未结 3 1912
滥情空心
滥情空心 2020-12-21 18:40

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

3条回答
  •  再見小時候
    2020-12-21 18:48

    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());
    

提交回复
热议问题