Runtime controls (controling textbox)

Hi,

I have a question about controling textboxes created runtime.
Is there any way of accessing directly to particular textbox created runtime (ex. textbox3)
now i'm going trough all controls on panel (textbox is on it) and searching for particular control name. That way i get contorl index.

code i'm using for creating them:

While items < i
index = New TextBox
With index
.Name = "index" & (items + 1).ToString
.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(238, Byte))
.ReadOnly = False
.Size = New System.Drawing.Size(200, 22)
.TabIndex = 1 + items
.TabStop = True
.Location = New Point(txtLocationX, (LocationY + (items * 40)))
.Text = ""
.AccessibleName = "index" & (items + 1).ToString
.BorderStyle = BorderStyle.Fixed3D
End With
Me.PanelItems.Controls.Add(index)
items+=1
End While





Answer this question

Runtime controls (controling textbox)

  • gilabite

    This would appear to be you writing your own index table to track this information. I suppose you could use something like a hashtable or something to hold this information.

    Add an entry into the hashtable when you create the control and then when you need to find the appropriate index - you search for the Key (which is the control name) to get the index.

    Hash Table in VB.NET - http://abstractvb.com/code.asp A=1025

    Ultimately its just an indexed lookup table instead of you having to enumerate through the controls collection.


  • houseofmusic

        Dim button1 As New Button
        ' declaring a variable does not set the name property!!
        button1.Name = "button1" 
        Dim button2 As New Button
        button2.Name = "button2"
        Me.Controls.Add(button1)
        Me.Controls.Add(button2)
        ' me.controls is a ControlCollection. In vb2005, you can access an item 
        ' by the key - which is the name...
        Me.Controls("button2").Location = New Point(100, 100)

    If it is not vb2005, then you have to loop through all controls...


  • TommyH

    thanx for help!

    unfortunately, I'm creating app in VB.NET 2003.
    To now i have been looping trough all controls in control collection, but I was hopeing that there is some batter way of doing this.

    But I have think of something... :)
    I'll create one dataTable or 2d Array in witch I'll store information about control indexes.
    like:
    ID |textbox | textbox |
    |name | index |
    1 |txt1 | 5 |
    2 |txt2 | 9 |

    so this way I have created my simple control collection :)
    So I know a index of every runtime created control




  • Runtime controls (controling textbox)