|
-
Jul 5th, 2004, 05:56 PM
#1
Thread Starter
PowerPoster
What's the best way to iterate through multiple controls? (Resolved)
Hi all,
I anticipate having to build a form with well over 100 controls, mainly labels that will emulate LED's. I need some way to iterate through many controls at a time, but I'm really not getting anywhere 
I was thinking of some kind of for each...next loop, but a search of these forums produced zero results, and none of my VB .NET books mention it other than in passing. Presumably it isn't the way to go about this. Plus I spent several hours yesterday playing about in VS and I kept getting error after error, even using code copied and pasted from MSDN Library, so I 'm obviously not understanding this at all.
So, can any of you good people point me in the right direction here please?. If there's one thing that I miss it's the old control array in VB6 - at least I knew how to use that.
Last edited by SuperSparks; Jul 6th, 2004 at 07:04 PM.
Nick.
-
Jul 5th, 2004, 06:30 PM
#2
You can create control arrays...
VB Code:
Private MyLabels() As Label = New Label() {Label1, Label2, Label3}
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
For Each lbl As Label In MyLabels
MsgBox(lbl.Text)
Next
End Sub
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jul 5th, 2004, 07:42 PM
#3
PowerPoster
HI,
Maybe I'm missing the point but why does he need a Control Array? All he is asking is to iterate through a large number of controls and that has been discussed many times in this forum. He can check which type of control is being passed and, by using the Tag property, he can even isolate controls of the same type into different groups. e.g.
VB Code:
Dim ctr As Control
For Each ctr In Controls
MessageBox.Show(ctr.Name)
Next
or
VB Code:
Dim ctr As Control
For Each ctr In Controls
If TypeOf ctr Is TextBox Then
MessageBox.Show(ctr.Name)
End If
Next
or
VB Code:
Dim ctr As Control
For Each ctr In Controls
If TypeOf ctr Is TextBox Then
Select Case ctr.Tag
case = "Red"
ctr.forecolor=Color.Red
case Else
ctr.forecolor=color.White
End Case
End If
Next
HI supersparks,
I do not understand when you say thee is noting in this forum on the subject, there are dozens of threads on it. What are you searching under?
Last edited by taxes; Jul 5th, 2004 at 07:49 PM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jul 5th, 2004, 07:54 PM
#4
Thread Starter
PowerPoster
Hi Taxes, I searched under "for each" which returned zero results, and then I tried "for each next" which brought up dozens of results for for next loops. Usually I'm pretty good at searching, being a mod on another forum using vBulletin, but this one defeated me If you could suggest another term I'd be grateful, I get a lot of help from the old threads.
It's the lack of results that had me thinking that it wasn't the way to go about this.
Thanks for the help so far guys, that's given me something to get started with anyway.
I'm thinking of putting the related labels into groupboxes, will that make things easier or harder?
-
Jul 5th, 2004, 08:22 PM
#5
PowerPoster
Hi SuperSparks,
Try searching under "Looping through Controls" but only look at the VB.NET posts.
Iin theory putting the labels into groupboxes should help, but I could not iterate through controls contained in another control. I don't understand this as both forms and controls are all objects.
Why don't you just use the Tag property to group them?
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jul 5th, 2004, 09:08 PM
#6
Thread Starter
PowerPoster
Even that only found 10 threads. I'm surprised that this hasn't been discussed more.
Well I tried an example anyway, on a test form with a Groupbox and 3 NumericUpDowns that I happened to have handy.
VB Code:
Dim nud As NumericUpDown
For Each nud In Me.GroupBox2
Next
And what I got for my pains, in Intellisense, was:
Expression is of type "System.Windows.Forms.Groupbox", which is not a collection type
I just don't know why I'm struggling so much with this, but even the examples from MSDN Library, cut & pasted throw exceptions. Do I need to import something?
-
Jul 6th, 2004, 06:32 AM
#7
PowerPoster
Hi,
As I told you, I could not iterate through the controls contained in another control.
With acknowledgements to BrownMonkey, if you want to lightup a control, say by setting it's backcolor to a predetermined colour, then all you have to do is set the Tag property of the control to the desired VB.NET color ( say enter "Red" in textbox1.Tag) and then:
VB Code:
Dim ctr As Control
Dim sCol As String
For Each ctr In Controls
If TypeOf ctr Is TextBox Then
IfCstr(ctr.Tag)<>"" then
sCol = CStr(ctr.Tag)
ctr.BackColor = Color.FromName(sCol)
End If
End If
Next
In this context, the colour entered in the Tag property is case insensitive.
Is that what you are looking for?
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jul 6th, 2004, 07:30 AM
#8
I'd go with crptcblade's idea and put them in the array, much easier to change all the properties at once and stuff.
I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)
-
Jul 6th, 2004, 07:39 AM
#9
PowerPoster
Originally posted by Ideas Man
I'd go with crptcblade's idea and put them in the array, much easier to change all the properties at once and stuff.
I'd be interested to know why you consider it easier.
I think you would use virtually the same code in either case and you don't bother with setting up the array.
What am I missing, please? (apart from a brain)
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jul 6th, 2004, 07:52 AM
#10
PowerPoster
Hi,
With acknowledgements to CyberHawke,
VB Code:
Dim ctl1 As Control, ctl2 As Control
For Each ctl1 In Me.Controls
If ctl1.Name = "Panel1" Then
For Each ctl2 In ctl1.Controls
If ctl2.Name = "Button1" Then MessageBox.Show("Found It!")
Next
End If
Next
will let you iterate through contained controls.
EDIT, CyberHawke has just come up with
VB Code:
Dim ctl As Control
For Each ctl In Me.Panel1.Controls
MessageBox.Show(ctl.Name)
Next
Last edited by taxes; Jul 6th, 2004 at 07:59 AM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jul 6th, 2004, 08:36 AM
#11
PowerPoster
I posted code that does something like you are asking:
http://www.vbforums.com/showthread.p...hreadid=287282
-
Jul 6th, 2004, 03:22 PM
#12
Thread Starter
PowerPoster
Many thanks people, at last I'm getting somewhere 
Although crptcblade's idea will undoubtedly be very useful for other projects, I don't fancy it much for this one, if only to save my poor old fingers from all that typing 
Those last two examples from Taxes/ CyberHawke worked perfectly, I think I'll be able to do something with those.
And I'll do some investigating of tags, those are a new one for me and I know nothing about them.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|