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

后端 未结 3 1894
滥情空心
滥情空心 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:46

    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();  
    
    0 讨论(0)
  • 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());
    
    0 讨论(0)
  • 2020-12-21 18:48

    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;
    
    0 讨论(0)
提交回复
热议问题