1. Delegate
Two forms: the form is very simple. Only the color change function is implemented:
CodeAs shown below, only the button Event code is pasted:
Open the form2 button event
Private Void Button#click ( Object Sender, eventargs e) {form2 frm2 = New Form2 (); frm2.changecolor + = New Changeformcolor (frm2_changecolor); frm2.show ();} Void Frm2_changecolor ( Bool Topmost ){ This . Backcolor = Color. lightblue; This . Text = " Changed successfully! " ;} Button for changing the color of form1
// Define a delegate Public Delegate Void Changeformcolor ( Bool Topmost ); // Give an event Public Event Changeformcolor changecolor; Private Void Button#click ( Object Sender, eventargs e) {changecolor ( True ); This . Close ();}
2. Let's take a look.
After the modification, press confirm, disable form2, and automatically refresh form1. The idea is as follows: Query on form1 to display the datagridview, right-click and choose modify. The mode form form2 is displayed. Modify and close, automatically refresh form1.
Form1 form code
//Add to the modify button. The mode form form2 is displayed to modify it.Form2 form2 =NewForm2 (); form2.owner=This; Form2.showdialog ();
Confirm button code
If(//Your modification method){MessageBox. Show ("Modified successfully!");//Forcibly convert a form with form2 to form1, and assign the value to form1, a variable of the form1 form type.Form1 form1 = (form1)This. Owner;//Note that the following select () method is a query method in form1. Its function is equivalent to refresh, but refresh does not work. It doesn't matter. Call this method to refresh it.Form1.Select(); Close ();}
3. The third type is a little troublesome, but not bad (if the modified data is single and relatively small)
The two forms are as follows (the interface is very simple and mainly used for demonstration. However, if you use a button in form2 to submit data, you cannot close the form. You must write it to the formclosed event, depressing ...... who has a better way to communicate with each other ):
Click choose to pop up form2. After selecting the gender, close the form and pass the data to the form1sex box. If there are few codes, paste them all.
All form1 form code
Using System; Using System. Collections. Generic; Using System. componentmodel; Using System. Data; Using System. drawing; Using System. LINQ; Using System. text; Using System. Windows. forms; Namespace Test { Public Partial Class Form1: FORM { Public Form1 () {initializecomponent ();} Private Void Button#click ( Object Sender, eventargs e) {form2 frm2 = New Form2 (); If (Frm2.showdialog () = Dialogresult. OK ){ // The mode form pops up and assigns a value to strsex of the public attribute in form2. Textbox2.text = Frm2.strsex; frm2 = Null ; // Clear }}}} Form2 form code
Using System; Using System. Collections. Generic; Using System. componentmodel; Using System. Data; Using System. drawing; Using System. LINQ; Using System. text; Using System. Windows. forms; Namespace Test { Public Partial Class Form2: FORM { Public Form2 () {initializecomponent ();} // Define a private property STR for storing sex. If you use any property, add the property by yourself. When you finish, stop the cursor to the property STR and press Ctrl + R, e to automatically encapsulate the property. Private String STR; Public String Strsex // Here the automatic encapsulation should be Str. I changed the name and the table struggled with this { Get { Return STR ;} Set {STR = Value ;}} Private Void Form2_formclosed ( Object Sender, formclosedeventargs e ){ // Close the form after the assignment This . Strsex = Combobox1.selectedtext; This . Dialogresult = Dialogresult. OK ;}}}
OK, I have finished writing. This is mainly to take notes for myself, so that you can read the notes later. If you don't read the notes, you will see the notes. If there is anything wrong, please point it out, don't use jww. despise such people! I am also a beginner. Who is not here !!!
Reprinted please indicate the source: http://www.cnblogs.com/xw-yanger/archive/2012/06/26/2563562.html
Send an email if there is a problem: Xw_yanger@163.com
Everyone makes common progress!