Visual Basic 2010 Creating a Control Array

后端 未结 5 1226
你的背包
你的背包 2020-12-21 09:03

How can I make a control array? Or the equivalent.

I am used to Visual Basic 6 which presents the option of whether to create a control array when I copy and paste a

相关标签:
5条回答
  • 2020-12-21 09:24

    You can act only through code. For example:

    Dim c() As TextBox
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim j As Integer
        For j = 0 To 10
            ReDim Preserve c(j)
            c(j) = New TextBox
            c(j).Name = "txt" & j
            c(j).Parent = Me
            c(j).Top = j * c(j).PreferredHeight + 2
            c(j).Tag = j
            c(j).Visible = True
            AddHandler c(j).KeyPress, AddressOf TestKeyPress
        Next
    End Sub
    
    Public Sub TestKeyPress(source As Object, e As KeyPressEventArgs)
        Dim index As Integer
        index = CInt(source.tag)
        If index >= 5 Then
            If e.KeyChar > "9" Or e.KeyChar < "0" Then
                e.Handled = True
            End If
        Else
            If e.KeyChar <= "9" And e.KeyChar >= "0" Then
                e.Handled = True
            End If
        End If
    End Sub
    

    This will create eleven text boxes assigning to all the same event handler.

    The TAG property is used to store and retrieve the idex of the text box.

    0 讨论(0)
  • 2020-12-21 09:24

    there are three ways to create control array.

    1. assign the same name to more then one control.
    2. copy an evangelizing control and past it into the form.
    3. set the index properly to a value that is not null.
    0 讨论(0)
  • 2020-12-21 09:25

    Control arrays are long gone. Object-oriented methods are now the law of the land for VB.

    You need to use a collection of some sort, such as a list.

    0 讨论(0)
  • 2020-12-21 09:26

    Another implicit feature of the control arrays that is easily overlooked is the association of numeric indexes with each element of the array. The indexes can be any positive int as long as they areunique in the collection; they do not have to be sequential. Consequently the array is more like a dictionary indexed by integers with each item value being an instance of a specific type of control.

    Logic in the VB6 event handlers for the control array get the value of the index along with the attributes of the event to be handled. Logic in the handler typically uses the index to determine which instance of the control was raising the event.

    .NET event handlers are quite different. You will typically get a reference to a specific control instance and an instance of a specific event object with the attributes of the event to be handled. You will NOT get the index.

    Also, VB6 apps sometimes have logic that iterates/manipulates the control array as an array.

    In our default translations, we try to support legacy VB6 logic that depends explicitly on the control array and its indexes. We rewrite control arrays as a group of individual control instances and then we add them to a generic OrderDictionary<int, controlType> during form initialization. The individual controls subscribe to the events and we can use the collection to lookup the index given a control instance or to iterate/manipulate the items in the "array". If you do not explicitly need the index or the array, you can get rid of the collection.

    Dynamically adding controls is more work now -- it was conceptually like adding an item to the control array and supported with a single statement in VB6 (Load control). As far as I know, in .NET you need to clone a control, deep copy the properties explicitly, and hook up the event handlers explicitly. It can be generalized using reflection and other moderately advanced techniques -- but it sure is not simply calling "load control". If there is an easier way to do this in .NET I would love to hear about it. FWIW: we translate to a helper function.

    0 讨论(0)
  • 2020-12-21 09:42

    A control array in VB6 solely existed to allow a single event handler for multiple controls of same type.

    You do this in VB.NET by putting the controls into the Handles clause:

    private sub Button_Click(...) Handles Command1.Click, Command2.Click, Command3.Click
    
    end sub
    
    0 讨论(0)
提交回复
热议问题