Visual C # implement data transfer between forms (2)

Source: Internet
Author: User

In the previous article, we once pointed out that there is a major drawback in the solution to the first case of data transmission between forms, that is, the number of parameters passed between forms is fixed, and the type is also fixed. This is because the system from the namespace is modified. windows. the form2 class constructor derived from Form class in forms, because the parameters and types in the constructor are fixed, and the main form transmits data to the form, it is implemented by parameters in the constructor, so it causes the above disadvantage. In fact, there is still a drawback in this method, that is, every time data is transferred between forms, a form must be constructed, and such data transmission is one-time. These disadvantages do not affect the transmission of a small amount of data between forms. However, if you want to transmit a large amount of data and transmit data to the form in real time through the main form, it is difficult to use this method.

Next we will introduce another method to transfer data from the main form to the form. This method can completely solve the two shortcomings above, in the main form, the program operates from the form flexibly just like the components added to the form.

Design Concept

This method implements two functions:

First, the main form can transmit data to the form in real time, which is displayed when the value of the trackbar in the main form is changed, A label component defined in the form displays the current value of the trace entry;

Second, you can request data from the form to the main form and obtain the data displayed by each component in the main form. The program shows that when you click the get data from form1 button in the form, the program can pass the content displayed by the two textbox components in the main form to the form, it is displayed by two textbox components in the form.

The first function is to consider the form as an instance of the main form and add it to the components in the form. It can be seen that the variables are defined in the form, the default definition type of components added from the form is private (private). to access these components, you must change them to public (common ); the second function is implemented by modifying the form2 constructor. The constructor is implemented by using instances of the form1 class (that is, the main form) to create and initialize instances of the form2 class (that is, from the form ). In this way, the main form is an instance of the form, so that you can request data to the main form. Of course, you need to define the types of components to be accessed from the default private (private) type to public (common ). The implementation method of the above two functions is complicated. I hope you can understand it with the specific implementation code below.

Step 2: Data Transfer Between forms

1. Create a project file for Visual C #. The project name is [data transfer method 02 for different forms in VC ].

2. set Visual Studio. switch to the form1.cs (Design) window, and drag the following components from the Windows Forms tab in the toolbox to the form1.cs (Design) form, perform the following operations:

· Two textbox components used to input data transmitted to the form2 form

· Two label components

· A trackbar component named trackbar1.

3. Switch the current window of Visual Studio. NET to the form1.cs window, that is, the code editing window of form1.cs. Replace the initializecomponent process generated by the system with the following code.



Private void initializecomponent ()
{
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 word :";
This. label2.location = new system. Drawing. Point (27, 83 );
This. label2.name = "label2 ";
This. label2.tabindex = 1;
This. label2.text = "prompt message :";
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. form#load );
(System. componentmodel. isupportinitialize) (This. trackbar1)
). Endinit ();
This. resumelayout (false );
}

4. because the data request from the form to the main form is the "text" attribute value of two textbox components, You need to modify the definition type of the two textbox components in the form1.cs file, modify the default value of "private" type to "public" type. The Definition Statement of the modified textbox component in form1.cs is as follows:



Public System. Windows. Forms. textbox textbox1;
Public System. Windows. Forms. textbox textbox2;

Add the following code after the above Code. The following Code creates a form2 class instance m_form, that is, from the form:



Private form2 m_form;

5. add the following code after the main function in form1.cs. The function of the following code is to change the value displayed by the label3 component in the form after modifying the trace value in the main form, in this way, data is transmitted to the form in real time in the main form:



Private void trackbar1_valuechanged (Object sender, system. eventargs E)
{
M_form.label3. Text = trackbar1.value. tostring ();
}

6. after the above Code is added and the following code is added at the back of the Code, the following code is used as a constructor of the form2 class, create and initialize instances of the form2 class through instances of the form1 class. Adding the form2 class to the project file and modifying the constructor of the form2 class will be completed in steps 7th to 11 in this section.



Private void form1_load (Object sender, system. eventargs E)
{
M_form = new form2 (this );
// Create and initialize the slave form through the main form
M_form.show ();
// Display the form
}

7. Select project> Add windows form. The add new project> VC # data transfer method 01 dialog box is displayed. In this dialog box, enter form2 In the Name (n): text box, and click open, in VC #, a new form named form2 is added to the data transfer method 01 project of different forms ].

8. set Visual Studio. switch to the form2.cs (Design) window, and drag the following components to the form2.cs (Design) form from the Windows Forms tab in the toolbox, perform the following operations:

· Two textbox components are used to display the data requested from the main form.

· Two label components.

· A button component named button1.

9. Switch the current window of Visual Studio. NET to the form2.cs window, that is, the code editing window of form2.cs. Replace the initializecomponent process generated by the system with the following code.



{
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 = "prompt message :";
This. label1.location = new system. Drawing. Point (38, 45 );
This. label1.name = "label1 ";
This. label1.tabindex = 4;
This. label1.text = "Welcome word :";
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 = "getting data from form1 ";
This. button1.click + = new system. eventhandler (this. button#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. because the value of the trace entries in the main form is displayed through the label component in the form, therefore, you must change the "private" type defined when creating the label3 component in the form2.cs file to the "public" type. The code for creating the label3 component after modification is:



Public System. Windows. Forms. Label label3;

Because form2 class instances are initialized through form1 class instances, add the following code after creating the label3 component. The following Code creates a form1 class instance, the role is to initialize the form2 class instance (that is, from the form ):



Private form1 mf_form;

11. Modify the form2 class constructor. The specific operation is as follows: Replace the default constructor in form2.cs with the following code:



Public form2 (form1 myform)
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();
This. mf_form = myform;
//
// Todo: add Any constructor code after initializecomponent calls
//
}

12. add the following code after the main function in form2.cs. The following code is used to request data from the main form and obtain data from the main form, of course, the data type must be changed to "public.



Private void button#click (Object sender, system. eventargs E)
{
Textbox1.text = This. mf_form.textbox1.text;
Textbox2.text = This. mf_form.textbox2.text;
}

13. At this point, after the above steps are completed successfully and all are saved, Visual C # Completes the processing of the second case of data transmission between forms. Click the shortcut key F5 to run the program. When you adjust the value of the trace entry in the form1 form, we will find that the value displayed by the label3 component in the form2 form also changes; after entering data in the two textbox components of form1 form, click the get data from form2 button in form2 form, the program can successfully obtain data from form1, it is displayed through the textbox component corresponding to form2. Figure 01 shows the interface after the program is run:

Figure 01: [data transfer methods for different forms in VC #02] running interface of the program

Summary

This section describes how to implement data transfer between forms is much more difficult than the method described in the first article. The difficulty lies in how to modify the constructor. For data to be accessed from other forms, it must be set to "public"; otherwise, data transmission between forms cannot be implemented.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.