There is Form1 . It contains richTextBox1 and Button1 . Button1 calls Form2 .

On Form2 there is textBox1 and Button1 .

QUESTION: How can Form2.Button1 transfer a text message from Form2.textBox1 to Form1.richTextBox1 when I click on Form1.richTextBox1 ? While not closing any of the forms?

There is a suspicion that the best and most universal way to do this is to use delegates. But something I can not figure out how to do it right?

  • write an event in the arguments of which you'll transfer data from the text field, subscribe to this event on the first form, the event should work when you press the button, well, this is briefly - Dmitry

2 answers 2

Event Arguments

 public class MyEventArgs : EventArgs { private readonly string _text; public string Text { get { return _text; } } public MyEventArgs(string text) { _text = text; } } 

Form 2:

 public partial class Form2 : Form { public Form2() { InitializeComponent(); } public delegate void MyEventHandler(object sender, MyEventArgs args); public event MyEventHandler MyEvent; protected virtual void MyEventRaise() { if(MyEvent != null) MyEvent(this, new MyEventArgs(textBox1.Text)); } private void button1_Click(object sender, EventArgs e) { MyEventRaise(); } } 

on the first forum subscribe to the event

 var form2 = new Form2(); form2.MyEvent += Form2_MyEvent; 

we understand that we have come there

 private void Form2_MyEvent(object sender, MyEventArgs args) { // Текст из текстбокса, вставляем куда нам надо richTextBox1.Text += args.Text; } 
  • I apologize for the Dummy question, but even so ... Anyway, where is the MyEventArgs class placed? - Mikhail Danshin Nov.
  • @MikhailDanshin Place it in a separate file, if you do not know where else to put it, this will be the most correct decision, and so, you should read about nimspaces - Dmitry
  • I get three errors when compiling. CS1520 Returns S0103 The name '_text' doesn’t contain a constructor that takes 1 arguments - Mikhail Danshin
  • I'm going quite successfully, check if everything is right in your code - Dmitry
  • Thank! Found a mistake! Works Great! What you need! - Mikhail Danshin

There are several ways to solve this problem:

  1. Pass in Form2 a reference to Form1 - this can be done in three classic ways: constructor, property, context;

Constructor:

form 1

 var form_2 = new Form2(this); 

form 2

 private Form1 form_1; public Form2(Form1 obj) { this.form_1 = obj; } private void f() { this.form_1.RichTextBox1.Lines[0] = "Hello"; } 

Property:

Form 1:

 var form_2 = new Form2(); form_2.UpForm = this; 

Form 2:

 public class Form2 : Form { public Form1 UpForm { get; set; } private void f() { this.UpForm.RichTextBox1.Lines[0] = "Hello"; } } 

Context:

It is done through a static object.

An object:

 static internal class GLOBAL { static public Form1 CONTEXT_FORM; } 

Form 1:

 GLOBAL.CONTEXT_FORM = this; var form_2 = new Form2(); 

Form 2:

 private void f() { GLOBAL.CONTEXT_FORM.RichTextBox1.Lines[0] = "Hello"; } 
  1. Via event: described in Form2 event, and in Form1 to subscribe to it. (You already described Dmitry)
  • Is it possible to give a specific example of both methods? - Mikhail Danshin
  • I did not quite understand about the transfer to Form2 of a link to Form1 through the designer. What in your example is the variable form_1 and where should it be declared? - Mikhail Danshin
  • @MikhailDanshin, yes I didn’t pee :(, I'll fix it now. In form 2 there should be such a variable or property. - Mirdin
  • Suppose this is a property declared in Form2. But what do I pass to the constructor via the this keyword? If I passed an instance of a class, then I would have access to its methods, properties, objects, and so on. Otherwise, Visual Studio does not give access to RichTextBox1. And here I do not understand how to be. - Mikhail Danshin
  • @MikhailDanshin this is a link to yourself - Mirdin