Visual C # Implement Forms Data Pass (2)

xiaoxiao2021-03-06  64

In the previous article, we have pointed out that there is a major disadvantage in the solution of the first case of data delivery between the form, that is, the number of parameters delivered between the form is fixed, and the type is also fixed. This is because the configuration function of the Form2 class obtained from the Form class derived from the namespace system.windows.form is modified. Since the parameters and types in the constructor are fixed, the main window is from the window The body delivery data is implemented by the parameters in the constructor, so that the disadvantage is caused. In fact, there is still a disadvantage in this method, that is, data transfer between each form must build a form, and this data transfer is one-time. These disadvantages generally pass a small amount of data between the forms, generally do not have much impact, but if you want to pass a lot of data, and to deliver data from the form through the main form, use this method to use this method. Here is another method of implementing the data from the main window to the source from the form, which can completely solve the above two disadvantages, and the program is like the components added in the main form, flexible Operate from the form.

Design ideas

This method implements two functions:

First, the main form can transmit data from the form in real time, representing the value of the trace bar in the main form, displaying the current value of the tracking strip from a LABEL component defined in the form. ;

Second, the data request can be proposed from the form to the main form, and the data displayed in each component in the main form can be obtained. The program is manifested, and the program can pass the content displayed from the two TEXTBOX components in the main form from the form from the form, and passed from the form in the form. The TextBox component is displayed separately.

The implementation of the first function is to see an instance from the form as a main form, add to components from the form, it can be seen from the one variable defined from the form, since the form The component default definition type of the added component is private (private), so you must change these components, you must change to public (shared); and the implementation of the second function is to modify the constructor of FORM2, constructor The implementation function is to create and initialize an instance of the FORM2 class (ie, from the form) by the instance of the Form1 class (ie, the main form). Thus, for the main form, the main form is an example, so that data requests can be proposed to the main form, of course, the component definition type required to access from the default private type is modified from the default private type. For public (shared). The first method of the above two functions, the second method is more complicated, and it is desirable to understand the specific implementation of the following.

Data delivery situation between the second form Implementation steps

1. First create a project file for Visual C #, the project name is [Different Form Data Transfer Method 02] in VC #.

2. Switch the current window of Visual Studio .Net to the [Form1.cs] window and drag into the following components from the [Windows Forms] tab in [Toolbox] to [Form1.cs (Design) 】 In the form and perform the corresponding operation:

· Two TextBox components to enter data transmitted to the FORM2 form

· Two label components

· A TrackBar component with name TRACKBAR1.

3. Switch the current window of Visual Studio .Net to the [Form1.cs] window, namely the code editing window of Form1.cs. And use the following code to replace the INITIALIZECMPONENT process generated by the alternate system. Private vidinitiRizeComponent ()

{

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

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

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

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

THIS.TRACKBAR1 = New System.windows.Forms.trackbar ();

(System.comPonentModel.isupportInitialize) (this.trackbar1)

.Begininit ();

THIS.SUSPENDLAYOUT ();

This.Label1.Location = new system.drawing.point (27, 41);

THIS.LABEL1.NAME = "label1";

this.label1.tabindex = 0;

THIS.Label1.Text = "Welcome words:";

THIS.Label2.Location = new system.drawing.point (27, 83);

THIS.LABEL2.NAME = "label2";

THIS.Label2.tabindex = 1;

THIS.Label2.Text = "Tip information:";

This.TextBox1.Location = new system.drawing.point (108, 38);

THIS.TEXTBOX1.NAME = "TextBox1";

THIS.TEXTBOX1.TABINDEX = 2;

THIS.TEXTBOX1.TEXT = "";

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

THIS.TEXTBOX2.NAME = "TextBox2";

this.TextBox2.tabindex = 3;

THIS.TEXTBOX2.TEXT = "";

THIS.TRACKBAR1.LARGECHANGE = 1;

this.trackbar1.location = new system.drawing.point (12, 182);

THIS.TRACKBAR1.MAXIMUM = 100;

THIS.TRACKBAR1.NAME = "trackbar1";

This.trackbar1.size = new system.drawing.size (272, 42);

this.trackbar1.tabindex = 1;

This.trackbar1.ValueChanged = New System.EventHandler

this.trackbar1_valuechanged;

This.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (292, 273);

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

THIS.TRACKBAR1,

THIS.TEXTBOX2,

this.TextBox1,

THIS.LABEL2,

THIS.LABEL1});

THIS.MAXIMIZEBOX = FALSE;

THIS.MINIMIZEBOX = FALSE;

THIS.NAME = "Form1";

THIS.TEXT = "Form1";

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

(System.comPonentModel.isupportInitialize) (this.trackbar1)

.Ndinit ();

This.ResumeLayout (false);

}

4. Since the data request raised from the form is the "text" attribute value of the two TextBox components, the definition type of these two TextBox components in the Form1.cs file is to be modified, default is defined as "Private" "The type modified to" public "type, the definition statement of these two TEXTBOX components in Form1.cs is as follows:

Public system.windows.Forms.TextBox textBox1;

Public system.windows.Forms.TextBox textBox2;

After the above code, add the following code, the following code is an instance m_form that creates a FORM2 class, that is, from the form:

PRIVATE FORM2 M_FORM;

