You are here:About>Computing & Technology>Visual Basic> Using VB.NET> GDI+ Graphics in Visual Basic 2005 .NET
About.comVisual Basic
Newsletters & RSSEmail to a friendSubmit to Digg

GDI+ Graphics in Visual Basic 2005 .NET

From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!
Mar 4 2007

Geoff's Traffic Light Simulator

Consider this to be a "time out" in our march toward mastering GDI+!

I received an email from Geoff, an About Visual Basic reader who also writes programming tutorials in Scotland. He was upgrading a golden oldie that had been written originally in VB 3.0! The idea was to teach the concept of binary numbers by relating a 3-digit binary number to a traffic light. If a bit was turned on, the corresponding light in a red-yellow-green traffic signal would also be turned on.

The VB 3.0 version of this program used images dropped into a form component and it was pretty simple:

lights.Image = lightsred.Image ' VB 3.0 code

Encouraged by this series, Geoff wanted to draw shapes using GDI+ instead, but was having trouble making it work. I suggested some code that resulted in a form that looks like this:

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

Rather than the VB 3.0 technique of using an image created in a graphics editor, I set the Brush object - L1, L2, or L3 - used to draw a filled circle to either white or the color of the traffic light signal. That made the actual code used to draw the signal look like this:

g.FillEllipse(L1, 39, 11, 30, 30)

As in previous programs, I figured out the position and size by adjusting the numbers until it looked good.

The circles - actually round "ellipse" shapes in GDI+ - are drawn when a Panel component is invalidated and a Paint event is triggered. The circles are then drawn on top of another image to create a traffic light.

 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.