Creating a Windows Ad Rotator in Visual Basic

Introduction

Ad rotators are usually found on Web pages and Web applications. When you see an ad on a Web application, it is most likely an ad rotator or some variant of it. Today, I will show you how easy it is to create an ad rotator, but for a Windows form, without any Web controls.

What Is an Ad Rotator?

An ad rotator displays an advertisement banner on a Web page. This banner serves as an advertisement. It can rotate through different pictures, or just make use of one image. Irrespective of the number of images used, you are able to click the picture(s) to navigate to a Web page, depending on which image was clicked. The keyword here is Web page. Seeing the fact that we are dealing with Windows Forms here, we will have to make our own ad rotators. It is actually easier than what you may realise.

Our Visual Basic Ad Rotator Project

Start Visual Studio and create a Visual Basic Windows Forms project. Once the project has loaded, add the following onto your Form:

  • PictureBox
  • Timer. Set its Enabled property to True and give it an interval that you see fit.
  • ToolTip

I have kept the default names for all objects on my form; you are welcome to use your own names for the objects.

Images

Before we start to code, first build the project. This will create the output folders and files. In this example, I have placed all the pictures that our adRotator will use in the Bin folder. I have created ten pictures, for example. Each picture will represent a "slide," and each picture will be clickable and navigate to its own Web address. I have named all my images the following:

  • Pic_0.jpg
  • Pic_1.jpg
  • Pic_2.jpg
  • Pic_3.jpg
  • Pic_4.jpg
  • Pic_5.jpg
  • Pic_6.jpg
  • Pic_7.jpg
  • Pic_8.jpg
  • Pic_9.jpg

Just a note: I am not the best artist.

You may notice that all the pictures have more or less the same name—only the numbers differ. This makes it easier use in code. Imagine if the files were named like so:

  • Picture of a zero.jpg
  • Number 1.jpg
  • It looks like a number 2.jpg
  • Lucky number 3.jpg

Names such as the preceding list will cause a lot of problems in code and it will force you to hard code the figures' values.

Code

Add the following variables just underneath your Class declaration, making them Private level variables:

   Private strURL(9) As String 'Address Array'

   Private btSelPic As Byte 'Selected Picture'

The strURL array will be used to hold the various Web addresses. btSelPic will identify which picture was clicked.

Add the following code for the Timer's Tick event:

   Private Sub Timer1_Tick(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Timer1.Tick

      Static intPicCount As Byte   'Current Picture Shown'

      'Load Pictures From Sequential Files'
      PictureBox1.Image = Image.FromFile("Pic_" & _
         intPicCount.ToString() & ".jpg")

      ToolTip1.SetToolTip(PictureBox1, strURL(intPicCount))

      btSelPic = intPicCount

      intPicCount += 1   'increment Current Picture'

      If intPicCount > 9 Then intPicCount = 0   'Start Over'

   End Sub

In the Timer_Tick event, a static variable is created. This means that the sub procedure, in this case Timer_Tick, is able to remember the variables's value after it has exited. The variable keeps incrementing, and the PictureBox's picture gets set to the appropriate picture. If the sequence reaches 9, it simply starts all over again.

Add the Form_Load event to initialise the strURL array:

   Private Sub Form1_Load(ByVal sender As Object, _
      ByVal e As System.EventArgs) Handles MyBase.Load

      'Initialise Address / Tooltip Sequence'
      strURL = New String() {
         "http://www.ncc-cla.com",
         "http://www.codeguru.com",
         "http://www.msdn.microsoft.com",
         "http://www.google.com",
         "http://www.vbforums.com",
         "http://www.facebook.com",
         "http://www.http://www.codeguru.com/forum/ _
            forumdisplay.php?s=&forumid=12",
         "http://www.codeguru.com/forum/forumdisplay.php?f=22",
         "http://www.codeguru.com/forum/forumdisplay.php?f=4",
         "http://www.cooper.com/alan/father_of_vb.html"
      }

   End Sub

Each address corresponds to an image.

Add the last event:

   Private Sub PictureBox1_Click(ByVal sender As Object, _
      ByVal e As System.EventArgs) Handles PictureBox1.Click

      'Open IE'
      System.Diagnostics.Process.Start("C:\Program Files\ _
         Internet Explorer\IExplore.exe", strURL(btSelPic))

   End Sub

This enables us to click the picture and navigate to its associated address.

Download the Files

Please feel free to download the zip file that accompanies this article. It contains the program files and the associated images.

Conclusion

It is quite easy to create your own ad rotator for a Windows project when the need arises. All you need is a bit of common sense and imagination.



About the Author

Hannes DuPreez

Hannes du Preez is a Microsoft MVP for Visual Basic for the ninth consecutive year. He loves technology and loves Visual Basic. He loves writing articles and proving that Visual Basic is more powerful than what most believe. You are most welcome to reach him at: ojdupreez1978@gmail.com

Related Articles

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date