Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

index of an element in a control array

1 view
Skip to first unread message

Massimo Riccardi

unread,
Dec 20, 2009, 9:02:33 AM12/20/09
to
Hi all,

I have an array of controls (made at runtime) and containing 10 buttons
(Button(0),Button(1)....etc.).
Ho I can get the index of a selected control?
For example if I click the button number 5 how I can retrieve its index
(that it is the fifth button)?
(In VB6 there was the "INDEX" .....)

Thanks
Massimo


Family Tree Mike

unread,
Dec 20, 2009, 11:01:49 AM12/20/09
to

In your click handler:
Dim b as Button = CType(sender, Button)
dim idx as integer = Array.IndexOf(Button, b)

--
Mike

eBob.com

unread,
Dec 20, 2009, 11:11:18 AM12/20/09
to
Most straightforward would be to simply do a lookup. More efficient would
be to take advantage of the Button's Tag property. I.E. simply set the Tag
to the index value.

Bob

"Massimo Riccardi" <rima...@aliceposta.it> wrote in message
news:4b2e2e6d$0$1106$4faf...@reader3.news.tin.it...

Phill W.

unread,
Dec 22, 2009, 6:09:05 AM12/22/09
to
Massimo Riccardi wrote:

> I have an array of controls (made at runtime) and containing 10 buttons
> (Button(0),Button(1)....etc.).

> How I can get the index of a selected control?

Why do you need it?

Event handlers all receive a reference to the Control that raised the
event, so you don't need to index for that ...

Private Sub AnyButton_Click( _
byval sender as Object _
, byval e as EventArgs _
)
Dim btn as Button = Nothing
If TypeOf sender is Button Then
btn = DirectCast( sender, button )
End If

btn.Text = ...

End Sub

HTH,
Phill W.

0 new messages