1. Home
  2. Computing & Technology
  3. Visual Basic
The VB.NET Upgrade Wizard
Clear as mud? Let's go over this a bit more slowly.
 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
 


Clear as mud? Let's go over this a bit more slowly.

1 - Create Enum structures

Drawing the lines with GDI+ will require five parameters. (That is, for the particular version of the "overloaded" GDI+ DrawLine method we will use.) The first is a Pen object and the other four are the x and y coordinates of the start and end of the lines. You can provide a convenient way to specify all of these using meaningful names by including Enum structures at the top of your code. The actual values (the default is pixels, not in twips as in VB 6 - another change) will vary according to how you have drawn your form and you may have to adjust them to draw the lines where they look good in your form. Here's the Enum that worked for me:

Enum Structure

2 - Create a GDI+ graphics object in the form and a Pen object

If you're following along, this is the section of code where your program is now crashing when you try to run it. Specifically, it's not finding linWin in the statement:

linWin(iWin).Visible = True

You can simply delete this statement. The replacement with GDI+ takes a few more lines.

[Old code -- place the GDI+ declarations and Case structure after this]
If iWin >= 0 Then

' Display the appropriate win line on the playing grid


[The GDI+ declarations]
' Set up Win Line graphics
Dim g As Graphics
' Sets g to a Graphics object representing the drawing surface
' of the form g is a member of.
g = Me.CreateGraphics
Dim myPen As New Pen(Color.Red)
myPen.Width = 5

3 - Draw the appropriate win line with a Case structure

Here's where the Enum's defined earlier pay off. The Case structure simply tests iWin and then uses the DrawLine method. Here's the first two (of eight required) selections to give you the idea. The entire Case structure is in the downloadable source code.

[The first few lines of the Case structure -- see the entire source listing for the rest] Select Case iWin
Case 0
g.DrawLine(myPen, _
XPoint.RightSide, YPoint.FirstRow, _
XPoint.LeftSide, YPoint.FirstRow)
Case 1
g.DrawLine(myPen, _
XPoint.RightSide, YPoint.SecondRow, _
XPoint.LeftSide, YPoint.SecondRow)

Next page > Working Code At Last? > Page 1, 2, 3, 4, 5, 6, 7, 8, 9
Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

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

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

All rights reserved.