Comparison Between VB 6.0 and VB .NET Objects, Part 2

In Part 1 of Comparison Between VB 6.0 and VB.NET Objects, you looked at the differences in the Properties Windows. This article, Part 2, compares the most obvious and common features between the VB 6.0 and VB.NET Toolboxes, based on these aspects:

  1. Toolbox Tabs
  2. Toolbox Tools
  3. Active X
  4. Control Arrays

ToolBox Tabs

The General tab of the Visual Basic 6.0 Toolbox displays the standard Visual Basic controls plus any ActiveX controls you have added to your project.

With the General tab, you can:

  • Display ToolTips for the Toolbox buttons: Select the Show ToolTips option in the General tab of the Tools, Options dialog box.


    Figure 1

  • Customize the Toolbox by adding tabs to it: Right Click anywhere on the Toolbox, to display Figure 2


    Figure 2

    Select Add Tab from the displayed menu, once clicked, a Dialog Box, similar to Figure 3, will be displayed.


    Figure 3

    Inside the above dialog box, you can enter a descriptive name for the new Toolbox tab.
    For example: Enter MDI in the New Tab Name dialog box.

    Repeat the previous steps for each new tab you want to add.
    For example: Enter DataBase Controls in the New Tab Name dialog box.

    Your Visual Basic 6.0 Toolbox should now look like Figure 4.


    Figure 4

  • Customize the General tab or a custom tab: Once all the necessary tabs are added to the Visual Basic 6.0 Toolbox, you now can add controls to the added tabs by selecting the Components command from the Project menu.

For the MDI Tab, you could include the following controls.

Microsoft Windows Common Controls 6.0 (SP4)

The following controls are contained within the Microsoft Windows Common Controls 6.0 (SP4) set of controls:

  • ImageList
  • ListView
  • ProgressBar
  • Slider
  • Status Bar
  • TabStrip
  • Toolbar
  • TreeView

Your MDI Toolbox tab should now look like that in Figure 5:


Figure 5

Note: If these controls were added to the General tab, you may need to drag these controls from the General tab to the MDI tab.

Feel free to experiment with adding controls to the DataBase tab. Some controls that you can be added to your DataBase tab are:

  1. Microsoft Data Bound List Controls 6.0: The following controls are contained within the Microsoft Data Bound List Controls 6.0 set of controls:
    • DBList
    • DBCombo
  2. Microsoft DataList Controls 6.0 (OLEDB): The following controls are contained within the Microsoft DataList Controls 6.0 (OLEDB) set of controls:
    • DataList
    • DataCombo
  3. Microsoft ADO Data Control 6.0 (OLEDB)
  4. Microsoft DataGrid Control 6.0 (OLEDB)
  5. Microsoft DataRepeater Control 6.0 (OLEDB)

Your toolbox should now look like Figure 6.


Figure 6

To rename and delete tabs from the Visual Basic 6.0 Toolbox, right-click on the Tab name, and choose the desired option from the displayed menu, as shown in Figure 7.


Figure 7

Toolbox Tools

Contols are the building blocks with which you assemble your Visual Basic 6.0 program. The Visual Basic 6.0 Toolbox is a palette of controls, and you build your user interface by selecting controls from the Visual Basic 6.0 Toolbox and placing them on your forms.


Figure 8

Some controls are built into Visual Basic 6.0 and can't be removed from the Toolbox. These controls reside within Visual Basic 6.0 itself. These controls (as shown in Figure 8) are known as intrinsic controls. Other controls live outside Visual Basic 6.0 and reside in files that end with the extension .ocx. These controls are known as ActiveX controls, and can be added and removed from the Visual Basic 6.0 Toolbox—but more on that a little later.

For a detailed explanation of the Visual Basic 6.0 intrinsic controls, consult this Web page: Using the Standard Controls and more.

Active X Controls

