HighTechTalks DotNet Forums  

Really easy question about looping through controls

VB.net Controls microsoft.public.dotnet.languages.vb.controls


Discuss Really easy question about looping through controls in the VB.net Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Jonathan Brown
 
Posts: n/a

Default Really easy question about looping through controls - 02-29-2008 , 06:46 PM






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.


Reply With Quote
  #2  
Old   
RobinS
 
Posts: n/a

Default Re: Really easy question about looping through controls - 03-01-2008 , 02:31 AM






Here's an example of how to loop through all the controls on a form. You
have to do this recursively in case you have (for example) panels with
controls in them, or option groups, or something like that.

This example adds event handlers to all the textboxes and comboboxes so when
the user tabs into them, or tabs out of them, it handles the event. (I'm
backcoloring the entry in my event handlers).

Private Sub AddEventHandlers(ByVal ctrlContainer As Control)
For Each ctrl As Control In ctrlContainer.Controls
If TypeOf ctrl Is TextBox _
OrElse TypeOf ctrl Is ComboBox Then
AddHandler ctrl.Enter, AddressOf ProcessEnter
AddHandler ctrl.Leave, AddressOf ProcessLeave
End If
'If control has children, call this function recursively
If ctrl.HasChildren Then
AddEventHandlers(ctrl)
End If
Next
End Sub

Instead of adding event handlers, you can check the tag or name or whatever
and do what you're trying to do.

Hope this helps.
RobinS.
GoldMail, Inc.
-------------------------------------
"Jonathan Brown" <JonathanBrown (AT) discussions (DOT) microsoft.com> wrote

Quote:
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.



Reply With Quote
  #3  
Old   
Jonathan Brown
 
Posts: n/a

Default Re: Really easy question about looping through controls - 03-01-2008 , 10:09 AM



So I can't specify which controls on a form to loop through? I couldn't use
say, the first part of the controls' names and then append a variable string
at the end to specify a specific control? I have to loop through all of the
controls on a form given their type.

"RobinS" wrote:

Quote:
Here's an example of how to loop through all the controls on a form. You
have to do this recursively in case you have (for example) panels with
controls in them, or option groups, or something like that.

This example adds event handlers to all the textboxes and comboboxes so when
the user tabs into them, or tabs out of them, it handles the event. (I'm
backcoloring the entry in my event handlers).

Private Sub AddEventHandlers(ByVal ctrlContainer As Control)
For Each ctrl As Control In ctrlContainer.Controls
If TypeOf ctrl Is TextBox _
OrElse TypeOf ctrl Is ComboBox Then
AddHandler ctrl.Enter, AddressOf ProcessEnter
AddHandler ctrl.Leave, AddressOf ProcessLeave
End If
'If control has children, call this function recursively
If ctrl.HasChildren Then
AddEventHandlers(ctrl)
End If
Next
End Sub

Instead of adding event handlers, you can check the tag or name or whatever
and do what you're trying to do.

Hope this helps.
RobinS.
GoldMail, Inc.
-------------------------------------
"Jonathan Brown" <JonathanBrown (AT) discussions (DOT) microsoft.com> wrote in message
news:AF42C5B8-A50C-4BB0-BDD8-46EFF39478EE (AT) microsoft (DOT) com...
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.




Reply With Quote
  #4  
Old   
Jonathan Brown
 
Posts: n/a

Default Re: Really easy question about looping through controls - 03-01-2008 , 10:55 AM



What does it mean, if control has children? (try to keep the answer as clean
as possible)

"RobinS" wrote:

Quote:
Here's an example of how to loop through all the controls on a form. You
have to do this recursively in case you have (for example) panels with
controls in them, or option groups, or something like that.

This example adds event handlers to all the textboxes and comboboxes so when
the user tabs into them, or tabs out of them, it handles the event. (I'm
backcoloring the entry in my event handlers).

Private Sub AddEventHandlers(ByVal ctrlContainer As Control)
For Each ctrl As Control In ctrlContainer.Controls
If TypeOf ctrl Is TextBox _
OrElse TypeOf ctrl Is ComboBox Then
AddHandler ctrl.Enter, AddressOf ProcessEnter
AddHandler ctrl.Leave, AddressOf ProcessLeave
End If
'If control has children, call this function recursively
If ctrl.HasChildren Then
AddEventHandlers(ctrl)
End If
Next
End Sub

Instead of adding event handlers, you can check the tag or name or whatever
and do what you're trying to do.

Hope this helps.
RobinS.
GoldMail, Inc.
-------------------------------------
"Jonathan Brown" <JonathanBrown (AT) discussions (DOT) microsoft.com> wrote in message
news:AF42C5B8-A50C-4BB0-BDD8-46EFF39478EE (AT) microsoft (DOT) com...
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.




