iterate through a set of controls

  • Thread starter Thread starter romy
  • Start date Start date
R

romy

Hi

In VB.net

I have a set of linkButtons controls in a form , which I want to iterate on
them and change their text property.

How it's done ?

thanks
 
Dim c As Control

For Each c In Me.Controls
If TypeOf c Is LinkButton Then
c.Text = "Bla Bla"
End If
Next c

Crouchie1998
BA (HONS) MCP MCSE
 
romy said:
I have a set of linkButtons controls in a form , which I want to iterate
on them and change their text property.

Web Forms or Windows Forms?
 
Romy,

\\\
Dim frm As Control = Me.FindControl("Form1")
For Each ctl as Control In frm.Controls
if TypeOf ctl Is Linkbutton Then
DirectCast(ctl, Linkbutton).Text = "WathEver"
End If
Next
///

I hope this helps,

Cor
 
romy said:
I have a set of linkButtons controls in a form, which I want to
iterate on them and change their text property.

You /could/ loop through the Controls collection of the Form,
looking for LinkButtons. Sadly, this /only/ works when all the
LinkButtons are actually placed /on/ the Form and not inside
any "container" controls, like Panels or GroupBoxes, in which
case you have to [recursively] scan the Controls Collection of
each container!

Alternatively, if the LinkButtons are all set up at Design Time,
create a - gasp - Control Array. No, don't laugh; I actually
mean an Array of Controls or, in this case, of LinkButtons, as in

Private AllLinkButtons as LinkButton() = { _
Me.LinkButton1, Me.LinkButton2, Me.LinkButton3
...
}

Then, you can simply use

For Each eLB as LinkButton in AllLinkButtons
eLB.Text = "whatever"
Next

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top