Since C # has an event in Event Callback, why not? In addition, events play a more convenient role in form communication. We know that events are actually state capturing, at the end, I will give an example of the capture status. First, let's look at the examples of data operations. Form2:
// Define a delegate that requires a string-type parameter
Publicdelegate void MyDelegate (string text );
Public partial class Form2: Form1
{
// Define the delegate event
Public event MyDelegate MyEvent;
Public Form2 (string text)
{
InitializeComponent ();
This. textBox1.Text = text;
}
Private void btnChange_Click (object sender, EventArgs e)
{
// Trigger the event and send the modified text back
MyEvent (this. textBox1.Text );
This. Close ();
}
}
Form1: public partial class Form1: Form
{
Public int index = 0;
Public string text = null;
Public Form1 ()
{
InitializeComponent ();
}
Private void listBox1_SelectedIndexChanged (object sender, EventArgse)
{
If (this. listBox1.SelectedItem! = Null)
{
Text = this. listBox1.SelectedItem. ToString ();
Index = this. listBox1.SelectedIndex;
Form2 form2 = new Form2 (text );
// Register the MyEvent event of the form2_MyEvent Method
Form2.MyEvent + = new MyDelegate (form2_MyEvent );
Form2.Show ();
}
}
// Process
Void form2_MyEvent (string text)
{
This. listBox1.