VB.NET help needed - refer to a control by an integer

dullard

Elite Member
May 21, 2001
26,200
4,871
126
I'm starting to make the switch from VB6 to VB.NET. I have one big headache so far: lack of control arrays. Advice and articles that I read try to give you a way to mimic control arrays, but that just seems bad practice to do so (and they haven't worked for me either).

My biggest problem is trying to work with many controls on a form. For example, I need 500 textboxes with text that depends on data in a computer file. In VB6 that took only 4 lines to do a loop, read the file, and update the textbox's text. How do I do it in .NET?

So far, my searches have come up with things like this:
Code:
Put calls to this in a loop:
 
Sub changeText(ByVal str As String, ByVal txt As String)
  Dim ctl As Object
 
  For Each ctl In Controls
    If TypeOf ctl Is Button Then
      If DirectCast(ctl, Button).Name = str Then
        DirectCast(ctl, Button).Text = txt
      End If
    End If
  Next
End Sub
But that too just gives me errors or warnings. Usually, it is something like "Object reference not set to an instance of an object." But depending on the exact use, I've seen many other problem messages.

Is there a proper way to refer to a control by number such as TextBox1, TextBox2, TextBox3, ..., TextBox500? I'm using VB 2010 Express if it matters.

Thanks
 
Last edited:

uclabachelor

Senior member
Nov 9, 2009
448
0
71
I'm starting to make the switch from VB6 to VB.NET. I have one big headache so far: lack of control arrays. Advice and articles that I read try to give you a way to mimic control arrays, but that just seems bad practice to do so (and they haven't worked for me either).

My biggest problem is trying to work with many controls on a form. For example, I need 500 textboxes with text that depends on data in a computer file. In VB6 that took only 4 lines to do a loop, read the file, and update the textbox's text. How do I do it in .NET?

So far, my searches have come up with things like this:
Code:
Put calls to this in a loop:
 
Sub changeText(ByVal str As String, ByVal txt As String)
  Dim ctl As Object
 
  For Each ctl In Controls
    If TypeOf ctl Is Button Then
      If DirectCast(ctl, Button).Name = str Then
        DirectCast(ctl, Button).Text = txt
      End If
    End If
  Next
End Sub
But that too just gives me errors or warnings. Usually, it is something like "Object reference not set to an instance of an object." But depending on the exact use, I've seen many other problem messages.

Is there a proper way to refer to a control by number such as TextBox1, TextBox2, TextBox3, ..., TextBox500? I'm using VB 2010 Express if it matters.

Thanks

Use an arraylist. You can add any object to the arraylist.
 

Tencntraze

Senior member
Aug 7, 2006
570
0
0
Your code actually works fine in a quick test I threw together, since the code itself looked fine. What's the error that you're getting?

If you don't want to have to loop through controls (don't forget to recursively check the Controls collection of Panels, GroupBoxes, etc), you could always keep a dictionary of every control, keyed by the name of the control. It would probably be pretty tedious to go this route if there are that many controls, but if you want to use some recursion you could loop through all controls once to build the dictionary and then use that, avoiding unnecessary repeated recursion.

Edit: Minor note, you should pass those strings ByRef into the method call.
 
Last edited:

KLin

Lifer
Feb 29, 2000
30,950
1,076
126
Perhaps there's a better solution than populating 500 different textbox controls? Maybe populate a listbox with the text?
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
I ran your code in Visual Studio 2008 and it ran fine. Managing 500 textboxes though isn't the best option. I think using the listbox or grid would be better options. Load the lines of the textbox into a string collection and bind it to the grid.
 

clamum

Lifer
Feb 13, 2003
26,256
406
126
Edit: Minor note, you should pass those strings ByRef into the method call.
Could you explain this further? I'm genuinely curious... I thought you use ByRef if you wanted to be able to change the object/parameter being passed.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
Byval copies the data over the stack (a copy of the source variable) which can be slow with alot of data. Byval copies a pointer over the stack (a reference to the source variable) which is 4 bytes or 8 bytes depending on 32-bit/64-bit program. Which is why when you pass by ref, it changes the original variable rather than a copy of the source variable.
 

Ichinisan

Lifer
Oct 9, 2002
28,298
1,236
136
byval copies the data over the stack (a copy of the source variable) which can be slow with alot of data. byref copies a pointer over the stack (a reference to the source variable) which is 4 bytes or 8 bytes depending on 32-bit/64-bit program. Which is why when you pass by ref, it changes the original variable rather than a copy of the source variable.

ftfy (?)
 

Kyanzes

Golden Member
Aug 26, 2005
1,082
0
76
I'm not sure why would you need like 500 textboxes but I'm suspecting that perhaps you are trying to create some sort of an itemized list, perhaps with a scrollbar.

If that's the case, you don't need to create literally 500 textboxes. You need to create a control that always displays the number of textboxes required to be present on the screen but does not actually create 500 instances.

This method of displaying stuff is not very efficient:
TextBox1 ' off screen
---------
TextBox2 ' visible on screen
TextBox3 ' visible on screen
TextBox4 ' visible on screen
---------
TextBox5 ' off screen
TextBox6 ' off screen
TextBox7 ' off screen
...


(In this case) you only need 3 textboxes and a buffer to feed them data.

If your controls differ in size then simply add a few controls at the top and the bottom (figure out the min/max vertical size of the controls) so no blank space would appear at the edges.
 

dullard

Elite Member
May 21, 2001
26,200
4,871
126
I'm not sure why would you need like 500 textboxes but I'm suspecting that perhaps you are trying to create some sort of an itemized list, perhaps with a scrollbar.
I over-simplified so that the post wouldn't get lost in fine details. You are pretty close though. It is a scrolling list, but the 500 was AFTER your simplification, otherwise it would be thousands.

Just imagine something like Excel, several scrolling rows and columns, but with buttons on screen associated with each cell for addition, deletion, descriptions, etc. The numbers of unique items on the screen all at the same time add up quickly. They aren't all textboxes, but I can handle the coding for buttons, images, labels, etc and just said textboxes in my post.

I haven't yet worked on this, I have been sidetracked for the past week. Thanks for the responses though.