an event in C # that triggers another form in one form creates two forms, Form1,form2, add controls TextBox1 and Button1 in Form1, create a Form2 object Form2 b=NULL; add button1 in Form2, define delegates and events//Defining Delegates Public Delegate voidMyDelegate (); //Defining Events Public Eventmydelegate myevent; Add the message corresponding function to the button1 in Form2 and make the modificationPrivate voidButton1_Click (Objectsender, EventArgs e) { if(MyEvent! =NULL) MyEvent ();//Raising an event This. Close (); Add a function in the Form1 codevoidb_myevent () { This. TextBox1.Text + ="clicked the B form button \ r \ n"; Modify the Form1 constructor PublicForm1 () {InitializeComponent (); b=NewForm2 ();//Instantiate B formB.myevent + =NewForm2.mydelegate (b_myevent);//listening for B form EventsAdd the message response function for Button1 in Form1Private voidButton1_Click (Objectsender, EventArgs e) {B.showdialog (); This will pop up Form2 when you click a button in Form1, and TextBox1 in Form1 will display the "B form button clicked" When you click the button in Form2. The specific code is as follows (VS 2005 implementation): Form1 code:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespaceform1{ Public Partial classForm1:form {Form2 b=NULL; PublicForm1 () {InitializeComponent (); b=NewForm2 ();//Instantiate B formB.myevent + =NewForm2.mydelegate (b_myevent);//listening for B form Events } voidb_myevent () { This. TextBox1.Text + ="clicked the B form button \ r \ n"; } Private voidButton1_Click (Objectsender, EventArgs e) {B.showdialog (); }}}form2 Code:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespaceform1{ Public Partial classForm2:form { PublicForm2 () {InitializeComponent (); } //Defining Delegates Public Delegate voidMyDelegate (); //Defining Events Public EventMyDelegate MyEvent; Private voidButton1_Click (Objectsender, EventArgs e) { if(MyEvent! =NULL) MyEvent ();//Raising an event This. Close (); } }}
Events in C # that trigger another form in one form