[C # Learning] calling controls between forms

Source: Internet
Author: User

First, Method 1:

If there are two forms, form_a and Form_b, each form has a key, button_a and Button_b, to implement click Button_a to display Form B, then the program for the Buttom_a click event in form a should be:

Private void Button_a_click (object  sender, EventArgs e) {    form_b  new  Form_b ( );    F.show ();}

If you want to change the background color of form a by clicking the key button_b in form B, then you need to do:

1. Add the following code to the class in the Form_b form:

 Public form_a FB;  Public Form_b (form_a  f) {    InitializeComponent ();     = F;} Private void Button_b_click (object  sender, EventArgs e) {    = color.brown;}

2. The Button_a click event in the Form_a form becomes:

Private void Button_a_click (object  sender, EventArgs e) {    new form_b ( this );    F.show ();}

The complete program is as follows

Form_a:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceformexam{ Public Partial classForm_a:form { PublicForm_a () {InitializeComponent (); }        Private voidButton_a_click (Objectsender, EventArgs e) {Form_b F=NewForm_b ( This);        F.show (); }        Private voidForm_a_load (Objectsender, EventArgs e) {        }    }}

Form_b:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceformexam{ Public Partial classForm_b:form { PublicForm_a FB;  PublicForm_b (form_a f) {InitializeComponent (); FB=F; }        Private voidButton_b_click (Objectsender, EventArgs e) {fb. BackColor=Color.brown; }    }}

Ii. Method 2: Implementation through delegation

1. Define a delegate type outside the class of Form_b

 Public Delegate void Changeformcolor ();

2. Define a delegate's method within the class of Form_b

 Public Event Changeformcolor ChangeColor;

3. Button_b Click event to

Private void Button_b_click (object  sender, EventArgs e) {    changecolor ();}

4. The Click event in Form_a is

Private void Button_a_click (object  sender, EventArgs e) {    new  form_b ();     New Changeformcolor (f_changecolor);    F.show ();}

5. Write a method to change the form_a background color

void F_changecolor () {    this. BackColor = Color.brown;}

The complete procedure is as follows:

Form_a:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceformexam{ Public Partial classForm_a:form { PublicForm_a () {InitializeComponent (); }        Private voidButton_a_click (Objectsender, EventArgs e) {Form_b F=NewForm_b (); F.changecolor+=NewChangeformcolor (F_changecolor);        F.show (); }        voidF_changecolor () { This. BackColor =Color.brown; }    }}

Form_b:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceformexam{ Public Delegate voidChangeformcolor ();  Public Partial classForm_b:form { Public EventChangeformcolor ChangeColor;  PublicForm_b () {InitializeComponent (); }        Private voidButton_b_click (Objectsender, EventArgs e)        {ChangeColor (); }    }}

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.