Dynamically refer to textbox's

sabe

Member
Joined
Aug 8, 2006
Messages
17
Programming Experience
Beginner
Hi everyone

Does anyone know how to refer dynamically to texbox's, eg:

2 textbox's named Txt1 and Txt2

a=1
Txt(a).text = "Hello"

the above abviously does not work, as I am sure this is how vb6 did work.

Cheers

Sabe
 
You are talking about control arrays from vb6? No there's no such thing in .net. You have a textbox you want to manipulate? just use it's name, also the form/panel/groupbox and all other container controls have a control collection. Usually a property called 'Controls'
 
Yes a control array, but this no longer exists ?? What do you mean under controls, have you an example ?
 
Sorry meant to say that was a simple example before, it is a little more complex that I am trying to acheive.
 
I have written a prg that pulls a timetable for students in a school from a real time registration system, I have currently hardcoded to each textbox using case statements but now wish to clean my code.

Basically I have a textbox named Txt11 (standing for day1 (monday), period 1) these details are taken from a sql db, so I do :

Select case DayNumber
Case 1 'Monday
Select Case Period
Case 1
Txt11.text=Classname & Teacher etc . .
'Would like to
Txt(Daynumber & Period).text=ClassName & Teacher
case 2
etc . .
end select
Case 2 'Tuesday
etc . .
End Select

But will then probably do a loop and lose all the case statements
 
ok i think i get it. It may be possible to use a hashtable here. You can use key and value pairs. So you would create your new hashtable add the textboxes with the key you would like to represent it by. eg...

VB.NET:
Dim SchoolHashTable as new hashtable
 
SchoolHashTable.Add("DayNumber",TxtDayNumber)
etc..
you can then reference the textbox in the hashtable by it's key. Run a loop through the hashtable get the info that you want and put it into the desired textbox. With me?
 
I'd suggest not using an integer to represent days. There already exists the DayOfWeek enumeration, which is a better option. Also, there's no need to use a collection. You can simply create a normal array and then refer to each control by its index in that. Given that the underlying type of an enumeration is Integer you can even refer to them by a day of the week:
VB.NET:
Private dayFields As TextBox()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.dayFields = New TextBox() {Me.sundayText, _
                                  Me.mondayText, _
                                  Me.tuesdayText, _
                                  Me.wednesdayText, _
                                  Me.thursdayText, _
                                  Me.fridayText, _
                                  Me.saturdayText}
End Sub
You then get the TextBox that corresponds to a particular day simply by indexing the array with a DayOfWeek value, e.g.
VB.NET:
Dim dayField As TextBox = Me.dayFields(DayOfWeek.Thursday)
 
Note the order that I used for the TextBoxes in the array. That's important because the members of the DayOfWeek enumeration are Sunday to Saturday with values 0 to 6 respectively. If you need to refer to the days with different values for some reason then I'd still suggest using the DayOfWeek enumeration and writing a method to translate, or else define your own enumeration for the days of the week, e.g.
VB.NET:
Public Enum DayOfWeek
    Monday = 1
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
End Enum
It's preferable to stick to the existing type if possible though.
 
Cheers guys

Some really good advise, I haven't seen a hashtable before - is this new? Think I will have a play with that as I can see it valuable for other programs, in this case I am trying to use the array, although it is coming up with too many arguments on this part:

(Me.sundayText(although my text box being named Txt10), _
Me.mondayText, _
Me.tuesdayText, _
Me.wednesdayText, _
Me.thursdayText, _
Me.fridayText, _
Me.saturdayText}

Also, any good info for printing Datagridviews?
 
Nearly there, still no sure on the printing as well

The hashtable works (with a but - see lower):

for count = 0 to number of lessons pulled for student

SchoolHashTable.add(CInt(DayName) & PeriodID, ClassName & vbcrlf & Teacher etc . . )

next

then I thought it would be:

