Option strict on and detecting a listview Name

J

James

Hi. I'm probably attempting the impossible but what the hey.

I'm modifying a multi-paneled form which has 8 different listviews, To
allow sorting by columns I've had to add a diffrerent handler Sub for
each of them eg.

Private Sub ListViewOne(ByVal o As Object, ByVal e As
ColumnClickEventArgs)
...
End sub

If I could somehow detect the o.Name property of the sender object I
could identify the ListView and make the Sub handle all 8 of the
ListViews.

However because "Option Strict On" disallows late binding, I can't find
a way of accessing the properties of the sending object.

PS: I'm adding the handler to each listview as I populate them with
"AddHandler"

Any polite ideas ?
 
J

James

hmmm Searched for ages for an answer so as luck would have it, I found
an answer 10 minutes after my post :)

The solution:

If DirectCast(o, Control).Name = "ListViewOne" Then
MsgBox("Yep, this is ListViewOne")
End If

A big thank you for the solution goes to :
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/

Who answered a similar question.
 
P

Phill W.

James said:
Private Sub ListViewOne(ByVal o As Object, ByVal e As
ColumnClickEventArgs)
...
End sub

If I could somehow detect the o.Name property of the sender object I
could identify the ListView and make the Sub handle all 8 of the
ListViews.

Or, better still, because you might rename one of the ListView controls
later on and forget to change this routine, how about

Private Sub AnyListView_ColumnClick( _
ByVal sender as Object _
, ByVal e as ColumnClickEventArgs _
)

If sender Is ListView1 Then
...
ElseIf sender Is ListView2 Then
...
End If

End Sub

Or, if you don't need to /differentiate/ between them (i.e. you want all
the ListViews to do the /same/ thing when you click on them

Private Sub AnyListView_ColumnClick( _
ByVal sender as Object _
, ByVal e as ColumnClickEventArgs _
)

If TypeOf sender Is ListView Then
With DirectCast( sender, ListView )
...
End With
End If

End Sub

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

Top