1. Home
  2. Computing & Technology
  3. Visual Basic

A Two Form Example Program

A lot of interesting code techniques in a short example.

By Dan Mabbutt, About.com

Jul 18 2008

Programming techniques illustrated in this article:

  • Raising a custom event (RaiseEvent)
  • Using Select Case to control RadioButton processing
  • Using the DateTimePicker component
  • And an important difference between Show and ShowDialog

Also, this application was coded using VB.NET 2008 Express to ensure that you can use totally free software to code it.

"Ed" wanted to code a fairly simple application to schedule inspections. He has built a much more complete form but the basic problem can be shown using this form:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The real issue here is that if you allow users to enter their own dates into a TextBox, then you have the devil's own time validating whatever garbage they might enter. In Ed's case, he also wanted to add specific time intervals to the date. To use VB's date handling objects, you have to use a real Date datatype.

The best way around this problem is to use VB.NET's DateTimePicker component to select the dates instead. To leave the original form as uncluttered as possible, I just added a new form to the project to change the date and display it with a button:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Then the inspection date itself is declared as a Date datatype. You can put it in one of the forms or a module by itself. If this was a larger application, I would seriously consider making it an XML file similar to the one used in my Visual Basic tutorial.

The code for the second form is:

Public Class SelectNewInspectDate
   Friend InspectDate As Date
   Private Sub UpdateInspecDate_Click( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles UpdateInspecDate.Click
      InspectDate = InspectDatePicker.Value
      Me.Hide()
   End Sub
End Class

But now the fun starts.

The same subroutine can be used to process all of the RadioButton components by adding them to the Handles clause:

   Private Sub FreqChanged( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles _
         FreqMonthly.CheckedChanged, _
         FreqBiMonthly.CheckedChanged, _
         ... and so forth

Microsoft and other sites typically use a cascading If structure to process RadioButton components.

If <button1 selected> Then
   <process button1>
ElseIf <button2 selected> Then
   <process button2>
... and so forth
EndIf

I think there's a better way. Select Case structures seem more streamlined to me.

Select Case <case value>
   Case <button1 selected>
      <process button1>
   Case <button2 selected>
      <process button2>
... and so forth
End Select

But what should be used as a case value? Just think of it in the reverse of the way you usually do. Test the state of the RadioButton components against the constant True. The actual code is shown on the second page.

But what if a new date is selected? The next inspection date has to be changed then too. How can we make that happen? The code that selects a new date can call the FreqChanged event subroutine. But you have to match the parameters too. Just borrow the parameters for the Button event.

FreqChanged(sender, e)

It's also possible to raise the event rather than making a call. But there's more coding involved and no advantage, but since this is a good example to show the syntax of RaiseEvent, here's how that is done.

1 - Code a new Event in the same module:

Public Class InpectionScheduleForm
   Private Event UpdateNextInspection( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs)

2 - Add this new Event to the Handles clause:

Handles FreqMonthly.CheckedChanged, _
FreqBiMonthly.CheckedChanged, _
Me.UpdateNextInspection

3 - Raise the Event instead of simply calling the subroutine:

RaiseEvent UpdateNextInspection(sender, e)

There's just one tiny problem left now. The program doesn't work. When you change the inspection date, the wrong dates shows up in each TextBox.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Tracing the program reveals that in the event subroutine for the Button:

SelectNewInspectDate.Show()
InspectDateDisplay.Text = _
   SelectNewInspectDate.InspectDate.ToString("MMMM dd, yyyy")
FreqChanged(sender, e)

The program keeps executing without waiting for the data to update in the second form. So InspectDate, in the second form, starts at the "zero date" for .NET. The solution is to show the second form "modally" with the ShowDialog method instead.

SelectNewInspectDate.ShowDialog()

This "blocks" the execution of the thread for the main form until the second one is dismissed.

The complete code for the main form is on the next page.

Explore Visual Basic

More from About.com

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. A Two Form Example Program

©2008 About.com, a part of The New York Times Company.

All rights reserved.