Any equivalent to VB6 form controls having an index?

J Trahair

Well-known member
Joined
May 14, 2008
Messages
175
Location
Spain
Programming Experience
10+
Is there an equivalent of the VB6 thing where say textboxes could have an Index number so there is one set of events, shared by each indexed txtBox, eg. txtBox(0).Text, txtBox(7).Text, and:

Private Sub txtBox_KeyPress(Index As Integer, KeyAscii As Integer)

If Index = 3 Or Index = 5 Then
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End If

If Index = 8 Then
KeyAscii = Asc(LCase(Chr(KeyAscii)))
End If

End Sub
 
All controls have a Controls collection property, this is a "control array" only endlessly more flexible than the Classic VB variant. Controls in these collections can be accessed by both integer index and string name.

In .Net all controls have a common set of events. Different controls may have additional events, these events may have same signature between various types controls.

An event handler may be wired up to handle the events of any number of objects, also of different types as long as the event signature is the same. .Net event handler signatures share a common parameter pattern, they have a "sender" parameter of Object type that always identifies the instance that raised the event, and an "e" parameter of various types that carry all additional event info.

Setting up the event handlers is easy and can be done 3 different ways: (A) generated from Designer, (B) generated from Code view, or (C) dynamically in code with the AddHandler statement. The generated event handlers require that the variables referencing these controls are declared WithEvents, which the forms Designer do by defaults.
  • In Designer select one or more controls, then change properties window to "events" (lightning icon), find the event and doubleclick it to generate the handler method (or optionally name the method and press Enter). It will automatically add all selected controls in the Handles list. It may look like this:
    VB.NET:
    Private Sub buttons_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click, Button1.Click
        
    End Sub
    Just doubleclicking a control in Designer generates the handler for the default event of that control.
  • In the two comboboxes at top of Code view you can select the objects to the left and the events to the right. Same code is generated, but here you can only select one object. In Code view you can write in additional controls in the Handles list manually also. You can also declare your own class variables WithEvents and select them in the left combo to generate event handlers.
  • Writing code to create control instances and wire up events is easy, here is an example:
    VB.NET:
    Private Sub CreateButtons()
        For i As Integer = 1 To 10
            Dim b As New Button
            b.Name = "codeButton" & i.ToString
            b.Text = i.ToString
            b.Location = New Point(i * b.Width, 0)
            Me.Controls.Add(b)
            AddHandler b.Click, AddressOf buttons_click
        Next
    End Sub
    
    Private Sub buttons_click(ByVal sender As Object, ByVal e As EventArgs)
        Dim b As Control = DirectCast(sender, Control)
        MsgBox(b.Name & " was clicked")
    End Sub
 
Thanks John. But these controls are created at run time - you can't add them at design time, position them by hand, alter widths to suit the data being entered etc.
 
Thanks John. But these controls are created at run time - you can't add them at design time, position them by hand, alter widths to suit the data being entered etc.

John's code example has a sub that creates controls on the fly at run time and adds event handlers for them too.

Also anything you can do at design time can be done at runtime and you can move things around at runtime that were created at design time.
 
Nice code John.

I like the code, and am using and altering it to make my menus. My only question is how to remove an object created at run time? I generate the first 'main' menu, and then click a button to open the sub menu. But when I click on another item in the main menu, the old buttons still show, and I have the new ones and they share names. but_sub_1, _2, _3, _2, _3.

Any help?

As always, thanks!

Nick
 
Back
Top