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

GDI+ Graphics in Visual Basic 2005 .NET
Geoff's Traffic Light Simulator - The Code

By Dan Mabbutt, About.com

Mar 4 2007

Here's the completed code that I suggested. If you code this, you will have to find or create the SigImage.GIF file. Create a copy of any small GIF file with the same name just to make the program run if you don't care about whether it looks exactly the same.

Public Class Form1    Private L1 As Brush = Brushes.White
   Private L2 As Brush = Brushes.White
   Private L3 As Brush = Brushes.White
   Private Sub Bit_CheckedChanged( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles Bit2.CheckedChanged, _
            Bit1.CheckedChanged, _
            Bit0.CheckedChanged
      Dim Signal As Integer = 0
      L1 = Brushes.White
      L2 = Brushes.White
      L3 = Brushes.White
      Bit0Label.Text = "0"
      Bit1Label.Text = "0"
      Bit2Label.Text = "0"
      If Bit0.Checked Then
         Signal = Signal Or 1 ' 00000001 in binary
         Bit0Label.Text = "1"
      End If
      If Bit1.Checked Then
         Signal = Signal Or 2 ' 00000010 in binary
         Bit1Label.Text = "1"
      End If
      If Bit2.Checked Then
         Signal = Signal Or 4 ' 00000100 in binary
         Bit2Label.Text = "1"
      End If
      DecEquiv.Text = CStr(Signal)
      Select Case Signal
         Case 1 ' binary 00000001
            L1 = Brushes.Red
         Case 2 ' binary 00000010
            L2 = Brushes.Goldenrod
         Case 3 ' binary 00000011
            L1 = Brushes.Red
            L2 = Brushes.Goldenrod
         Case 4 ' binary 00000100
            L3 = Brushes.Green
         Case 5 ' binary 00000101
            L1 = Brushes.Red
            L3 = Brushes.Green
         Case 6 ' binary 00000110
            L2 = Brushes.Goldenrod
            L3 = Brushes.Green
         Case 7 ' binary 00000111
            L1 = Brushes.Red
            L2 = Brushes.Goldenrod
            L3 = Brushes.Green
      End Select
      LightPanel.Invalidate()
   End Sub
   Private Sub LightPanel_Paint( _
      ByVal sender As Object, _
      ByVal e As System.Windows.Forms.PaintEventArgs) _
         Handles LightPanel.Paint
      Dim g As Graphics = e.Graphics
      Dim SigImage As Image = _
         Image.FromFile("D:\SigImage.gif")
      g.DrawImage(SigImage, New Point(0, 0))
      g.FillEllipse(L1, 39, 11, 30, 30)
      g.FillEllipse(L2, 39, 47, 30, 30)
      g.FillEllipse(L3, 39, 83, 30, 30)
   End Sub
End Class

If you detoured to this article from the GDI+ tutorial, you can get back to the beginning at Part 1 or where you might have been at Part 4.

Explore Visual Basic
By Category
About.com Special Features

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

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. GDI+ Graphics in Visual Basic 2005 .NET

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

All rights reserved.