Re: User control/listview
Why not just inherit the ListView control?
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.
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)
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.
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.
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.
Re: User control/listview
Quote:
Originally Posted by
keystone_paul
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!
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.