C # How to achieve access between two WinForm

xiaoxiao2021-03-06  90

In VB, we can usually pass through Form1! Text1.text, etc., but we find that in C # we find that this is different. Through my research, I finally came up with two WinForm. Measures between access, that is, through an intermediate class, the code is controlled, the code is as follows:

// ==============================

Form1.cs

// ==============================

Using system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

namespace WA_Test1 {///

/// OF: Davi /// // delegate public delegate string chktext (int myInt);

public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.Button button1; public System.Windows.Forms.TextBox textBox1; // public Form1 frm1 = new Form1 (); ///

/ // The required designer variable. /// private system.componentmodel.container components = null;

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

/// tDO: Add any constructor code after the initializationComponent call //}

///

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

#Region Windows Form Designer Generated Code ///

/// Designer Supports the required method - Do not use the code editor to modify the // / this method. /// private void initializecomponent () {this.button1 = new system.windows.Forms.Button (); this.textBox1 = new system.windows.Forms.TextBox (); this.suspendlayout (); / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /TI ); this.button1.tabindex = 0; this.button1.text = "button1"; this.button1.click = new system.eventhandler (this.button1_click); //// textbox1 // this.textbox1.location = New system.drawing.point (16, 56); this.TextBox1.name = "textbox1"; this.textBox1.size = new system.drawing.size (88, 21); this.TextBox1.tabindex = 1; this. TextBox1.text = "textbox1"; // // Form1 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (304, 117); this.Controls .Addrange (new system.windows.forms.control [] {this.textBox1, this.button1}); this.maximizebox = false; this.name = "form1"; this.text = "form1"; this.ResumeLayout False);} # endregion

///

/// The primary entry point of the application. /// [stathread] static void main () {form3 frm3 = new form3 (); application.run (frm3.getform1 ());}

Private void button1_click (Object sender, system.eventargs e) {form3 frm3 = new form3 (); (frm3.getform2 ()). show (); // frm3.inputbox ("title", "please enter some value", "20"); // MessageBox.show (frm3.inputbox (). Tostring ());}

Private void button2_click (object sender, system.eventargs e) {//}

}} // ===============================================52.cs // == ============================================== ingness; use system.drawing; using System.ComponentModel; using System.Windows.Forms; namespace WA_Test1 {///

/// OF: Davi /// public class Form2: System.Windows.Forms.Form {private System.Windows .Forms.textbox textBox1; private system.windows.Forms.Button Button1; /// // / / The necessary designer variables. /// private system.componentmodel.container components = null;

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

/// tDO: Add any constructor code after the initializationComponent call //}

///

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

#Region Windows Form Designer Generated 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 (16, 40); this.textbox1.name = "textbox1"; this.textbox1.size = new system.drawing.size (152, 21 this.TextBox1.tabindex = 0; this.TextBox1.text = "textbox1"; // // button1 // this.button1.location = new system.drawing.point (56, 72); this.button1.name = "Button1"; this.button1.size = new system.drawing.size (168, 24); this.button1.tabindex = 1; this.button1.text = "Change Form1 TextBox1"; this.button1.click = New System.EventHandler (this.button1_click); // // Form2 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (272, 117); this.controls.addrange (new system.windows.forms.control [] {this.button1, this.textbox1}); this.name = "form2"; this.text = "form2"; this.ResumeLayout (false); } #endregion

Private void button1_click (object sender, system.eventargs e) {form3 fm3 = new form3 (); (frm3.getform1 ()). TextBox1.text = this.TextBox1.text;}}} // ====== ========================================= ============================================================================================================================================================================= ==================================================== ingry system.drawing ; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace WA_Test1 {///

/// OF: Davi /// public class Form3 {/// /// The required designer variable. /// Private static form1 frm1; private static form2 frm2; public form3 () {/// /// Clean all the resources being used. /// } public void createform1 () {frm1 = new form1 ()} public void createform2 () {frm2 = new form2 ()} public form1 getform1 () {if (frm1 == null) { CreateForm1 ();} returnif1;} public form2 getform2 () {if (frm2 == null) {createform2 ();} Return FRM2;}} /// /// input box form /// < / summary>} * Welcome everyone to study with me.

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

New Post(0)