ActiveX is a set of technologies based on Microsoft's Component Object Model (COM) for creating reusable binary objects. ActiveX Controls are objects that can be placed on a form so that users can interact with applications. ActiveX Controls have events, properties, and methods and can be incorporated into other controls. ActiveX controls have an .ocx file extension.

To add an ActiveX control to the Toolbox, follow these steps:

Right-click anywhere on the toolbox, and then select Components from the displayed menu:


Figure 9

Or, select Project, Components from the Visual Basic 6.0 menu to bring up this dialog box:


Figure 10

The Components dialog box allows you to add components controls, designers, or insertable objects to your project. The Components dialog box contains the following three (3) tabs:

  1. Controls
  2. Designers
  3. Insertable Objects

Figure 11

Use the Controls tab to add ActiveX Controls to the Toolbox.

On the Controls tab, you also can browse for additional ActiveX Controls not found in the displayed list.


Figure 12


Figure 13
The Designer tab includes Designer Objects such as:
Data Environment Designer
Data Report
DHTML Page

that you can add to your Visual Basic 6.0 project.

Figure 14
Insertable objects, such as a Microsoft Excel Worksheet object, are components you can use as building blocks to build integrated solutions. An integrated solution can contain data in different formats, such as spreadsheets, bitmaps, and text, which were all created by different applications.

Control Arrays

A Control Array is an indexed group of similar controls that are of the same type and have the same name. Individual elements within the control array are identified by a unique index number.

Now, you can create a control array of CommandButtons.

Creating a Control Array

Method 1:

  • Add a CommandButton to your Form, and name it cmdCommandArray, for example.
  • Make sure that cmdCommandArray is selected. Choose Copy from the Edit menu (or right-click the obejct and select Copy) to copy the CommandButton to the Clipboard.
  • Choose Paste from the Edit menu (or right-click the Form and choose Paste). You're presented with a dialog box asking whether you want to create a control array. Click Yes to create the control array.

Now that the control array is created, if you go to the Properties window and display the Object drop-down list, notice that there are now two (2) CommandButtons with the same name, cmdControlArray, each with its own subscript.

Method 2:

  • Add a CommandButton to your Form, and name it cmdCommandArray, for example.
  • Add a second CommandButton to your Form, and give it the same name (cmdCommandArray)
  • You're presented with a dialog box asking whether you want to create a control array. Click Yes to create the control array.

Method 3:

  • Add a CommandButton to your Form, and name it cmdCommandArray, for example.
  • Change the CommandButton's (cmdControlArray) Index Property to 0.
  • Change the CommandButton's Caption to "Button 1".
  • Press F7 to access the Code Window.
  • Edit your Form_Load event to resemble the following:
Private Sub Form_Load()    'Occurs when form is loaded into memory

   'Create a new CommandButton
   Load (cmdCommandArray(1))    'first object has a 0 based index,
                                'so 1 means "2"

   'Move the new button directly underneath the previous button
   'same left position on form
   cmdCommandArray(1).Left = cmdCommandArray(0).Left
   cmdCommandArray(1).Top  = cmdCommandArray(0).Top _
   + cmdCommandArray(0).Height    'directly underneath previous button

   cmdCommandArray(1).Caption = "Button 2"    'give unique caption

   'make the new button visible, without making it visible, the
   'object will exist only in memory, and not on screen
   cmdCommandArray(1).Visible = True

End Sub

By following Method 3, you now are able to dynamically add CommandButtons to the Control Array at run time. New controls are exact duplicates of the first control element of the control array. The values of all properties except Index and Visible are identical—including the values of Left and Top. Thus, when you create a new control, it will be placed right over the first control in the array. For the new control to coexist with other controls in the array, you must move the control to a new position.

When you run the code, notice that the program creates a new CommandButton on the form and places it just below the first.

Events for Control Arrays

All Control Array elements share the same event handler.

For example: Double-click your cmdCommandArray button on the form. Your Code Editor should now have a procedure similar to the following:

