0

I don't do a bunch of work in VB.net, however I'd like to do some things that I do in other languages.

I have a form that has a bunch of rows of text boxes. Instead of creating setters for each text box of each row, I wish to have a dynamic setter, which can set the value based on a string value. E.g. An text box in row one has a name like tbName1, while row two has tbName2. Notice the pattern, where the number changes at the end.

I believe I'll need to iterate through all components on the form, get a match of the name and verify the component type is an text box. If both match, then set the value.

Solution needs to work with .NET 3.5 framework.

halfer
  • 19,824
  • 17
  • 99
  • 186
James Oravec
  • 19,579
  • 27
  • 94
  • 160
  • Ok, maybe I didn't understand the question properly. Are you asking how to create such rows of TexBox objects, or are you asking how to access them by name ? – Luc Morin Jan 06 '14 at 22:21

4 Answers4

1

Windows Forms (assuming it since you didn't specify) expose a property named Controls which can be indexed by either in Integer or a string.

You can thus access a TextBox on a form like this:

MyForm.Controls(TxtBoxName)

Hope this helps

Luc Morin
  • 5,302
  • 20
  • 39
1

If you want to change the text of let say tbName3 you could simply do:

Me.Controls.Item(String.Format("tbName{0}", 3)).Text = "Test"

In a loop:

For i As Integer = 1 To 10
    Me.Controls.Item(String.Format("tbName{0}", i)).Text = String.Format("I'm textbox #{0}!", i)
Next
Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64
0

Create a list of the relevant control names:

 Friend _cList as List(of String)
 For n as integer = 0  to 5
     _cList.Add("tbName" & n.ToString)
 Next n

Then to reference one:

 theForm.Controls(_clist(theIndex)).Text = "New text"

If they are actually on panels and the like, then change the method to track/store a List(of Control) (textboxes are Controls, not Components, by the way):

Friend _cList as List(of Control)

If you will be deleting them from the form, be sure to remove them from the List first or they dont dispose properly.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
0

Thanks for the suggestions guys, gave you both up votes for your suggestions, the following is what I created that does both the iterating and type checking.

    Sub SetTextBoxText(TextBoxName As String, Value As String)
        For Each control As Control In Controls
            If control.Name = TextBoxName Then
                If control.GetType.ToString = "System.Windows.Forms.TextBox" Then
                    control.Text = Value
                End If
            End If
        Next
    End Sub
James Oravec
  • 19,579
  • 27
  • 94
  • 160
  • 1
    almost everything there is not needed. Simply `Controls(TextBoxName).Text = Value` does the same. if you know the name there is no reason to iterate because control names must be unique. There is certainly no reason to test the type *after* you find by name since control names must be unique. In cases where you do have to find a control, `For Each tb As TextBox In Controls()(Of TextBox)` prevents having to test the type. A `List(Of T)` also saves iterating or finding ones you know about – Ňɏssa Pøngjǣrdenlarp Jan 07 '14 at 00:01