Control query between different forms - Method 1 (Static Form An array)

xiaoxiao2021-03-06  23

Control query between different forms - mode one ====================================== ======================== New creation is named: ucformcollector.cs ================= ====================================================== @ystem; use System.collections; using system.windows.forms;

Namespace queryform1 {

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

///

/// Form collector (using an array collection method) /// public sealed class ucformcollector {private static arraylist m_formlist = new arraylist ();

// Form an array collection public static arraylist formlist {get {return m_formlist;}}

///

/// Add Form /// /// Current Form public static void addform {if (m_formlist. Contains (p_form.handle) {return;}

m_formlist.add (p_form.handle); attachevent (p_form);}

///

/// Remove form /// /// Current Form public static void removeform (form p_form) {m_formlist.remove (p_form.handle);

///

/// query form /// /// Form name /// Return to the form Otherwise, returning null public static form queryform (String p_formname) {ienumerator myenumerator = m_formlist.getenumerator (); form currform = null; form formForm = null; form finder

While (myenumerator.movenext ()) {

Currform = (form) form.fromhandle ((system.intptr) Myenumerator.current;

IF (currform.name.tolower () == p_formname.tolower ()) {Findform = Currform;}}

Return FindForm;

}

///

/// Query on the control /// /// Form name /// Forms on the form /// Return to the found control, otherwise return null public static control queryControl (string p_formname, string p_controlname) {form currform = queryform (p_formname) Control FindControl = NULL;

IF (Currform! = null) {for (Control Ctr in Currform.Controls) {if (ctr.name.tolower () == p_controlname.tolower ()) {FINDCONTROL = Ctr;}}}

Return FindControl;

}

///

/// Added Form Event Delegate /// /// private static void attachevent (form p_currform) {p_currform. Closed = New EventHandler (OnFormCloseDremoveItem);

// Release after the form is closed, the form of the form of the form Private Static Void ONFORMCLOSEDREMOVEITEM (Object Sender, System.EventArgs E) {Removeform ((Form) Sender);}

}} ================================================ ============== New form name is: form1.cs =================================================== ============================================ Using system; use system.drawing; useing system.collections; using System.componentmodel; Using System.Windows.Forms; Using System.Threading; Namespace Queryform1 {///

/// Form1 summary description. /// 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;

//inquiry mode

Ucquerytype querytype = new ucquerytype ();

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

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

UcFormCollector.Addform (this);

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 (form collection method)"; this.ResumeLayout (false);}

#ndregion

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

Private void button1_click (object sender, system.eventargs e) {string formName = textbox1.text.trim (); string controlname = textBox2.text.trim (); string str = ""

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

IF (querytype == ucquerytype.form) {form currform = ucformollector.queryform (Formname);

IF (currform == null) {str = "Object is not all!";} else {str = "text is: /" currform.text "/" ";}} else f (querytype == Ucquerytype.control) {Control currcontrol = ucformcollector.queryControl (FormName, ControlName);

IF (currcontrol == null) {str = "Object is not found!";} else {str = "text is: /" currcontrol.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;}

}} ================================================ ============== The new form is named: form2.cs ====================================================== ============================================ Using system; use system.drawing; useing system.collections; using System.componentmodel; using system.windows.forms;

Summary description of Namespace queryform1 {///

/// 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 components = null;

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

// // Todo: Add any constructor code // ucformcollector.addform (this); UcFormCollector.addform (this); // Duplicate invalid

}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = null) {Components.dispose ();}} Base.Dispose (DISPOSION);} #Region Windows Form The code generated by the designer /// /// 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 (); / / ///////////////////////////Wwndrawing.point (56, 64); this.textBox1.name = "textbox1"; this.textbox1.size = new system.drawing.size (192, 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 (192, 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 for system.componentmodel; use system.windows.forms; namespace queryform1 {///

/// form3 summary. /// 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 //

UcFormCollector.Addform (this);

}

///

/// 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 (); / / ///////////////////////////Wwndrawing.point (56, 72); this.textBox1.name = "textbox1"; this.textbox1.size = new system.drawing.size (200, 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 (200, 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-66243.html

New Post(0)