Data delivery between the Forms in Visual C # (1)

xiaoxiao2021-03-06  65

A slightly complex program generally has two or more forms. Sometimes data is not only transferred in the same form, but also transfers between the form, which is transferred to each other between the main form and the data between the data from the form. From this article, we will list the four situations of data transfer between different forms, and the specific ways of implementing these four situations with Visual C #. Let's first introduce the first case in the delivery data between Visual C # to implement the data - from the main window to the string from the form. After reading this article, you still try to use this method to transfer numerical values ​​between the form. This article design, debug, running software environment: Windows2000 server version Visual Studio.Net official version, .NET Framework SDK version number 3705 implementation steps: 1. Start Visual Studio .NET2. Select menu [File] | [New] | [Project], pop up the [New Project] dialog 3. Set [Type] set to [Visual C # item] 4. Set [Template] to [Console Application] 5. In the [Name] text box Enter [VC # Different Form Data Transfer Method 01] 6. Enter [E: /VS.NET Project] in the [Location] text box, then click the [OK] button, so different form data in the VC # Transfer Method 01 The project is created 7. Switch to the current window of Visual Studio to the [Form1.cs (Design)] window, and drag into the following components from the [Windows Forms] tab in [Toolbox] In the [Form1.cs (Design)] form, and perform the appropriate operations: • Two TextBox components to enter data transmitted to the FORM2 form, two Label components, a Button component, name is Button1, and After dragging into the [Form1.cs (Design)], double-click it, then Visual Stuido .NET produces the processing code corresponding to its Click event. 8. Switch the current window of Visual Studio .NET to the [Form1.cs] window, namely: form1.cs code editing window. And use the following code to replace the INITIALIZECMPONENT process generated by the alternate system.

Private vidinitiRizeComponent ()

{

This.button1 = new system.windows.Forms.Button ();

This.TextBox1 = new system.windows.Forms.TextBox ();

This.TextBox2 = new system.windows.Forms.TextBox ();

THIS.LABEL1 = New System.windows.Forms.label ();

THIS.LABEL2 = New System.windows.Forms.label ();

THIS.SUSPENDLAYOUT ();

This.Button1.Location = new system.drawing.point (103, 149);

This.button1.name = "button1";

This.button1.size = new system.drawing.size (75, 36);

this.button1.tabindex = 0;

This.button1.text = "Send";

This.Button1.click = new system.eventhandler (this.button1_click);

This.TextBox1.Location = new system.drawing.point (93, 56); this.TextBox1.name = "textbox1";

This.TextBox1.size = new system.drawing.size (122, 21);

this.TextBox1.tabindex = 1;

THIS.TEXTBOX1.TEXT = "";

THIS.TEXTBOX2.LOCATION = New System.drawing.Point (93, 99);

THIS.TEXTBOX2.NAME = "TextBox2";

This.TextBox2.size = new system.drawing.size (123, 21);

This.TextBox2.tabindex = 2;

THIS.TEXTBOX2.TEXT = "";

THIS.Label1.Location = New System.drawing.Point (29, 57);

THIS.LABEL1.NAME = "label1";

THIS.Label1.tabindex = 3;

THIS.Label1.Text = "Name";

THIS.LABEL2.LOCATION = New System.drawing.Point (16, 100);

THIS.LABEL2.NAME = "label2";

THIS.LABEL2.TABINDEX = 4;

THIS.Label2.text = "Item";

THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);

THIS.CLIENTSIZE = New System.drawing.size (263, 223);

this.controls.addrange (new system.windows.forms.control] {

THIS.TEXTBOX2,

this.TextBox1,

this.button1,

THIS.LABEL2,

THIS.LABEL1});

THIS.NAME = "Form1";

THIS.TEXT = "Form1";

This.Load = New System.EventHandler (this.form1_load);

This.ResumeLayout (false);

}

At this time, the design interface of the main form of the project of different form data transfer method 01 in VC # is completed, as shown in Figure 1:

Figure 1: VC # Different Form Data Transfer Method 01 Project Main Form Design Interface

9. Switch the current window of Visual Studio to the code editing window of Form1.cs and replace the processing code corresponding to the Click event of the Button1 component in Form1.cs with the following code.

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Form2 myform = new form2 (TextBox1.text, TextBox2.text);

