Looping through array of option buttons with similar name - seems not to be possible (or easy) in VB .NET
-
2012年4月13日 4:16
Hello. In VB6 I could set up code to loop through say 10 option buttons to check their values. The code would be something likie this:
For i = 1 To 10
strNumber = "_" & Cstr(i)
If frmMain.Controls("optH89Hours" & strNumber).Checked = True Then etc.
Next i
But in VB .NET it seems that if I use the Controls command as above I cannot use/check the Checked property as Control does not take that as a property of Control. It seems also that I may have to use the Find property of Control as in "Controls.Find" etc. How can I do this in VB .NET? I have a very control intensive application and I must be able to easily loop through controls and check their values without writing out 10 x the code required. Thanks for your help.
すべての返信
-
2012年4月13日 4:41
Sanseviera,
There are endless things which were not possible with VB6 and are now with VB10 and even more with the upcoming VB11.
But was there in VB6 one way, there are in VB7 and higher endless ways, you can loop through all controls, you can make yourself a control array, you can recursively go through the controls. (In the newer versions controls are placed on other controls)
Here a simple sample on our website for a recursive way which fits in my idea the best to your question.
http://www.vb-tips.com/ClearTextBox.aspx
Maybe it was easy that 99% of the ways which are now possible were not possible in VB6, but that is the way as you look to it.
Success
Cor- 編集済み Cor LigthertMVP 2012年4月13日 4:42
-
2012年4月13日 13:51モデレータ
First thing to be aware of is that VB6 is very different from VB.NET, as you have found. You often need to get out of the habit of thinking the 'VB6 way' as it often holds you back :)
There are several options at your disposal.
First, you can pre-build arrays of the controls you will be looping through. This means that the control names are irrelevant when trying to 'find' them (you can have annoying runtime bugs - incorrectly named controls placed in an array will cause a compile-time bug which is far easier to correct). For example, if you have 3 check boxes that are called FirstCheck, SecondCheck, ThirdCheck, put them in an array:
Dim CheckBoxes(2) as CheckBox
CheckBoxes(0) = FirstCheck
CheckBoxes(1) = SecondCheck
CheckBoxes(2) = ThirdCheck
You can then loop through the array whenever you need to.
An alternative, and an example that you will see most often, is to iterate through the controls collection of containers.
For Each ctl As Control In Me.Controls Dim chk As CheckBox = TryCast(ctl, CheckBox) If chk IsNot Nothing Then ' Perform actions on checkbox If chk.Checked Then Debug.WriteLine(chk.Name & ": I'm Checked!") End If Next
There are a few variations on the above code, but basically you iterate through the controls collection, finding the controls you want, and casting it to the correct type.
Note that nested containers will require you to iterate through that container to find nested controls within that container (a GroupBox for example).
Stephen J Whiteley
- 編集済み SJWhiteleyModerator 2012年4月13日 13:53
- 回答の候補に設定 John Anthony Oliver 2012年4月13日 19:04
- 回答としてマーク Sansevieria 2012年4月14日 10:18
- 回答としてマークされていない Sansevieria 2012年4月21日 9:01
- 回答の候補の設定解除 Sansevieria 2012年4月21日 9:11
-
2012年4月13日 14:48One way is to create a List of controls that contain the ones you are interested in. You can populate the list when the form loads and use it to loop through the controls. Otherwise it is a case of looping through Form1.Controls. Better to have specific types of control in the list rather than a geneic control. You can have as many lists as you want e.g. one with buttons, one with labels, one with textboxes etc.
Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute. -
2012年4月14日 11:11Hello John and Stephen. Thanks very much. I think this will the solve the problem for me. I have yet to code it all up but I foresee no problems in doing so. Sorry I was tardy in my reply btw. Your efforts are very much appreciated.
- 回答としてマーク Sansevieria 2012年4月14日 12:05
- 回答としてマークされていない Sansevieria 2012年4月21日 9:11
-
2012年4月21日 9:10
Stephen,
how do I actually do this:
Dim CheckBoxes(2) as CheckBox
CheckBoxes(0) = FirstCheck
CheckBoxes(1) = SecondCheck
CheckBoxes(2) = ThirdCheckFor example one of my option button controls is called "frmMain.optC14Hours_1" (as it is in the form frmMain).
Do I put the name of the control in quotes like this: CheckBoxes(0) = "frmMain.optC14Hours_1". I have tried just stating
CheckBoxes(0) = frmMain.optC14Hours_1
but of course the IDE throws up a syntax error.
But then if I do put it into quotes how do I then address it in the decision making code? Can you complete the acode for me above so that I can understand and use it? Thanks.
-
2012年4月21日 14:17
Stephen,
how do I actually do this:
Dim CheckBoxes(2) as CheckBox
CheckBoxes(0) = FirstCheck
CheckBoxes(1) = SecondCheck
CheckBoxes(2) = ThirdCheckFor example one of my option button controls is called "frmMain.optC14Hours_1" (as it is in the form frmMain).
Do I put the name of the control in quotes like this: CheckBoxes(0) = "frmMain.optC14Hours_1". I have tried just stating
CheckBoxes(0) = frmMain.optC14Hours_1
but of course the IDE throws up a syntax error.
But then if I do put it into quotes how do I then address it in the decision making code? Can you complete the acode for me above so that I can understand and use it? Thanks.
Where are these checkboxes? The statement to reference them using an array should be on their container. You can't refer to a Form within its class as frmMain. Us "Me" to refer to a class from within the class. An easy way to reference the checkbones in an array uses the following syntax:
Dim CheckBoxes() As CheckBox = {optC14Hours_1, CheckBox2, ..., CheckBoxN } -
2012年4月21日 17:41
To do it like you want to try using
Me.Controls.Find(ControlName, True).FirstOrDefault
That will search all of the forms controls and also the children of its controls.
- 編集済み TechNoHick 2012年4月21日 17:41
-
2012年4月22日 23:03
Hi Sanseviera,
To go from what you had.>>
For i = 1 To 10
strNumber = "_" & Cstr(i)
If frmMain.Controls("optH89Hours" & strNumber).Checked = True Then etc.
Next i
To the VB.Net way.>>
Dim cb As CheckBox
Dim foundControls() As Control
For index As Integer = 1 To 10
foundControls = Me.Controls.Find("optH89Hours" & index.ToString, False)
If Not foundControls Is Nothing And TypeOf foundControls(0) Is CheckBox Then
cb = CType(foundControls(0), CheckBox)
If cb.Checked = True Then
'Your code here.>>
End If
End If
Next
Regards,

Click this link to see the NEW way of how to insert a picture into a forum post.
Installing VB6 on Windows 7
App Hub for Windows Phone & XBOX 360 developers.
- 編集済み John Anthony Oliver 2012年4月22日 23:04
- 回答としてマーク Shanks ZenMicrosoft Contingent Staff, Moderator 2012年4月25日 9:25

