G
G
Georgy Voronko2017-07-04 17:29:27
C++ / C#
Georgy Voronko, 2017-07-04 17:29:27

When displaying text from a TextBox from one form to a label of another form, the label disappears why?

I recently started learning C#.
Stupid problem, but when text is displayed from a TextBox from one form to the label of another form, the label disappears. why?
Here is the button code.

private void B_Ready_Click(object sender, EventArgs e)
        {
            Form_Registration f1 = new Form_Registration();
            form2 f2 = new form2();

            f2.Show();

            f2.LB_name.Text = f1.TB_name.Text;
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arkadiy Parinov, 2017-07-04
Georgy Voronko @

1) Not a good practice to give access to form controls from outside. It is better to define a public method that will change the text of the label, but access directly to the control will be closed inside the form (well, or here as a public property)

// this code is inside form2
public string LabelText
        {
            get
            {
                return this.LB_name.Text;
            }
            set
            {
                this.LB_name.Text = value;
            }
        }

2) Perform operations on working with the child form BEFORE calling the Show () method, because after that, the control will go to the new form and the code after that will not be executed
private void B_Ready_Click(object sender, EventArgs e)
{
    var f2 = new Form2();
    f2.LabelText = "bla bla";
    f2.Show();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question