Visual Basic. Synchronization of multiple Windows forms in net

Source: Internet
Author: User
Tags define bind contains implement variables
Visual|window This is an interesting question. I'm sure some smart programmers will advise me to use delegates. Before this happens, let's explore some of the solutions to this problem first.
Suppose I have two forms, each with two TextBox controls: Txt1stdata and Txt2nddata. How can I keep the controls in these two forms synchronized? For the problem we are discussing, there are two or 10 forms that are not important and the problem is the same.
The first approach is relatively simple. In fact, it's even simpler than our direct use of delegates, and I think the Commission sometimes gives people the feeling of killing a chicken with a sledgehammer. First, I build a class that contains properties that I want to share with all forms in the application (see Figure 1). For example, MyData and Moredata have the data that each form can display. I will come back to this class soon.
Second, as I mentioned earlier, I built two forms with the same controls (Txt1stdata and Txt2nddata). You can refer to the layout of Figure 2. All two forms have exactly the same data, and I will explain quickly why.


Figure 2 form Layouts

Next, I build a module called Modgeneral and add the following line of code:

Friend Datastuff as DataClass
This line of code creates a friend variable for my new class dataclass so that you have full access to the assembly, and for this simple example, the complete application. Then I added the following code to the Form1 Load event:
Datastuff = New DataClass Me.txt1stData.DataBindings.Add ("Text", Datastuff, "MyData") Me.txt2ndData.DataBindings.Add ( "Text", Datastuff, "Moredata")
The first line creates a DataClass new instance. The following two lines of code bind data to a TextBox control. For this form, that's it!
Now, how do you keep them synchronized with the data on Form2 and other forms? Add the following two lines to the form 2 Load event:
ME.TXT1STDATA.DATABINDINGS.ADD ("text", Datastuff, "MyData") Me.txt2ndData.DataBindings.Add ("text", Datastuff, " Moredata ")
This approach makes it easy to ensure that almost any type of data on a form is synchronized. You can simply bind the control to the same instance of a class, and that's fine.
Now look at another method. I created a new form named Frmbase. Then I put a TextBox (Txtnextdata) and label on it. I want every form in the application to share this textbox and label, and I want them to keep in sync with each other, so I rebuilt the project. By inheriting from the new frmbase, I created Form1 and Form2, so they inherit all the new controls. But how can I keep these controls synchronized? You must write a little code to achieve this effect, which is reused in a single class by simply calling a function.
The code in Figure 3 shows this called the Modgeneral module. Its first task is to define two variables: Myforms and Localnextdata. Myforms is a collection that contains a list of forms that I want to synchronize. The Localnextdata variable will store all the data I want to display in the form. Note that these variables can reside in a class rather than in a module.
The AddForm process comes from modgeneral, takes a form instance parameter, and joins it in the Myforms collection. I will use this collection in the Updatecontrolsnextdata process to determine which forms to update. AddForm also calls Updatecontrolsnextdata to make sure that a new form is updated with the correct data.
The other code in the Modgeneral is the Nextdata property. The set accessors for these properties update Localnextdata and also call Updatecontrolsnextdata to synchronize all forms. All I need to do is set nextdata when I want to change it, and all the forms will be updated by calling Updatecontrolsnextdata.
The third method is to customize the link, which is the essence of the second method. I created it to get the flexibility to handle more forms controls. For example, I just want to track and work with some forms that contain controls that must be synchronized. This method also lets me define the controls that I want to synchronize and only work with the forms of those controls.
I added another module for this approach (MODGENERALV2), as shown in Figure 4. The module includes a collection (Myformstoupdate) that contains all the forms I want to synchronize. The module also has a new array (controlstoupdate), which 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 alternative addform version, called Addformtoupdate. This method works like AddForm, but now it only adds a form with controls in one or more controlstoupdate arrays, so only the forms that contain the specific controls are in the Update collection. It allows me to call the function from each form. If I decide to add a particular control later, it will be automatically added to the form list. I just need to make small changes to the form code can be implemented.
This module also contains the updatecontrolsonallforms process, which performs the update. Instead of an application-level variable used in the previous method, I now use the concept of the main form. So I can copy the value of that form to all the other forms in the collection. Updatecontrolsonallforms is actually a set of simple for ... Nexts loops through all the controls on a form, finds the control that needs to be updated, and updates them.
To implement this feature in my form, I added this line of code to the form's Load event:
Addformtoupdate (Me)
An alternative method that I can add to the constructor. This line of code adds the current form instance to the collection for updating.
Now let's examine a single event procedure:
Private Sub Txt_leave (ByVal sender as Object, _ ByVal e as System.EventArgs) Handles Txtaddress.leave, _ Txtcustomer.leave , Txtname.leave updatecontrolsonallforms (Me) End Sub
This code bundles the Leave events of all three controls (Txtaddress, Txtcustomer, and txtname) that I want to synchronize onto an event handle. At this point I can add a line of code to invoke updatecontrolsonallforms. Me is passed to the procedure call, causing other forms to sync with the form.
Now I have three versions of code that can synchronize controls on a form, so I can make a choice. I might have used a custom event to define an event in DataClass and have each form subscribe to it. Then when this event is triggered, the forms can get new data from each event handle and set the appropriate control. However, the amount of code required to do so is not less than the first method to bind the control to a class. I can build a single process for implementing updates and put that process into a module. I need to pass the form instance to this procedure to implement the update. I can trigger this process with an event handle in the class. This process looks like this:
Sub Updatecontrols (ByVal thisform as Frmbase) with thisform. Txtnextdata.text = Localnextdata and End Sub
The thisform parameter is defined as a frmbase type so that it can access IntelliSense and obtain custom properties for the form. Simply writing it as a form will not be able to display the properties in Frmbase and their derived forms.
Another option is to use a delegate. Of course, delegates can let me redirect delegate calls to methods on each form. If I use a multicast mechanism, then I can have each form handle the event and update the corresponding control. Using a delegate to build such a function sounds simple, but it's more cumbersome and less practical for me. In addition, with the third method for ... Next loop Nesting, this code is not difficult to understand. After all, the most expensive part of an application is still its maintenance.

Send your questions and comments on Ken to basics@microsoft.com



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.