Reply With Quote
  #5  
Old   
Jack Jackson
 
Posts: n/a

Default Re: Really easy question about looping through controls - 03-01-2008 , 06:25 PM



On Fri, 29 Feb 2008 16:46:00 -0800, Jonathan Brown
<JonathanBrown (AT) discussions (DOT) microsoft.com> wrote:

Quote:
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


Reply With Quote
  #6  
Old   
RobinS
 
Posts: n/a

Default Re: Really easy question about looping through controls - 03-03-2008 , 01:52 AM



Not really. You can do control arrays like you could in VB6. I miss those a
lot. Unless you have thousands of controls on the form, the code I provided
won't take very long. You can check for the control being a textbox, andalso
starting with a specific prefix or something like that.

RobinS.
GoldMail, Inc.
-------------------------------------------
"Jonathan Brown" <JonathanBrown (AT) discussions (DOT) microsoft.com> wrote

Quote:
So I can't specify which controls on a form to loop through? I couldn't
use
say, the first part of the controls' names and then append a variable
string
at the end to specify a specific control? I have to loop through all of
the
controls on a form given their type.

"RobinS" wrote:

Here's an example of how to loop through all the controls on a form. You
have to do this recursively in case you have (for example) panels with
controls in them, or option groups, or something like that.

This example adds event handlers to all the textboxes and comboboxes so
when
the user tabs into them, or tabs out of them, it handles the event. (I'm
backcoloring the entry in my event handlers).

Private Sub AddEventHandlers(ByVal ctrlContainer As Control)
For Each ctrl As Control In ctrlContainer.Controls
If TypeOf ctrl Is TextBox _
OrElse TypeOf ctrl Is ComboBox Then
AddHandler ctrl.Enter, AddressOf ProcessEnter
AddHandler ctrl.Leave, AddressOf ProcessLeave
End If
'If control has children, call this function recursively
If ctrl.HasChildren Then
AddEventHandlers(ctrl)
End If
Next
End Sub

Instead of adding event handlers, you can check the tag or name or
whatever
and do what you're trying to do.

Hope this helps.
RobinS.
GoldMail, Inc.
-------------------------------------
"Jonathan Brown" <JonathanBrown (AT) discussions (DOT) microsoft.com> wrote in
message
news:AF42C5B8-A50C-4BB0-BDD8-46EFF39478EE (AT) microsoft (DOT) com...
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.





Reply With Quote
  #7  
Old   
RobinS
 
Posts: n/a

Default Re: Really easy question about looping through controls - 03-03-2008 , 01:56 AM



LOL. You want to read through all of the controls on the form.

What if it's a panel that has textboxes inside it, or an option group with
radio buttons in it? You want to be able to see those. You call my routine
below and pass in the form, like "AddEventHandlers(FavoriteForm)". If all of
your controls are directly on the form, it will not recurse.

So the first level would look at all controls on the form directly. If it
hits a panel, it calls itself to access the controls on the panel. If that
panel has a panel in it, it would call itself to access the controls in THAT
panel. And so on. Each time it finishes all the controls in a container
(like a panel), it goes back up a level. Put the code in and trace through
it and you'll see how it works.

It's the only way you can look through all of the controls; I know I've seen
code for doing it as one level, but it's much more complicated.

Does that make sense?

RobinS.
GoldMail, Inc.
-------------------------------------
"Jonathan Brown" <JonathanBrown (AT) discussions (DOT) microsoft.com> wrote

Quote:
What does it mean, if control has children? (try to keep the answer as
clean
as possible)

"RobinS" wrote:

Here's an example of how to loop through all the controls on a form. You
have to do this recursively in case you have (for example) panels with
controls in them, or option groups, or something like that.

This example adds event handlers to all the textboxes and comboboxes so
when
the user tabs into them, or tabs out of them, it handles the event. (I'm
backcoloring the entry in my event handlers).

Private Sub AddEventHandlers(ByVal ctrlContainer As Control)
For Each ctrl As Control In ctrlContainer.Controls
If TypeOf ctrl Is TextBox _
OrElse TypeOf ctrl Is ComboBox Then
AddHandler ctrl.Enter, AddressOf ProcessEnter
AddHandler ctrl.Leave, AddressOf ProcessLeave
End If
'If control has children, call this function recursively
If ctrl.HasChildren Then
AddEventHandlers(ctrl)
End If
Next
End Sub

Instead of adding event handlers, you can check the tag or name or
whatever
and do what you're trying to do.

Hope this helps.
RobinS.
GoldMail, Inc.
-------------------------------------
"Jonathan Brown" <JonathanBrown (AT) discussions (DOT) microsoft.com> wrote in
message
news:AF42C5B8-A50C-4BB0-BDD8-46EFF39478EE (AT) microsoft (DOT) com...
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.





Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.