Control Arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Arne Beruldsen

    #1

    Control Arrays

    I'm a recent convert to VB.net from VB6...and I can't believe they don't
    support control arrays. I have an app that uses a ton of Control
    Arrays...mostly labels and text boxes.

    I need some guidance on how to proceed...inclu ding any third party tools.

    Thanks
  • Tim Patrick

    #2
    Re: Control Arrays

    Try converting your VB6 application to .NET using the Upgrade Wizard built
    in to Visual Studio. To use the wizard, just open your VB6 .vbp file with
    the "File/Open Project" command in Visual Studio for .NET. Then check the
    code in those forms that use control arrays. The wizard uses some custom
    classes that try to simulate access in a control-array-like fashion.

    If your control arrays exist to support controls that share all logic in
    common, you can now share event handlers among multiple controls. This lets
    you give unique names to each control, but still use a common event handler.
    If the controls in your control array have completely unrelated logic, it
    is best to make them distinct controls with unique names, and dispense with
    the control array altogether.

    -----
    Tim Patrick
    Start-to-Finish Visual Basic 2005
    I'm a recent convert to VB.net from VB6...and I can't believe they
    don't support control arrays. I have an app that uses a ton of
    Control Arrays...mostly labels and text boxes.
    >
    I need some guidance on how to proceed...inclu ding any third party
    tools.
    >
    Thanks
    >

    Comment

    • Steve Long

      #3
      Re: Control Arrays

      Arne,
      I use Tim's approach a lot and it works well. So, you can give your controls
      unique names and in code use the AddHandler method to set the method for
      whatever event it is you are trying to handle. So, you would write a
      function, say TextBox_Click(s ender as Object, e as EventArgs)
      Then, use the AddHandler method:
      AddHandler mytextbox.Click , AddressOf TextBox_Click

      Then, in your TextBox_Click routine, you could cast the sender object to a
      textbox object and check its name.
      So:
      Dim myTxt as TextBox
      if Typeof sender is TextBox then
      myTxt = DirectCast(send er, TextBox)
      end if

      select case myTxt.Name.ToLo wer
      case "myname"
      ' do something
      case else
      ' do something else
      end select

      I wasn't looking at the docs when I wrote this so forgive if I've messed up
      an parameters to functions

      :)
      Steve

      "Arne Beruldsen" <ArneBeruldsen@ discussions.mic rosoft.comwrote in message
      news:E9270B7B-460E-42A1-9C8C-921CB012001F@mi crosoft.com...
      I'm a recent convert to VB.net from VB6...and I can't believe they don't
      support control arrays. I have an app that uses a ton of Control
      Arrays...mostly labels and text boxes.
      >
      I need some guidance on how to proceed...inclu ding any third party tools.
      >
      Thanks

      Comment

      • Tim Patrick

        #4
        Re: Control Arrays

        Using the name works fine, but you can also test the object itself using
        the "Is" keyword to see if it is actually one of your controls.

        Public Sub MyTextBoxHandle r(ByVal sender As Object, ByVal e As System.EventArg s)
        _
        Handles TextBox1.TextCh anged, TextBox2.TextCh anged, TextBox3.TextCh anged
        If (sender Is TextBox1) Then
        ' ----- Do text box #1 stuff.
        ElseIf (sender Is TextBox2) Then
        ' ----- Do text box #2 stuff.
        ElseIf (sender Is TextBox3) Then
        ' ----- Do text box #3 stuff.
        End If

        ' ----- Then add common code here.

        End Sub

        Even though, I think it is generally best to handle each control separately,
        and call common routines where there are overlaps.

        -----
        Tim Patrick
        Start-to-Finish Visual Basic 2005
        Arne,
        I use Tim's approach a lot and it works well. So, you can give your
        controls
        unique names and in code use the AddHandler method to set the method
        for
        whatever event it is you are trying to handle. So, you would write a
        function, say TextBox_Click(s ender as Object, e as EventArgs)
        Then, use the AddHandler method:
        AddHandler mytextbox.Click , AddressOf TextBox_Click
        Then, in your TextBox_Click routine, you could cast the sender object
        to a
        textbox object and check its name.
        So:
        Dim myTxt as TextBox
        if Typeof sender is TextBox then
        myTxt = DirectCast(send er, TextBox)
        end if
        select case myTxt.Name.ToLo wer
        case "myname"
        ' do something
        case else
        ' do something else
        end select
        I wasn't looking at the docs when I wrote this so forgive if I've
        messed up an parameters to functions
        >
        :)
        Steve
        "Arne Beruldsen" <ArneBeruldsen@ discussions.mic rosoft.comwrote in
        message news:E9270B7B-460E-42A1-9C8C-921CB012001F@mi crosoft.com...
        >
        >I'm a recent convert to VB.net from VB6...and I can't believe they
        >don't support control arrays. I have an app that uses a ton of
        >Control Arrays...mostly labels and text boxes.
        >>
        >I need some guidance on how to proceed...inclu ding any third party
        >tools.
        >>
        >Thanks
        >>

        Comment

        • Steve Long

          #5
          Re: Control Arrays

          Hmm, cool. I didn't even think about chaining the Handles keyword...


          "Tim Patrick" <invalid@invali d.com.invalidwr ote in message
          news:e3b469761f 078c8d6b40a04b9 36@newsgroups.c omcast.net...
          Using the name works fine, but you can also test the object itself using
          the "Is" keyword to see if it is actually one of your controls.
          >
          Public Sub MyTextBoxHandle r(ByVal sender As Object, ByVal e As
          System.EventArg s) _
          Handles TextBox1.TextCh anged, TextBox2.TextCh anged,
          TextBox3.TextCh anged
          If (sender Is TextBox1) Then
          ' ----- Do text box #1 stuff.
          ElseIf (sender Is TextBox2) Then
          ' ----- Do text box #2 stuff.
          ElseIf (sender Is TextBox3) Then
          ' ----- Do text box #3 stuff.
          End If
          >
          ' ----- Then add common code here.
          >
          End Sub
          >
          Even though, I think it is generally best to handle each control
          separately, and call common routines where there are overlaps.
          >
          -----
          Tim Patrick
          Start-to-Finish Visual Basic 2005
          >
          >Arne,
          >I use Tim's approach a lot and it works well. So, you can give your
          >controls
          >unique names and in code use the AddHandler method to set the method
          >for
          >whatever event it is you are trying to handle. So, you would write a
          >function, say TextBox_Click(s ender as Object, e as EventArgs)
          >Then, use the AddHandler method:
          >AddHandler mytextbox.Click , AddressOf TextBox_Click
          >Then, in your TextBox_Click routine, you could cast the sender object
          >to a
          >textbox object and check its name.
          >So:
          >Dim myTxt as TextBox
          >if Typeof sender is TextBox then
          >myTxt = DirectCast(send er, TextBox)
          >end if
          >select case myTxt.Name.ToLo wer
          >case "myname"
          >' do something
          >case else
          >' do something else
          >end select
          >I wasn't looking at the docs when I wrote this so forgive if I've
          >messed up an parameters to functions
          >>
          >:)
          >Steve
          >"Arne Beruldsen" <ArneBeruldsen@ discussions.mic rosoft.comwrote in
          >message news:E9270B7B-460E-42A1-9C8C-921CB012001F@mi crosoft.com...
          >>
          >>I'm a recent convert to VB.net from VB6...and I can't believe they
          >>don't support control arrays. I have an app that uses a ton of
          >>Control Arrays...mostly labels and text boxes.
          >>>
          >>I need some guidance on how to proceed...inclu ding any third party
          >>tools.
          >>>
          >>Thanks
          >>>
          >
          >

          Comment

          Working...