5. After the main function in Form1.cs, add the following code, the following code is to implement the display value of the Label3 component from the form changes when modifying the value of the trace bar in the main form, can change, This ways to pass the main form in real time to from the form:

Private Void TrackBar1_ValueChanged (Object Sender, System.EventArgs E)

{

m_form.label3 .text = trackbar1.value.tostring ();

}

6. Add the above code and add the following code, then add the following code, the following code features the use of the Form2 class constructor and create and initialize an instance of the Form2 class through the form of the Form1 class. Add the Form2 class in the project file and modify the construction function of the Form2 class will be completed in Section 7 and 11 of this section.

Private Void Form1_Load (Object Sender, System.EventArgs E)

{

M_Form = New Form2 (this);

// Create, initialize from the form through the main form

m_form.show ();

// Show from the form

}

7. Select the menu [Project] | [Add Windows Form], pop up the [Different Form Data Transfer Method 01] dialog box in the new item -VC #. After entering [FORM2] in this dialog box, enter [Form2] in the text box, click the [Open] button, add a new form in the VC # Different Form Data Password 01 items. , Name is [Form2]. 8. Switch the current window of Visual Studio .Net 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 data obtained to the main form request.

· Two label components.

· A Button component, name is Button1.

9. Switch the current window of Visual Studio .NET to the [Form2.cs] window, namely the code editing window of Form2.cs. And use the following code to replace the INITIALIZECMPONENT process generated by the alternate system.

{

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

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

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

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

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

This.label3 = new system.windows.Forms.label ();

THIS.SUSPENDLAYOUT ();

this.TextBox1.Location = new system.drawing.point (95, 42);

THIS.TEXTBOX1.NAME = "TextBox1";

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

THIS.TEXTBOX1.TABINDEX = 2;

THIS.TEXTBOX1.TEXT = "";

This.TextBox2.Location = new system.drawing.point (94, 80);

THIS.TEXTBOX2.NAME = "TextBox2";

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

this.TextBox2.tabindex = 3;

THIS.TEXTBOX2.TEXT = "";

THIS.Label2.Location = new system.drawing.point (27, 83);

THIS.LABEL2.NAME = "label2";

THIS.Label2.tabindex = 5;

THIS.Label2.Text = "Tip information:";

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

THIS.LABEL1.NAME = "label1";

THIS.Label1.tabindex = 4; this.label1.text = "Welcome words:";

This.Button1.Location = new system.drawing.point (80, 136);

This.button1.name = "button1";

This.button1.size = new system.drawing.size (135, 53);

THIS.BUTTON1.TABINDEX = 6;

This.button1.text = "Get data from Form1";

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

This.label3.Location = new system.drawing.point (102, 210);

THIS.LABEL3.NAME = "label3";

THIS.Label3.tabindex = 7;

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

THIS.CLIENTSIZE = New System.drawing.size (292, 273);

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

THIS.Label3,

this.button1,

THIS.TEXTBOX2,

this.TextBox1,

THIS.LABEL2,

THIS.LABEL1});

THIS.MAXIMIZEBOX = FALSE;

THIS.MINIMIZEBOX = FALSE;

THIS.NAME = "Form2";

THIS.TEXT = "Form2";

This.ResumeLayout (false);

}

10. Since the main form is displayed by the value of the tracking strip by the Label component from the form, the "private" type defined when the Label3 component is created in the FORM2.CS file must be modified to "public" type. , The code to create the Label3 component after modification is:

Public system.windows.forms.label label3;

Since the instance of the Form2 class is initialized by an instance of the Form1 class, then add the following code after creating the Label3 component, the following code is an instance of a FORM1 class, which is an instance of the FORM2 class (ie, from the form):

PRIVATE FORM1 MF_FORM;

11. Modify the constructor of the Form2 class, the specific operations are as follows, replace the default constructor in FORM2.CS with the following code:

Public Form2 (Form1 MyForm)

{

//

// Windows Form Designer Support

//

InitializationComponent ();

THIS.MF_FORM = MyForm;

//

// Todo: Add any constructor code after INITIALIZECOMPONENT call

//

}

12. After the main function in form2.cs, add the following code, the function of the following code is to implement data requests to the main form, and obtain data of the main form, of course, the type of data must be modified to "public" type. .

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

{

TextBox1.text = this.mf_form.TextBox1.text;

TextBox2.text = this.mf_form.textbox2.text;

}

13. At this point, the above steps are successfully completed, and after all saved, the method of processing the second case between the Visual C # implements the second case between the formation of data is completed. At this point, click the shortcut [F5] to run the program, when adjusting the value of the FORM1 form, we will find the value of the Label3 component displayed in the FORM2 form; when in the Form1 form After entering the data in the TextBox component, click the [Obtain the Data] button in the Form2 form, then the program can successfully get data from Form1, and display it through the corresponding TEXTBOX components in Form2. Figure 01 is the interface after the program runs:

Figure 01: Different Form Data Transfer Method 02】 Running Interface in VC #

summary

This section introduces this method of data transmission between the forms more difficult than the method described in this section, and the difficult point is how to modify the constructor. For those to access from other forms, you must set the modified "public", otherwise the formal data transfer cannot be implemented.

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

New Post(0)