myForm.show ();

}

10. Select menu [Project] | [Add Windows Form], pop up [Add New Items Data Password 01] dialog box. After entering [F ORM2] in this dialog box, enter [F ORM2] in the text box, click the [Open] button, add a new window in different form data transfer method 01 items in the VC #. The body, the name is [Form2]. 11. Switch the current window of Visual Studio to the [Form2] window and drag into the following components from the [Windows Forms] tab in [Toolbox] to [Form2.cs (Design) 】 In the form and perform the corresponding operation:

· Two TEXTBOX components to display string data from FORM1

· Two label components

· A Button component, named Button1, and after dragging into the [Form2.cs (Design)] form, double-click it, the Visual Stuido .NET produces the processing code corresponding to its Click event.

12. Switch the current window of Visual Studio to the code editing window of Form2.cs, and add the following code to the rear of the definition form2 code, the following code is to define two string variables to receive from Form1. String data:

PRIVATE STRING STR1, STR2;

13. Use the code corresponding to the form generated by the following code to replace the code:

Public Form2 (string senddata01, string senddata02)

{

//

// Windows Form Designer Support

//

InitializationComponent ();

Str1 = senddata01;

Str2 = senddata02;

//

// Todo: Add any constructor code after INITIALIZECOMPONENT call

//

}

14. Replace the InitializationComponent process generated by the Visual Studio .NET system in Form2.cs with the following code:

Private vidinitiRizeComponent ()

{

This.TextBox2 = new system.windows.Forms.TextBox ();

This.TextBox1 = new system.windows.Forms.TextBox ();

This.button1 = new system.windows.Forms.Button ();

THIS.LABEL2 = New System.windows.Forms.label ();

THIS.LABEL1 = New System.windows.Forms.label ();

THIS.SUSPENDLAYOUT ();

This.TextBox2.Location = new system.drawing.point (109, 93);

THIS.TEXTBOX2.NAME = "TextBox2";

This.TextBox2.size = new system.drawing.size (123, 21);

THIS.TEXTBOX2.TABINDEX = 7;

THIS.TEXTBOX2.TEXT = "";

This.TextBox1.Location = new system.drawing.point (109, 50);

THIS.TEXTBOX1.NAME = "textbox1"; this.textbox1.size = new system.drawing.size (122, 21);

This.TextBox1.tabindex = 6;

THIS.TEXTBOX1.TEXT = "";

This.button1.location = new system.drawing.point (75, 143);

This.button1.name = "button1";

This.button1.size = new system.drawing.size (119, 43);

This.button1.tabindex = 5;

This.button1.text = "Receive Data";

This.Button1.click = new system.eventhandler (this.button1_click);

This.Label2.Location = new system.drawing.point (32, 94);

THIS.LABEL2.NAME = "label2";

THIS.LABEL2.TABINDEX = 9;

THIS.Label2.text = "Item";

THIS.Label1.Location = new system.drawing.point (45, 51);

THIS.LABEL1.NAME = "label1";

THIS.Label1.tabindex = 8;

THIS.Label1.Text = "Name";

THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);

THIS.CLIENTSIZE = New System.drawing.size (265, 228);

this.controls.addrange (new system.windows.forms.control] {

THIS.TEXTBOX2,

this.TextBox1,

this.button1,

THIS.LABEL2,

THIS.LABEL1});

THIS.NAME = "Form2";

THIS.TEXT = "Form2";

This.ResumeLayout (false);

}

15. Replace the processing code corresponding to the Button1 component Click event in Form2.cs with the following code, the following code is to display the string data received from the Form1 form in the same manner:

Private void Button1_Click (Object Sender, System.Eventargs E)

{

TextBox1.text = str1;

TextBox2.text = str2;

}

16. At this point, the above steps are completed correctly, and all the work of different form data transfer method 01 items in VC # is completed. Figure 02 is an operation interface of different form data transfer method 01 programs in VC #. The string shown in Form2 is transmitted from the main form.

Figure 02: Run interface of different form data transfer method 01 program in VC #

to sum up

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

New Post(0)