• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1884
  • Last Modified:

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.
0
vadim63
Asked:
vadim63
  • 6
  • 6
  • 3
  • +1
2 Solutions
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 6
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now