Listview items count changed event? How to create it? (VB2005)

Is it possible to create a custom event for standard windows control "ListView"? Something like this:

Private Sub ListView1_count_changed (.....) handles ListView1."ItemsCountChanged"
Label1.Text = Me.ListView1.Items.Count
End Sub

Thank you.
LVL 10
vadim63Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
Not without creating a custom control based on the ListView and defining your own event.
0
Bob LearnedCommented:
No, but there might be a different way to approach a solution.

Bob
0
vadim63Author Commented:
So, should I create a user control with "Inherits System.Windows.Forms.ListView"?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Bob LearnedCommented:
How are you adding ListView items?

Bob
0
xersoftCommented:
if one could some how replace the ListViewItemCollection in a custom control based on the listView to say the following:

Private Class ListViewItemCollectionEx
        Inherits Windows.Forms.ListView.ListViewItemCollection

        Public Sub New(ByVal owner As ListView)
            MyBase.New(owner)
        End Sub

        Public Overloads Overrides Function Add(ByVal text As String) As System.Windows.Forms.ListViewItem
            MyClass.Add(New ListViewItem(text))
        End Function
        Public Overloads Overrides Function Add(ByVal text As String, ByVal imageIndex As Integer) As System.Windows.Forms.ListViewItem
            MyClass.Add(New ListViewItem(text, imageIndex))
        End Function
        Public Overloads Overrides Function Add(ByVal value As System.Windows.Forms.ListViewItem) As System.Windows.Forms.ListViewItem
            MyBase.Add(value)
            'raise some event here
        End Function
    End Class

That would be a start. I can't for the life of me figure out how to do this though. I would have thought a refference to the list was stored in a private member variable in the listview, but even using reflection it is hidden. Does microsoft hide private members from us? I'm thinking they do.
0
Bob LearnedCommented:
@xersoft:
Have you ever used Reflector?

Here is the declaration for the Items collection field:

    Private listItemCollection As ListViewItemCollection

Bob
0
vadim63Author Commented:
What is reflector?
All what I need is to rise a custom event in two cases: if the item is added and if the item is removed. So, If I understood correctly, I can use xersoft's idea and override functions Add, Remove and RemoveAt. Am I right?
0
vadim63Author Commented:
By the way, sorry guys for my English.
0
Bob LearnedCommented:
If you are not using a custom control, then you can raise events at the point where you add/remove the items.  Also, it may be possible to skip using the events.

Reflector for .NET
http://www.aisto.com/roeder/dotnet/

<Quote>
Reflector is the class browser, code explorer, code analyzer and documentation viewer for .NET. Reflector allows to easily view, navigate, search, decompile and analyze .NET assemblies in C#, Visual Basic, C++, IL assembly, Delphi and Chrome.
</Quote>

All the .NET framework are in assemblies, so you can use Reflector to take a peek into the framework, and see how Micro$oft does it.

Bob
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
xersoftCommented:
Wow.

Yeah that is a good piece of software. Thank you for pointing it out to me.

The gray members must be those that Microsoft removed Meta data for, hence reflection will not work. So in this case it looks like private really is private. It would be interesting to know if there was a way to get around this. And if it would be possible to duplicate this behavior in our own code.  For academic purposes, I’ll continue to look into it. Thanks again.

Vadim63, is your application in control of all addition and removal of listview items? If so then can you track down each place in your code where you add and item and each place where you remove an item? Once you find these locations place a line right under the add or remove that sets the label.text property to the new count.
0
Bob LearnedCommented:
Gray members are private members, and you still have access through reflection to private members.

Bob
0
vadim63Author Commented:
Yes, actually I already did it. Just I wanted to find out if it possible via custom event. Anyway thank you very much guys. I've got a very usefull info from you. I would like to split the points between xersoft and TheLearnedOne. How can do it?
0
Bob LearnedCommented:
There is a Split button at the bottom of the question.

Bob
0
xersoftCommented:
hmmm maybe then it would be possible to set the ListViewItemCollection member to a new instance. I'm not able to get a reference to any member with that name though:


        Dim members() As Reflection.MemberInfo = Me.GetType.GetMember("listItemCollection", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
        MsgBox(members.Length)

If this is a rights issue then  I can't imagine this working as a valid solution if every user had to have special rights to run the code! Back to the drawing board if so.
0
vadim63Author Commented:
My God. I'm blind. Thank you.
0
vadim63Author Commented:
Thank you again. I'm full of ideas. I'm going to play with it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.