Re: Really easy question about looping through controls



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
.



Relevant Pages

  • RE: New to VBA, Search for hole in numeric value using string as a cri
    ... You are setting the control to true and never checking for it's ... Then loop through those looking for the gap in numbers. ... > Dim tcrNum As Integer ... > Dim tcrPull As Integer ...
    (microsoft.public.access.formscoding)
  • RE: Fastest way to append text to a TextBox
    ... red-lines my CPU and, even if I limit the TextBox to just 30KB of text, ... Or, rather, why is updating the control /so/ slow, compared to hacking ... Dim iLen As Integer = .Text.Length + sText.Length ...
    (microsoft.public.dotnet.languages.vb)
  • catch values of dynamic controls
    ... Dim page As Control ... If TypeOf ctl Is PageView Then ... If TypeOf ctl Is TextBox Then ...
    (microsoft.public.dotnet.framework.aspnet)
  • force the binding Parse property
    ... I have a form in which I have a custom textbox control that gives me a new ... Public Sub New ... Dim binding As Binding = Me.DataBindings ...
    (microsoft.public.dotnet.languages.vb.controls)
  • RE: Datagrid cancel command does not work when adding new record
    ... If I edit a record then press the cancel button this works fine. ... Dim UserID, UserName, UserNetwork, UserType As String ... ' control in each cell -- a TextBox control. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)

Loading