How to implement multi-form synchronization in VB. NET (1)

Source: Internet
Author: User

Assume that I have two forms, each of which has two textbox controls: txt1stdata and txt2nddata. How can I synchronize controls in these two forms? Two or ten forms are not important for the problem we are discussing. The problem is the same.

The first method is relatively simple. As a matter of fact, it is even easier than directly using a commission. I think sometimes it gives people the feeling of using a knife to kill chickens. First, I create a class that contains the attributes that I want to share with all forms in the application (see figure 1 ). For example, mydata and moredata have data that can be displayed in each form. I will return to this class soon.

Second, as I mentioned earlier, I used the same controls (txt1stdata and txt2nddata) to create two forms. You can refer to figure 2 layout. Both forms have identical data, and I will soon explain why.

498) This. style. width = 498; ''onmousewheel = ''javascript: Return big (this) ''Height = 165 alt = in VB. net. How to implement multi-form synchronization src = "/files/uploadimg/20061204/1635410 .jpg" width = 305 border = 0 twffan =" done ">

Next, I create a module named modgeneral and add the following code:

Friend DataStuff As DataClass

This line of code creates a friend variable for my new class dataclass so that you can fully access the Assembly. In this simple example, it refers to a complete application. Then I added the following code to the load event of form1:

DataStuff = New DataClass

Me.txt1stData.DataBindings.Add("Text", DataStuff, "MyData")

Me.txt2ndData.DataBindings.Add("Text", DataStuff, "MoreData")

Create a new dataclass instance in the first line. The following two lines of code bind the data to the Textbox Control. For this form, it is just a few operations!

Now, how do you synchronize them with the data on form2 and other forms? Add the following two rows to the form load event of Form 2:

Me.txt1stData.DataBindings.Add("Text",DataStuff, "MyData")

Me.txt2ndData.DataBindings.Add("Text",DataStuff, "Moredata")

This method ensures that almost any type of data on all forms is synchronized. You can simply bind the control to the same instance of a class.

Now let's look at another method. I created a new form named frmBase. At this time, I put a textbox (txtNextData) and label on it. I want every form of the application to share the textbox and label, and I want them to be synchronized with each other, so I rebuilt the project. By inheriting from the new frmBase, I created Form1 and Form2, so they inherited all the new controls. But how can I keep these controls synchronized? At this time, you must write a little code to achieve this effect. The code is reused in a single class by simply calling a function.

The code in Figure 3 shows the modGeneral module. Its first task is to define two variables: MyForms and localNextData. MyForms is a set that contains the list of forms that I want to synchronize. The localNextData variable stores all the data that I want to display in the form. Note that these variables can reside in a class rather than a module.

The AddForm process comes from modGeneral, which includes a form instance parameter and adds it to the MyForms set. I will use this set during UpdateControlsNextData to determine which forms to update. AddForm also calls UpdateControlsNextData to ensure that a new form is updated with correct data.

Other code in modGeneral is the NextData attribute. The set accessors of these attributes update localNextData and also call UpdateControlsNextData to synchronize all forms. In this case, all I need to do is set NextData when I want to change it. By calling UpdateControlsNextData, all forms will be updated. The third method is the custom link, which is the essence of the second method. I created it to get the flexibility to process more forms controls. For example, I only want to track and process some forms that contain controls that must be synchronized. This method also allows me to define controls to be synchronized and only process the forms of these controls.

I added another module (modgeneralv2) for this method, as shown in figure 4. This module includes a set (myformstoupdate) that contains all the forms that I want to synchronize. This module also has a new array (controlstoupdate) that provides a list of controls I want to synchronize. The array is defined as follows:

Private ControlsToUpdate() As String = {"txtCustomer", "txtAddress", "txtName"}

This module has a new improved version that replaces addform, called addformtoupdate. This method works in a similar way as addform, but now it only adds forms with one or more controls in the controlstoupdate array. Therefore, only those forms with specific controls are available in the update set. It allows me to call this function from every form. If I decide to add a specific control later, it will be automatically added to the form list. You only need to make minor changes to the form code.

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.