Question Index on controls??

ecaf

Member
Joined
Dec 1, 2008
Messages
20
Programming Experience
1-3
Does anyone know if you can index controls now in vb.net?
You could do it in VB6 - where you would have 6 radio buttons and if you copied the 1st it would ask you did you want to index the control.
So you would have optName(0), optName(1), optName(2).... optName(5)

It was useful to do a select case on the controls or else do a For loop on them either.

I can't see anyway to do this in vb.net now, is there a way to do it? Or a workaround for this?

(sorry for posting this in VS.net general - I made a mistake earlier) :eek:
 
Now each container control, like Form or Panel for example, has a Controls property that returns a collection of the controls it contains, this collection can be indexed by integer index and string name index. If you type in a form Me.Controls(0) or Me.Controls("controlname") both return a form control. Later, for example in event handling you can list multiple controls in the event handler, or have the IDE do it for you, then the 'sender' parameter of the event handler identifies the control that raised the event.
 
I dont think you can do that anymore but what i've done before is this.

Items on the page:
rdoItem1
rdoItem2
rdoItem3
rdoItem4


for loop
VB.NET:
For i as integer = 0 to 4
    dim rdoItem as RadioButton = FindControl("rdoItem" & i)
    ...
next

Obviously throw in some error handling. I imagine there is a better way to do it but that is the trick I used when i came from vb6.
 
Yeah! I thought as much - going to give your example a try now, thanks.

JohnH - not sure I understand your examples, probably my limited experience in vb.net?
 
I dont think you can do that anymore but what i've done before is this.

Items on the page:
rdoItem1
rdoItem2
rdoItem3
rdoItem4


for loop
VB.NET:
For i as integer = 0 to 4
    dim rdoItem as RadioButton = FindControl("rdoItem" & i)
    ...
next

Obviously throw in some error handling. I imagine there is a better way to do it but that is the trick I used when i came from vb6.

Sorry rcombs4, I feel a bit silly now, but it says Name 'FindControl' is not declared.
How do I include or declare it? Is it standard in VS2008?
Thanks.
 
His method is better.

Are you working on a webproject or a forms project?


The find control method is a part of a container. So say you have a panel. Then put all your radio buttons inside the panel.

Now you can use Panel1.FindControl() if you are working on a web project
Or use (not sure) Pane1.Controls.Find() if you are working on a forms project.

His method is a more secure way of doing this.

(Im going to assume you are web based)
Add all your radio buttons in a Panel.
Your .aspx page
VB.NET:
<asp:Panel id=pnlButtons" ...>
   <asp:radiobutton id="rdoSomeButton1" ... />
   <asp:radiobutton id="rdoSomeButton2" ... />
   <asp:radiobutton id="rdoSomeButton3" ... />
   <asp:radiobutton id="rdoSomeButton4" ... />
   <asp:radiobutton id="rdoSomeButton5" ... />
</asp:Panel>

Code Behind
VB.NET:
for each ctrlItem as Control in pnlButtons.Controls
  if TypeOf ctrlItem Is RadioButtonControl Then
     dim rdoButton as RadioButtonControl = ctrlItem
     ....
  End If
next

Something like that should do you just fine.
 
Back
Top