In UserControl there is a Textbox, there is a text in it. This text needs to be transferred to another TextBox in a new form.

From another class I try to show text from TextBox (which is in UserControl)

In UserControl in the field TextBox'a put Public access (so that you can get out of the form to TextBox'a)

MyUserControl PC = new MyUserControl(); TextBox.Text = PC.NameTextBox.Text); 

But the text is not shown.

How to output text from UserControl to a TextBox of another form?

  • And how is the UserControl added to the form (or where is it displayed)? - default locale
  • @defaultlocale, MyUserControl PC = new MyUserControl(); and read data from it: PC.NameTextBox.Text - Luser
  • one
    @AlexKrass, Get the text turned out, ATP. - Luser
  • one
    @ Dmitriy is the most correct to use for this kind of interaction the context context — a model class that is created once and passed to the designer of each form, and through it the data exchange is organized. In WinForms, the MVC pattern is most common. In WPF - MVVM (C) - Lunar Whisper

1 answer 1

Well, as an option, you can pervert and do so

 private void Form2_Load(object sender, EventArgs e) { var T = Form.ActiveForm.Controls.OfType<Panel>().FirstOrDefault(g => g.Name == "panel1").Controls.OfType<UserControl1>().FirstOrDefault(); var y = T.Controls.OfType<Label>().FirstOrDefault().Text; MessageBox.Show(y); } 

When loading form 2, we take the text Label from UserControl with Form1