Control query between different forms - Method 2 (event and callback)

xiaoxiao2021-03-06  23

About the control query between different forms - mode two ====================================== ======================== New catename is named: ucformQueryExtender.cs ================= ===================================================== Using system; use system .Collections; using system.componentmodel; using system.windows.form

Namespace queryform2 {

/ / Query Event Entrusted Public Delegate Void QueryObjectEventHandler (UcQueryObjectProp P_Queryop); // Tune Event Entrusted Public Delegate Void CallbackeVentHandler (Object P_Object);

///

/// Form control query expander (using event registration callback mode) /// public sealed class ucformqueryextender {public static event queryObjectEventHandler query;

// Query public static void doquuery (ucqueryObjectprop p_queryop) {if (query! = Null) {query (p_queryop);}}

Public Static Event Callback; // Tonance Public Static Void Docallback (Object P_Object) {if (Callback! = NULL) {Callback (p_Object);}}

// Public object query method public static void findObject (Form p_currform, ucQueryObjectProp p_queryop) {

//

Ucquerytype querytype = p_queryop.querytype; string formName = p_queryop.formname; string controlname = p_queryryop.controlname;

//

Object FindObject = NULL;

if (! FormName.ToLower () = p_CurrForm.Name.ToLower ()) {return;} if (QueryType == UcQueryType.Form) // query form {FindObject = p_CurrForm;} else if (QueryType == UcQueryType.Control ) // Query control {Foreach (Control Ctr in p_currform.controls) {if (ctr.name.tolower () == ControlName.tolower ()) {FindObject = Ctr;}}}

UcformQueryExtendeer.docallback (FindObject);

}

// Query the form of the form PUBLIC ENUM ucQuerytype {form, control}

// Structure of the query parameter (Parameters in structure) public struct ucqueryObjectProp {public string formname; public string constroname;}};} PUBLIC STRING FORMNAME

} ==================================================== ============= New form name is: form1.cs =========================== ============================================ ingry system.drawing; use system.collections; using system.collections; using system.collections; using system.collections; .Componentmodel; using system.windows.form; system.threading;

