1. Home
  2. Computing & Technology
  3. Visual Basic
New Upgrade Wizard Tricks
Part 4: Converting a Control Array: A Better Way

One way to go would be to use the enhanced capability of the Handles keyword in VB .NET to handle every button with the same subroutine. Here's what this looks like in code:

Private Sub Button1_Click( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles _
    Button1.Click, Button2.Click, _
    Button3.Click, Button4.Click, _
    Button5.Click, Button6.Click, _
    Button7.Click, Button8.Click, _
    Button9.Click, Button0.Click
    Dim Ctrl As Control = CType(sender, Control)
    MsgBox("You clicked " & Ctrl.Name & vbCrLf & _
    "and the Tag property is " & Ctrl.Tag)
End Sub
Alternative Control Array

The buttons are not part of a Control Array anymore. They're grouped only because your code specifies that the same subroutine should be called for all of them. When you think that, first, you can process any collection of form controls with the same subroutine, not just identical buttons like VB 6 allows. And, second, you have a much richer set of events to choose from (57 versus 17 as a raw count for buttons), VB .NET provides a lot more capability to help you get the job done.

The verdict on the Upgrade Wizard isn't quite as positive, however. Think of it as one of those emergency spare tires they put in cars these days. Useful in a pinch, but not the kind of thing you want to depend on for the 'long haul'. For that, you need to learn how to be your own Upgrade Wizard!

First page > New Upgrade Wizard Tricks > Page 1, 2, 3, 4
Explore Visual Basic
By Category
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, 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

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

All rights reserved.