Private Sub cmdCommandArray_Click(ByVal Index As Integer)
End Sub

Notice the above Index argument, now included in the CommandButton's event handler. This is how you distinguish between the elements of the control array, usually with a Select Case statement. A small example follows:

Private Sub cmdCommandArray_Click(ByVal Index As Integer)

Select Case Index    'determine which element was clicked on
   Case 0            'first button
      'indicate that first button was clicked
      MsgBox("First Button Clicked")
   Case 1            'second button
      'indicate that second button was clicked
       MsgBox("Second Button Clicked")
End Select

End Sub

When the above code is run, you will notice that you are able to click on both buttons, and both buttons display a different message, indicating which button was clicked.

Now, look at how the Visual Basic .NET Toolbox changed, and how you can accomplish the same functionalities.

Comparison Between VB 6.0 and VB .NET Objects, Part 2

Now that you've seen the familiar Visual Basic 6.0 toolbox in action, look at how the (now mediocre) Visual Basic 6.0 Toolbox morphed into a much more manageable and user friendly toolbox, namely the Visual Basic .NET Toolbox.

ToolBox Tabs

The Visual Basic .NET Toolbox is divided into the following panes:

  1. Windows Forms
  2. Components
  3. Data
  4. Clipboard Ring
  5. General
[NETToolBoxWindowsFormsTab.jpg]
Figure 15
Windows Forms tab: Displays a list of Windows Forms controls and dialog boxes that can be used with Windows applications. The controls and components on the Windows Forms Tab are ordered by estimated frequency of usage. To set the order to be alphabetical, right-click the Toolbox tab and choose Sort Items Alphabetically.

To make use of certain control capabilities, you may need to work with the controls in an a typical fashion. For example, to assemble multiple RadioButton and CheckBox controls on a form so that they behave as a group, you must first add a GroupBox control to your form and then add the desired controls to the GroupBox. Additionally, certain controls that do not have a user interface (such as the Timer or ImageList controls) will appear in the tray at the bottom of the Windows Forms Designer, rather than on the surface of the form itself, when added to the form. For more information about working with these controls, see Adding Controls Without a User Interface to Windows Forms.

The Components and Data tabs of the Toolbox also contain controls and components that can be used with Windows Forms.

[NETToolBoxComponentsTab.jpg]
Figure 16
Components tab: Displays components you can add to Visual Basic and Visual C# designers. In addition to the .NET Framework components that are shipped with Visual Studio (such as the MessageQueue and EventLog components), you can add your own or third-party components to this tab. For details, see Managing Tabs and Items in the Toolbox.

To display this tab from the View menu, select Toolbox. In the Toolbox, select the Components tab.

[NETToolBoxDataTab.jpg]
Figure 17
Data tab: Displays data objects you can add to a Visual Basic and C# forms and components. The Data tab of the Toolbox appears when you create a project that has an associated designer. The Toolbox appears by default in the Visual Studio integrated development environment; if you need to display the Toolbox, select Toolbox from the View menu.

For instance: The DataSet control adds an instance of a typed or untyped dataset to the form or component. When you drop this object onto a designer, it displays a dialog box that allows you to select an existing typed dataset class or specify that you want to create a new, blank untyped dataset.

The SqlConnection control creates a connection object that allows you to communicate with SQL Server version 7.0 or later.

The DataView control Creates a data view that allows you to specify filter and sort options in an object that controls can bind to.

[NETToolBoxClipBoardRingTabCodeView.jpg]
Figure 18
Clipboard Ring tab: This tab stores the last twelve items added to the system Clipboard Ring by using the Cut or Copy commands. These items then can be dragged from the Clipboard Ring tab and dropped onto the active editing or design surface. The Toolbox is available from the View menu.

To cycle through the contents of the Clipboard Ring tab while you work in an editor or designer, press CTRL+SHIFT+V repeatedly. This pastes successive items from the Clipboard Ring tab into the active file, replacing the last item inserted with the next one selected. When the desired item appears, you can then press CTRL+V (paste) repeatedly to paste the currently selected item multiple times.

