Visual Basic 2005

In the good old days of VB6 I was able to open a frame and have an arrangement of labels, text, and combo boxes that referenced a real device in the real world. Since there were up to 6 identical devices out in the world I would copy the frame and paste it 5 more times on my form. This would give me 6 identical devices with indexed controls. I could now configure these and handle events in a single procedure and simply index through the controls. Everything was layed out quite logically.

Now Visual Basic 2005 comes along and I no longer have this ability. What is the most logical way to group controls together and then handle their setup and event handling in a straight-forward technique

Thanks in advance...



Answer this question

Visual Basic 2005

  • Gabriel Giggs

    Having started with VB4 and recently migrated to '05, let me say I feel your pain ;)

    Searching this fourm for "Control array" will give you several methods of making VB6-esque control arrays. The eaisest method IMHO is to just add the handlers to your events and work with what was passed in:

    Private Event Something_Clicked(Byval Sender as Object, ... ) Handles Button1.Click, Button2.Click
    ....

    End Sub

    You can DirectCast the sender to the type of control it is or just evalute it's .Name property. Also, In '05 you can use the My namespace to access the controls by a concantenated name, such as My.Controls("Label" & IndexValue).Text = "New Value".


  • Gio

    Collections.

    In switching from VB6 to VB2005, you can no longer use an Array of controls. You are supposed to use a Collection, which can mix different types of controls. To get one of my VB6 programs going, I found that defining a Collection of identical controls allowed me to reuse most of my VB6 code.

    For example, if you have 6 comboboxes, first define your Collection:

    Public Comb2 As New System.Collections.Generic.List(Of ComboBox)

    Then fill the collection with your ComboBoxes (I did in the New sub):

    Sub New()
    InitializeComponent()
    Comb2.Add(ComboBox1) 'Comb2(item=0)
    Comb2.Add(ComboBox2)
    Comb2.Add(ComboBox3)
    Comb2.Add(ComboBox4)
    Comb2.Add(ComboBox5)
    Comb2.Add(ComboBox6)
    End Sub

    Then you can use most code involving an array, but the array refers to the collection (which I called Comb2)

    For iseq = 0 To 5
    av2 = Lefty(LTrim(Mid(FullText(X), 8, 66)), X3) 'text
    Combo2T(iseq) = av2 'text array
    Comb2.Item(iseq).Text = av2 'Collection
    Comb2.Item(iseq).ForeColor = Color.White
    Comb2.Item(iseq).BackColor = Color.Black
    ToolTip1.SetToolTip(Comb2.Item(iseq), Combo2T(100 + iseq))
    Next iseq

    note I have a function called:
    Lefty = Microsoft.VisualBasic.Left(astring, X1)

    So you can maintain loops and the array style to recycle most of your code



  • Visual Basic 2005