A summary description of Namespace Queryform2 {///

/// Form1. /// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Button button1; private System.windows.Forms.Radiobutton radiobutton1; private system.windows.Forms.Radiobutton radiobutton2; /// // / / The required designer variable. /// private system.componentmodel.container components = null; // Query method

Ucquerytype querytype = new ucquerytype ();

Public Form1 () {// // Windows Form Designer Support for // InitializeComponent ();

// / / TODO: Add any constructor code after INITIALIZEComponent call //

Thread T1 = New Thread (New ThreadStart (Showform2)); Thread T2 = New Thread (New ThreadStart (ShowForm3));

T1.Start (); t2.start ();

}

Private void showform2 () {Application.run (New Form2 ());

Private void showform3 () {Application.run (New Form3 ());

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {this.textBox1 = new System.Windows.Forms.TextBox (); this.textBox2 = new System.Windows.Forms.TextBox (); this.button1 = new System. Windows.Forms.Button (); this.radioButton1 = new System.Windows.Forms.RadioButton (); this.radioButton2 = new System.Windows.Forms.RadioButton (); this.SuspendLayout (); // // textBox1 / / This.TextBox1.Location = new system.drawing.point (24, 40); this.textbox1.name = "textbox1"; this.TextBox1.tabindex = 0; this.textBox1.text = "formname"; // / / textbox2 // this.textBox2.Location = new system.drawing.point (24, 88); this.textBox2.name = "textbox2"; this.textBox2.tabindex = 1; this.TextBox2.text = "controlname"; // // button1 // this.button1.Location = new system.drawing.point (24, 200); this.button1.name = "button1"; this.button1.tabindex = 2; this.button1.text = " Query "; this.button1.click = new system.eventhandler (this.button1_click); // // radio Button1 // this.radioButton1.Checked = true; this.radioButton1.Location = new System.Drawing.Point (24, 136); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing. Size (136, 24); this.radioButton1.TabIndex = 3; this.radioButton1.TabStop = true; this.radioButton1.Text = "query form (form)"; this.radioButton1.CheckedChanged = new System.EventHandler ( THIS.RADIOBUTTON1_CHECKEDCHANGED; //// Radiobutton2 // this.radiobutton2.location = new system.drawing.point (24, 160); this.Radiobutton2.name = "

Radiobutton2 "; this.Radiobutton2.size = new system.drawing.size (192, 24); this.Radiobutton2.tabindex = 4; this.Radiobutton2.text =" Query the control on the form; this.Radiobutton2 .CheckedChanged = new System.EventHandler (this.radioButton2_CheckedChanged); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size (6, 14); this.ClientSize = new System.Drawing.Size (292, 273); this.controls.add (this.controls.add (this.radiobutton1); this.controls.add (this.button1); this.Controls.add (this.TextBox2); this.controls .Add (THIS.TEXTBOX1); this.name = "form1"; this.text = "Form and control query (event registration callback mode)"; this.ResumeLayout (false);}

#ndregion

Static void main () {Application.run (New Form1 ());}

Private void button1_click (Object sender, system.eventargs e) {

UcQueryObjectProp Queryop = New ucqueryObjectProp ();

Queryop.QueryType = querytype; queryop.formname = textBox1.text.trim (); queryop.controlname = textbox2.text.trim ();

UcformQueryExtender.callback = new callbackendhandler (Callback);

UcformQueryExtender.doQuery (queryop);

UcformQueryExtender.callback - = New CallBackeVentHandler (Callback);

}

Private Void Callback (Object P_Object) {String Str = ""

Str = "QueryType:" querytype.totring () "/ n / n";

IF (p_Object == null) {str = "Object is not found!";} else {if (querytype == ucquerytype.form) {str = "text is: /" " ((form) p_Object). Text "/";} else if (querytype == ucquerytype.control) {str = "text is: /" " ((Control) p_object) .Text " / ";}} messagebox.show STR);

}

Private void radiobutton1_checkedchanged (Object sender, system.eventargs e) {querytype = ucQuerytype.form;}

Private void radiobutton2_checkedchanged (Object sender, system.eventargs e) {querytype = ucQuerytype.control;}}} ============================ ================================== New form name is: form2.cs ====== ============================================================================================================================================================================================================= ====== Using system; using system.drawing; using system.collections; using system.componentmodel; using system.windows.form;

A summary description of Namespace queryform2 {///

/// form2. /// public class form2: system.windows.forms.form {private system.windows.forms.textbox textbox1; private system.windows.Forms.Button Button1; /// / / / must Designer variable. /// private system.componentmodel.container component = null; public form2 () {// // Windows Form Designer Support for // InitializationComponent ();

// // Todo: Add any constructor code // ucformQueryExtender.Query = new queryObjectEventHandler (ucformqueryextender_onquery);

}

Private void ucformQueryExtender_onquery (ucQueryObjectProp p_queryop) {ucformQueryExtender.findObject (this, p_queryop);}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method. /// private void initialization () {this.textbox1 = new system.windows.forms.textbox (); this.button1 = new system.windows.Forms.Button (); this.suspendlayout (); / / // textBox1 // this.textBox1.Location = new system.drawing.point (56, 64); this.textbox1.name = "textbox1"; this.textBox1.size = new system.drawing.size (208, 21 ); This.TextBox1.Tabindex = 0; this.textbox1.text = "query form 2 text box: textbox1"; // // button1 // this.button1.Location = new system.drawing.point (56 104); this.button1.name = "button1"; this.button1.size = new system.drawing.size (208, 23); this.button1.tabindex = 1; this.button1.text = "query window Body 2 button: button1 "; // // Form2 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (292, 273); this.controls .Add (this.button1); this.controls.add (this.textbox1); this.name = "form2"; this.text = "query form 2"; this.ResumeLayout (false);} #ENDREGION

}} ================================================ ============== The new form is named: form3.cs ========================== ============================================ Using system; use system.drawing; useing system.collections; using Summary description of system.componentmodel; use system.windows.forms; namespace queryform2 {///

/// form3 /// public class form3: system.windows.forms.form {private system.windows.forms.textbox textbox1; private system.windows.Forms.Button Button1; /// /// must be Designer variable. /// private system.componentmodel.container components = null;

Public Form3 () {// // Windows Form Designer Support for // InitializationComponent ();

// / / TODO: Add any constructor code after INITIALIZEComponent call //

UcformQueryExtender.Query = New queryiryObjectEventHandler (ucformQueryExtender_onquery);

}

Private void ucformQueryExtender_onquery (ucQueryObjectProp p_queryop) {ucformQueryExtender.findObject (this, p_queryop);}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method. /// private void initialization () {this.textbox1 = new system.windows.forms.textbox (); this.button1 = new system.windows.Forms.Button (); this.suspendlayout (); / / // textBox1 // this.TextBox1.Location = new system.drawing.point (56, 72); this.textbox1.name = "textbox1"; this.textbox1.size = new system.drawing.size (208, 21 ); This.textBox1.tabindex = 0; this.TextBox1.text = "The text box of the query form 3: textBox1"; // // button1 // this.button1.location = new system.drawing.point (56 112); this.button1.name = "button1"; this.button1.size = new system.drawing.size (208, 23); this.button1.tabindex = 1; this.button1.text = "query window Body 3 button: Button1 "; // // form3 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (292, 273); this.controls .Add (this.button1); this.Controls.add (this.textBox1); this.name = "form3"; this.text = "query form 3"; this.ResumeLayout (false);} #ENDREGON} } =============== =================================================

转载请注明原文地址:https://www.9cbs.com/read-66241.html

New Post(0)