[NETToolBoxAllTabsGeneralDisplayed.jpg]
Figure 19
General tab: The General tab appears by default whenever the Toolbox opens. You can use this tab to store default controls for your projects, including custom controls. The Toolbox is available from the View menu.

Initially, the General tab of the Toolbox contains only the Pointer tool. You can add, remove, rename, and organize items on this tab, including custom items. These items then can be dragged from the General tab and dropped onto the active editing or design surface.

Customize the Toolbox by adding tabs to it:

To add a tab to the Visual Basic .NET Toolbox, all you need to do is:

Right-click anywhere on the Toolbox; a menu similar to Figure 20 will be displayed.

[NETToolBoxSelectedToolMenu.jpg]
Figure 20: Choose Add Tab

Unlike Visual Basic 6.0, a dialog box for entering the Tab Name will not appear. If you look closely at the Visual Basic .NET Toolbox, you will notice an entry field for the new tab name on the Visual Basic .NET Toolbox itself.

Enter a name (MDI, for example) in the space provided and press Enter. The new Toolbox tab, named MDI, should now appear as part of your Visual Basic .NET Toolbox, as Figure 21 shows.

[MDI.JPG]
Figure 21

To add controls to your newly added tab, drag the necessary controls from any of the other tabs. In Figure 21, I have dragged the Timer Control from the Components tab (or the Windows Forms tab), and the HScrollbar from the Windows Forms tab. Feel free to experiment with some other controls.

Note: You can add, remove, rename, and organize items on the General tab as well.

Customize the appearance of controls in the Visual Basic .NET Toolbox tabs:

From a personal point of view, I've found it somewhat tedious sometimes to scroll through the entire list of Windows Forms controls, especially when I want to use a control at the bottom, or near the bottom of the entire list of controls, like the PageSetupDialog control, for example.

To display all the controls on a particular tab, follow these easy steps:

  1. Right-click any Tab Name (Windows Forms, for example) to display Figure 22.
  2. Deselect (uncheck) ListView.

[NETToolBoxSelectedTabMenu.jpg]
Figure 22

Your Windows Forms tab should now resemble Figure 23:

[NETToolBoxWindowsFormsTabMDI.jpg]
Figure 23

Toolbox Tools

Differences between the Visual Basic 6.0 controls and Visual Basic .NET controls.
Using the Data tab controls
Using the Components tab controls
Using the Windows Forms tab controls

ActiveX Controls

Changes in ActiveX control creation
Authoring a component in Visual Basic .NET

Control Arrays

The creation of Control Arrays in Visual Basic .NET has changed quite a bit since Visual Basic 6.0. Visual Basic .NET does not allow you to give two (2) objects the same name, neither does it have an Index property for controls as in Visual Basic 6.0. Now you may ask: How do I create control arrays, then?

Answer:
You can't create control arrays per se, but you can still share one event handler with multiple controls, using one of two (2) simple methods:

  1. AddHandler
  2. Handles

You can use the AddHandler procedure, as in the following example:

  • Add three (3) Button controls to your Form.
  • Name each of them btnArrayButton1, btnArrayButton2, and btnArrayButton3, respectively.
  • Switch to Code View by pressing F7.
  • Expand the Windows Form Designer generated code region by clicking on the plus ( + ) sign next to Windows Form Designer generated code.
  • Edit the Sub named New as follows:
  • Public Sub New()
    
    MyBase.New()
    
    'This call is required by the Windows Form Designer.
    
    InitializeComponent()
    
    'Add any initialization after the InitializeComponent() call
    'adds event handler to btnArrayButton1
    AddHandler btnArrayButton1.Click, AddressOf button_Click
    'adds event handler to btnArrayButton2
    AddHandler btnArrayButton2.Click, AddressOf button_Click
    'adds event handler to btnArrayButton3
    AddHandler btnArrayButton3.Click, AddressOf button_Click
    
    End Sub
    

