Results 1 to 9 of 9

Thread: User control/listview

  1. #1

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    User control/listview

    I'm new at VB .net and never did this in VB 6.0. Maybe someone can point me in the right direction.

    I'm trying to create a generic ListView user control that can be used over and over again in different projects. I've got all the basics done but I want to be able to act on click events in the from not the user control. In other words if someone right clicks on the listview, for example, I want the form to know it and be able to act on it.

    I haven't had any luck with Google.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: User control/listview

    Why not just inherit the ListView control?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: User control/listview

    I'm very new at this. Would reading up on inheritance get me close to what I want? I don't have any formal training. Just VB 6.0 experience.
    Last edited by TysonLPrice; Jul 10th, 2009 at 05:59 AM.

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: User control/listview

    Assuming that your usercontrol will have more than just the listview, you probably want this route :

    Declare your event and then raise it when you want it to occur.

    For example here's a user control with a listview and an event called ListViewRightClick event that I will raise when the user right-clicks on the listview.

    Code:
    Public Class myUserControl
    
        Public Event ListViewRightClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    
        Private Sub ListView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick
    
            If e.Button = Windows.Forms.MouseButtons.Right Then
                RaiseEvent ListViewRightClick(sender, e)
            End If
    
        End Sub
    End Class
    And then I have a form which contains an instance of the user control and I just have an event handler much as you would with any other control's events :

    Code:
    Public Class Form1
    
        Private Sub MyUserControl1_ListViewRightClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyUserControl1.ListViewRightClick
    
            MessageBox.Show(e.Location.ToString)
    
        End Sub
    
    End Class
    Note that I have set up the arguments to my own event to have the same types as those of the standard mouse-click event and I'm basically passing the sender (ie the listview) and the event args that the original mouse-click raised. You can have whatever you like as the argument types and content (or even none at all), but it is convention to use the object raising the event, and some form of EventArgs (you can create your own type that inherit from System.EventArgs)

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: User control/listview

    If your usercontrol inherits the ListView control, it will behave and look exactly like a ListView control. That is, until you add your own code to change that.

    Usually, you would inherit the ListView control, and then use the Override methods to override specific behavior.

  6. #6
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: User control/listview

    I was interpreting the original question as being a case of the usercontrol contains a listview as one of its constituent controls, but on re-reading maybe it is just a custom listview and nothing else, in which case you can disregard my comment and go with inheriting and overriding a standard listview.

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: User control/listview

    I re-read the OP's question too, and it seems like he already has the user control very much done except the raiseevent part on right clicks on the listview.
    If this is the case then Paul's code definately helps you heading in the right direction. If you haven't get anything done and just get started on the project, go the inheritance approach. You only have to override what's needed and it saves a lot of time besides preserving the control behavior on the parts that are not overridden.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: User control/listview

    Quote Originally Posted by keystone_paul View Post
    I was interpreting the original question as being a case of the usercontrol contains a listview as one of its constituent controls, but on re-reading maybe it is just a custom listview and nothing else, in which case you can disregard my comment and go with inheriting and overriding a standard listview.
    It is a user control with just a listview that is meant to be generic. What our team is trying do do is build a number of controls to be used globally in new projects. We are just now starting to move away from VB 6.0 and trying to build commonly used controls such as listview, comboboxes etc.

    In the user control for the listview I'm working on it does things like dynamically builds the column headers and rows from a recordset passed it or allow user defined columns and rows to be added. Plus changes to appearance, etc.

    Your solution worked for me but based on what I see being posted it's not the best way to go if your starting from scratch. Could you outline what I should start looking at along those lines?

    By the way...Thanks for taking the time to offer your solution!

  9. #9
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: User control/listview

    No problem - if all you are doing is taking an existing control and extending it you can just create a class that inherits the base class (ie the listview). Simply add the line "Inherits ListView" after the first line "Public Class MyListView" (or whatever the control is going to be called)

    Then it will automatically generate all the events and properties that already exist in the listview and you can either simply add new ones (new events can be done as per my example) or you can override existing ones. (NB - you won't see the code for the inherited members in your class)

    If you don't want to create a new composite control then you don't really need to create a user control - a user control would be more useful if for example you wanted to tie a few controls together like if you wanted to add a toolstrip above your listview and provide add, delete buttons etc.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width