Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Upgrading  »  Multiple Forms in VB.NET. Part 3 - Using Events in Multiple Forms
 »  Home  »  Windows Development  »  Win Forms  »  Multiple Forms in VB.NET. Part 3 - Using Events in Multiple Forms
Multiple Forms in VB.NET. Part 3 - Using Events in Multiple Forms
by Ged Mead | Published  09/03/2003 | Upgrading Win Forms | Rating:
Ged Mead

Ged Mead (XTab) has been around computers since the 1980's when the first affordable home computers came on the market. His journey from that very first Dragon 32 to the present has taken him through many different facets of the IT Industry. These include formal training as a Systems Analyst, employment in a mainframe software development environment, and a short time spent demonstrating rugged military IT systems in the days when it took two strong men to carry a 'mobile' system.

His most rewarding challenge was the creation of a financial management system for a large organisation.

Now based in an idyllic lochside location in the West of Scotland, he is currently involved in a range of development projects, whenever he can drag his gaze away from the stunning surrounding views, that is!

Ged is a Microsoft MVP, Senior Editor for DevCity.NET, vbCity Developer Community Leader and Admin, Helper of the Month competition winner and DevCity.NET newsletter Editor.

 

View all articles by Ged Mead...
Multiple Forms in VB.NET. Part 3 - Using Events in Multiple Forms

Article source code: multipleforms3.zip

As with the previous articles in this series, this one is also aimed at .NET Newbies and Upgraders. It tries to explain concepts as simply as possible, with the greatest use of plain English and minimum use of technical terms. The aim is to get the core ideas across as quickly as possible, so you can achieve the results you desire now; the technical details can follow in time. I know that many will not agree with this approach, but as a relative .NET Newbie myself, I know just how frustrating it can be trying to plough through a mass of technical detail in the early days when all you really want to do is, well, get started!

In this article, we are going to look at another topic on ways of dealing with multiple forms. As promised at the end of Part 2, we are first going to take a look at a way of passing data between multiple forms, but this time the user doesn't need to click a button to fire up the event.

Using Events

In our example project, we will have two forms. As the user enters data into a textbox in one form, this data will be copied to a label in the second form. Once again, although we're using a simple textbox for this example of the technique, the core idea can be extended for use in many much more sophisticated ways.

So, create two Windows Forms and name them EventsForm1 and EventsForm2. We'll be using EventsForm1 as the 'main' form, which is the one that has the label to receive the input data, and EventsForm2 as the form with the input textbox.

Here's the code we need to create an instance of EventsForm2. First, in EventsForm1, we declare a Form variable.

' This must be declared outside of any code blocks
    Dim WithEvents f2 As New EventsForm2()

Note that this has been declared WithEvents. This is important and if you leave it out, the main form will not be aware of what we are about to do in the second form.

And I thought that as we are being more dynamic in this Part, we would also instantiate and show the second form without user intervention. Or, in other words, we'll put it in the first form's load event and both forms will appear to the user at the same time.

Private Sub EventsForm1_Load(ByVal sender As Object_
    ByVal e As System.EventArgsHandles MyBase.Load

    If Not IsNothing(f2Then
        If Not f2.IsDisposed Then
            f2.WindowState