The above procedure is known as a Constructor. Constructors are special methods that allow control over initialization. They are run after the program begins or when an instance of a type is created. Unlike other members, constructors are not inherited and do not introduce a name into a type's declaration space. Constructors may only be invoked by object-creation expressions or by the .NET Framework; they may never be directly invoked. You must include the AddHandler statement in the Constructor to make it part of the Form's class, so that the eventhandlers are known to the Form. AddressOf designates the name of the procedure to run.

Once done, click on the minus ( - ) sign next to Windows Form Designer generated code and add the following Sub Procedure, which is the Procedure that is supposed to run when either button is clicked:

Private Sub button_Click(ByVal sender As Object, _
                         ByVal evArgs As EventArgs)

   'determine if btnArrayButton1 is clicked
   If (sender Is btnArrayButton1) Then
      MessageBox.Show("Button1 Pushed!")
   'determine if btnArrayButton2 is clicked
   ElseIf (sender Is btnArrayButton2) Then
      MessageBox.Show("Button2 Pushed!")
   'determine if btnArrayButton3 is clicked
   ElseIf (sender Is btnArrayButton3) Then
      MessageBox.Show("Button3 Pushed!")
   End If

End Sub

AddHandler takes two arguments: the name of an event from an event sender such as a control, and an expression that evaluates to a delegate. You do not need to explicitly specify the delegate class when using AddHandler because the AddressOf statement always returns a reference to the delegate.

If you want to handle multiple events with a single event handler, you can do this by registering the same method with multiple events using the AddHandler procedure. Each event must have the same signature. When you use a single event-handling method for multiple events, you can determine which control raised an event from the sender parameter.

When the above code is run, you will notice that you are able to click on all three buttons, and all three buttons display a different message, once clicked.

You can use the Handles statement in a procedure, as in the following example:

  • Add three (3) Button controls to your Form.
  • Name each of them btnArrayButton1, btnArrayButton2, and btnArrayButton3, respectively.
  • Double-click btnArrayButton1 to display its event handler.
  • Edit the displayed event procedure as follows:
  • Private Sub btnArrayButton1_Click( _
       ByVal sender As Object, _
       ByVal e As System.EventArgs) _
       Handles btnArrayButton1.Click, btnArrayButton2.Click, _
               btnArrayButton3.Click     'adds event handlers to
                                         'all three buttons
               MessageBox.Show("Control Array Test", "Handles Example")
    
    End Sub
    

By specifying Handles in any event procedure, you are, in a sense, linking the different events of different controls to each other, causing Visual Basic .NET to execute one procedure only for multiple controls.

When the above code is run, you will notice that you are able to click on all three buttons, and all three buttons display the same message, once clicked.

Extending the Visual Basic .NET Toolbox During Runtime

The Visual Basic .NET Toolbox Interface

Conclusion

I hope that, when you open Visual Basic .NET for the first time, that you will not find it too difficult to get accustomed to the Visual Basic .NET Toolbox. Be on the lookout for Part III in the Comparison Between VB 6.0 and VB.NET Objects series, entitled Project Explorer versus Solution Explorer.



About the Author

Hannes du Preez

Hannes du Preez is a Microsoft MVP for Visual Basic. He is a trainer at a South African-based company. He is the co-founder of hmsmp.co.za, a community for South African developers.

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

  • Hundreds of millions of users have adopted public cloud storage solutions to satisfy their Private Online File Sharing and Collaboration (OFS) needs. With new headlines on cloud privacy issues appearing almost daily, the need to explore private alternatives has never been stronger. Join ESG Senior Analyst Terri McClure and Connected Data in this on-demand webinar to take a look at the business drivers behind OFS adoption, how organizations can benefit from on-premise deployments, and emerging private OFS …

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

Most Popular Programming Stories

More for Developers

RSS Feeds