There are 2 forms: the main one and the form created by clicking on radioButton. There is a textBox on the main form, and a button on the second form. Question: how can I make the textBox content on the first one change when I click a button on the second form? Where do I need to transfer the main form?

  • C # has no buttons. Specify correct question marks. C # is just a programming language. - Vlad from Moscow
  • Get event on the second form and call it at the touch of a button. And the main form will be assigned by the handler of this event and will change itself what follows in its components. - Igor

2 answers 2

An example of writing
try so, only at the object to which you apply, put the modifier Public

  • = yourForm() - chic syntax. Not that. - Qwertiy
  • one
    1. make an object modifier to which you want to refer to "Public". 2. Declare the form in the class is admissible: (Form1 form1 = new Form1 ();) 3. Then you already refer to this form - Sergey Neykovich
  • one
    Bad idea. 1. It is not recommended for objects to make the public access modifier. 2. Creating a second copy of the first form through the constructor, you will not receive a link to the first form (it’s better to add the form1 type parameter to the second form constructor and pass this to it when creating the second form. - Oleg Klezovich
  • @OlegKlezovich agrees with you - Sergey Neykovich

In general, I am in favor of doing what you indicated in the comments. Namely:

 //form1 public partial class Form1 : Form { ... private void radioButton1_CheckedChanged(object sender, EventArgs e) { Form2 f2 = new Form2(this); f2.Show(); } public void ChangeTextInTextBox(string newText) { textBox1.Text = newText; } } //form2 public partial class Form2 : Form { public delegate void ChangeTextBox(string nText); public event ChangeTextBox ChangeTextBox1; public Form2(Form1 f1) { InitializeComponent(); ChangeTextBox1 += f1.ChangeTextInTextBox; } private void button1_Click(object sender, EventArgs e) { ChangeTextBox1("New Text"); } } 

For me, so briefly and quite beautifully, and it seems that it does not violate the principles of the PLO.