Re: Really easy question about looping through controls
- From: Jack Jackson <jjackson@xxxxxxxxxxxxxxxx>
- Date: Sat, 01 Mar 2008 16:25:57 -0800
On Fri, 29 Feb 2008 16:46:00 -0800, Jonathan Brown
<JonathanBrown@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
I have a form that has a cluster of 9 textboxes that are named txtMatrix1_0,
txtMatrix1_1, txtMatrix1_2 ... txtMatrix1_8
I want to loop through those nine textboxes, convert their values from
string to
byte, and then assign the value of each textbox to an array. The code could
be something like the following without using a loop:
dim arMatrix(8) as Byte
arMatrix1(0) = CByte(txtMatrix1_0.text)
arMatrix1(1) = CByte(txtMatrix1_1.text)
...
arMatrix1(8) = CByte(txtMatrix1_8.text)
But is there a way to do this in a for loop? Something like the following:
dim X as integer
dim ctl as new control
for x = 0 to 8
ctl.name = "me.txtMatrix1_" + CStr(x)
ctl.text = CByte(ctl.text)
arMatrix1(x) = CByte(str.text)
Next x
I'm using the Option Strict On line so I have to convert my data types
properly. I'm sure I'm doing this completely wrong and it's really driving
me nuts.
Appreciate any input.
There are a couple of approaches you could take.
1. Create an array with the control references in it:
Dim aTextbox(8) As Textbox
aTextbox(0) = txtMatrix1_1
......
Then loop through the array.
2. Loop through all of the controls on the form looking for your
textboxes:
For Each ctl As Control in Me.Controls
Dim txtbox as Textbox = TryCast(ctl, Textbox)
If txtbox IsNot Nothing Then
If txtbox.Name.Length > 11 AndAlso _
txtbox.Name.Substring(0, 10) = "txtMatrix1_" Then
Dim indx As Integer = _
Convert.ToInt32(txtbox.Name.SubString(11))
arMatrix1(indx) = ...
End If
End If
Next
.
- Prev by Date: Re: Really easy question about looping through controls
- Next by Date: Re: Really easy question about looping through controls
- Previous by thread: Re: Really easy question about looping through controls
- Index(es):
Relevant Pages
|
Loading