Subclassing / Inheritance

Subclassing / Inheritance

Enhancing existing controls with new features and functions is easy with .Net.

Let’s say that you have a form that uses a ListView control, and you want  to be able to allow the user to move items up and down in the ListView by  pressing a button – one button for up, and another for down.

You could write code in the button click events that manipulated items in the  ListView, but a better solution is to create a new version of the ListView  control that ‘knows’ how to move items like this for itself.  You could make  this functionality available wherever you need it simply by using this new  version instead of the original.

Creating a new control seems like a big job, but in fact all you are doing is  adding some extra functionality to an existing control, and the only thing you  need to worry about are these extra functions.  A single line of code will  do all the rest.

The magic that makes this possible is inheritance.  If you create a  control that inherits from the ListView control, then you get all the  functionality in the list view control for free.  If your changes simply  involve adding something to the control, then it is only that additional code  that you need to write.

With the project where you want to use the new control open, select Project /  Add Class…   Give the file a sensible name, such as myListView.   Put the following code in the Class file.

Public Class myListView
Inherits ListView

End Class

Select Build / Build Solution.  A new component will appear at the top  of the components in the tool box – myListView.  Open the form in design  view and drag the new component on to the form.

That’s all there is to it.  This new version of the ListView class can  now be used in your project.  At the moment it doesn’t do anything  differently than the old ListView component did.  But the important point  is that it already does everything that the original  component did.  That’s what the ‘Inherits ListView’ line of code did for  you.

To add additional functionality we simply create public methods for the  class.  In this example we will add two methods.  Each one will take  an argument of the selected items in the list view.  One method will move  those items up a row, the other will move them down a row.   Note that  we must check that the arguments are valid.  In this case, we will simply  not move anything if there is an invalid index in the list.  A more common  response would be to raise an exception, but in this case the programmer using  this control would have to work pretty hard to create an invalid argument, so  they probably deserve to be met with an impassive refusal to move.  Add the  following code to the class, under the Inherits line:

Public Sub ItemMoveUp(ByVal SelectionIndices As  ListView.SelectedIndexCollection)
  For Each Index As Integer In SelectionIndices
    If Index <= 0 OrElse Index >=  Me.Items.Count Then
      Exit Sub
    End If
  Next
  For Each Index As Integer In SelectionIndices
    Dim Store As ListViewItem =  Me.Items(Index)
    Me.Items.RemoveAt(Index)
    Me.Items.Insert(Index - 1, Store)
    Next
  Me.Refresh()
End Sub

Public Sub ItemMoveDown(ByVal SelectionIndices As  ListView.SelectedIndexCollection)
  For Each Index As Integer In SelectionIndices
    If Index < 0 OrElse Index >=  Me.Items.Count - 1 Then
      Exit Sub
    End If
  Next
  For I As Integer = SelectionIndices.Count - 1 To 0  Step -1
    Dim Index As Integer =  selectionindices(I)
    Dim Store As ListViewItem =  Me.Items(Index)
    Me.Items.RemoveAt(Index)
    Me.Items.Insert(Index + 1, Store)
  Next
  Me.Refresh()
End Sub

Now, on the main form, create your list view control using myListView and  configure it in the usual way.  Create a Move UP button and a Move Down  button.  Add code like this:

Private Sub btnUP_Click(ByVal sender As  System.Object, _
        ByVal e As System.EventArgs) Handles  btnUP.Click
  MyListView1.ItemMoveUp(MyListView1.SelectedIndices)
End Sub

Private Sub btnDN_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles  btnDN.Click
   MyListView1.ItemMoveDown(MyListView1.SelectedIndices)
End Sub

Run the program. Select one or more items in the custom list view. Select the  up button to move the selected items up, or the down button to move the selected  items down.

That works, but you will soon notice there is a problem – when  writing code for this control the new methods do not appear in Intellisense. See here for an upgrade to this control that fixes this problem.

 

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s