1. Home
  2. Computing & Technology
  3. Visual Basic
The VB.NET Upgrade Wizard
Working Code At Last -- Sort Of

At this point, the program will now run (more or less) successfully. But there are still a few surprises. Some of the things that still don't look quite right include:

  • The win lines are not erased anymore since they're GDI+ objects rather than line components and there is no code to erase them
  • There may be an unexpected error message from the program (not from VB.NET) that pops up when the program starts
  • The GDI+ win lines disappear when windows are moved or changed, such as minimizing and maximizing windows, or when another window like a MsgBox covers them temporarily
  • The win lines are interrupted by the labels where the X's and O's appear in the playing grid

Some of these things can be fixed without recoding the entire application, and some can't. Most relate to the mixing of GDI+ with other controls. Looks like Cornell and Morrison's warning about "subtle hazards" is right on target.


 Related Resources
• The VB 6 Version:
TicTacToe

• A GDI+ Introduction for Visual Basic 6 Programmers
• A GDI+ Introduction for Visual Basic 6 Programmers
• Source for Upgraded .NET Version
 

A quick and simple way to erase the win lines when a new game starts is to add an Invalidate() statement in the InitPlayGround subroutine. This function tells Windows that the contents of a window are no longer valid so that it will need to be redrawn.

The second problem can be fixed with a bit more coding. If you scan the converted source code, you see this interesting comment added by the conversion wizard just before each section of code where the problem warning window can be displayed.

UPGRADE_WARNING: Event optOPlayer.CheckedChanged may fire when form is intialized. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup2075"'

How convenient! Clicking the link gives you an interesting page that immediately plunges you into a page that assumes you completely understand how to use object inheritance in VB.NET. The page includes the helpful hint:

What to do next
Add an IsInitializing property to the form and add logic to the event procedure to check the value of the property before running the code.

And this example code is also included:

Check Initialization Code "Add an IsInitializing property to the form" ???

For many VB 6 programmers just learning VB.NET, this might be like instructions for climbing Mount Everest that start with, "Making the final assault ... " Wait a sec! We have to get close to the summit first!

In brief (very brief, in fact, way too brief), DefInstance is a form object property derived from the base form. You can see it declared in the "Upgrade Support" Region that was automatically created by the conversion wizard. You can add another property - the hint suggests that you call it IsInitializing - with the new Property syntax for VB.NET. Property Set, Property Let and Property Get are not supported in VB.NET. (What! More new, incompatible stuff? Yes, yes, and yes again!) Here's how you can code a new property for the main form, frmTicTacToe. The Get and Set here are entirely different from the ones in VB 6.


  
      ' Form property to show when form is initializing
      Private m_IsInitializing As Boolean = True
      Public Property IsInitializing() As Boolean
          Get
              Return m_IsInitializing
          End Get
          Set(ByVal Value As Boolean)
              m_IsInitializing = Value
          End Set
      End Property

Once this property is available in frmTicTacToe, you can add this to the initialization code:

frmTicTacToe.DefInstance.IsInitializing = False

and this to the two problem subroutines (only the first is shown):

  
      Private Sub optOPlayer_CheckedChanged( ... {same}
          If frmTicTacToe.DefInstance.IsInitializing = True Then
              Exit Sub
          Else
  {same}
          End If
      End Sub
  

In our final, VB.NET only version, we do away with this kind of confusing code adjustments entirely. This is another reason why you might want to consider just starting over to convert to VB.NET.

This still doesn't solve the problem of the GDI+ graphics used for the win lines not refreshing or painting correctly. For this, we'll have to wait for the next version of Tic Tac Toe that is created the "VB.NET" way!

The two main goals of this article were to introduce you to VB.NET and the conversion wizard, but more importantly, to convince you that VB.NET is a whole 'nother world. As Dorothy might have said if the tornado had taken her to VB.NET instead of Oz,


"Toto, we're not in Visual Basic anymore!"

Next page > Start of Article > Page 1, 2, 3, 4, 5, 6, 7, 8, 9

Explore Visual Basic

More from About.com

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

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

All rights reserved.