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 (); } }}