Events in C # that trigger another form in one form

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.