Txt11.text=SchoolHashTable.item("11")
Txt12.text=SchoolHashTable.item("12")
etc. .

BUT, here would be nice to use the array to say something like:

For Hashcount = 10 to Schoolhashtable.count + 10
Txt(Hashcount).text=Schoolhashtable(Hashcounter.thstring)
next

but I cannot get the array to work
 
So the problem is here that you can't use a variable when refering to controls in that manner. I've only just got up but from what i cansee you need to run a couple of loops to fill the textboxes with the data you want. It would go something like (but not exactly) this....

VB.NET:
Dim tb As TextBox
'Start as loop through the hashtable
For Each key As String In Me.Hashtable.Keys
 
'Start a loop through the forms controls
For Each ctrl As Control In Me.Controls
 
'See if we've got a textbox
If TypeOf ctrl Is TextBox Then
 
'If it is, turn our local variable (tb) into the one we want
tb = DirectCast(ctrl, TextBox)
 
'substring here will pick out the number at the end of yuor textboxes name
so you'll need to to change it to 3 if all you textboxes are called 'txt..'
'Check to see if our key matches the last to numbers of the textboxes name.
If key = tb.Name.Substring(7) Then
 
' It does so put it in a messagebox to see if we got it right
MsgBox(CStr(Me.hh(key)))
 
'It doesn't so round the loop we go again.
End If
End If
Next
Next


Point of note though this kind of thing works but if there are a lot of records then it's gonna be a bit slow.
 
Think I will stick with :
Txt11.text=SchoolHashTable.item("11")
Txt12.text=SchoolHashTable.item("12")

as it seems quite fast
 
Last edited by a moderator:
Yes a control array, but this no longer exists ?? What do you mean under controls, have you an example ?

Of course a control array exists:

Dim boxes(0 to 1) as TextBox
boxes(0) = TextBox1
boxes(1) = TextBox2


the difference is you now have to make the control array yourself..



-

If the concept of Hashtables is new to you ,you should read up about them. They are a powerful data storage container, but there is a better one, which hashtable is based on

If you take a look at System.Colelctions.Generics.Dictionary you will see it is broadly the same as a hashtable, except you can specify what ytpes of objects you wish to put in it.

By default, a Hashtable uses String for the key and Object for the data. This is fine, but you have to cast the object back into the specific type you put in. Dictionary on the other hand can be typed when you make it:

Dim textboxes as New Dictionary(Of String, Textbox)

Now, you can store your textboxes into the dictionary, with strings for lookups:

textboxes.Add("day1period1", tbxD1P1)
textboxes.Add("day1period2", tbxD1P2)
textboxes.Add("day1period3", tbxD1P3)


when you say:
textboxes.Item("day1period1")

it behaves like a textbox, because it IS a textbox, so you can:
textboxes.Item("day1period1").Text = "this is the text in tbxD1P1"


This is very similar to what youre asking with:
Txt(a).Text = "hello"


-


If you were doing this with a hashtable, as per what vis781 has shown you, you would do this:
DirectCast(textBoxesHT.Item("day1period1"), TextBox).Text = "this is the text in tD1P1"

it looks similar to the dictionary, with the addition of DirectCast(object to cast, type to cast into)

we can avoid needing to write that, by using a typed dictionary
.


-

Note also we could have done this with a regular array as it has a fixed size. suppose there are 5 days in the week and 10 perioods in the day:

Dim theWeek(1 to 5, 1 to 10) as TextBox
'note we have to link our text boxes on the form into this array
theWeek(1,1) = tbxD1P1
theWeek(1,2) = tbxD1P2
theWeek(1,3) = tbxD1P3

-

finally, i must ask why youre doing this with ~50 text boxes.. did you not see the datagridview control? it allows you to work with a grid of editable cells (that are, in essence textboxes), like in an excel spreadsheet and is much easier than all this messing around with big blocks of text boxes...
 
Back
Top