Click here to Skip to main content
15,915,057 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralCombobox and Database Pin
gls2ro4-Feb-04 2:14
gls2ro4-Feb-04 2:14 
GeneralRe: Combobox and Database Pin
Pugman8124-Feb-04 13:49
Pugman8124-Feb-04 13:49 
QuestionHow to sort the listview on the basis of the clicked column Pin
Tasnim4-Feb-04 1:22
Tasnim4-Feb-04 1:22 
AnswerRe: How to sort the listview on the basis of the clicked column Pin
Vi24-Feb-04 4:26
Vi24-Feb-04 4:26 
GeneralBut I want it in Vb.net Pin
Tasnim4-Feb-04 23:19
Tasnim4-Feb-04 23:19 
GeneralRe: But I want it in Vb.net Pin
Dave Kreskowiak5-Feb-04 9:32
mveDave Kreskowiak5-Feb-04 9:32 
GeneralCouldnt find it!!! Pin
Tasnim5-Feb-04 23:20
Tasnim5-Feb-04 23:20 
GeneralRe: Couldnt find it!!! Pin
Dave Kreskowiak6-Feb-04 3:24
mveDave Kreskowiak6-Feb-04 3:24 
OK. The second class in the example just implements a Sorter. What your doing is writing your own custom sorter that can sort items on any column. The only argument you need in the column number. This would allow your to write sorters for different types of data in each column.

When you 'Implement' an interface, in this case IComparer, your given a framework that .NET expects you to follow. All you have to do is hang code on the methods that you have to implement. In the example in the help files, the only real code is the Compare function. .NET expects the function to take 2 parameters, both generic Objects and expects you to compare them somehow and return an Integer as a result of the compare. Looking at the documentation for IComparer.Compare (the function you have to supply code for!), the function is expected to return the following:
Less than zero if x is less than y. <br />
Zero if x equals y. <br />
Greater than zero if x is greater than y. <br />

In the sample below, .NET will call you with two ListViewItem objects, x and y. Since you know that your going to be getting two ListViewItems to compare, you have to cast the two Objects as ListViewItems. Then you can use the ListViewItems.SubItems property and the column number that is passed in when this class is created (Public Sub New(ByVal column as Integer)) to get the Text to compare. Then all you have to do is use the String.Compare method on the two SubItem.Text Strings and return the value that .NET and the IComparer interface expects you to return. Check out the docs on String.Compare and you'll see that its return values match those expected by IComparer.Compare.
' Implements the manual sorting of items by columns.
Class ListViewItemComparer
   Implements IComparer

   Private col As Integer

   Public Sub New()
      columnNumber = 0
   End Sub

   Public Sub New(ByVal column As Integer)
      columnNumber = column
   End Sub

   Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
      Dim returnValue as Integer
      Dim compX as ListViewItem = CType(x, ListViewItem)
      Dim compY as ListViewItem = CType(y, ListViewItem)

      returnValue = String.Compare(compX.SubItems(columnNumber).Text, compY.SubItems(columnNumber).Text)
      Return returnValue
   End Function
End Class


Now that you have a comparer class written, all you have to do is tell the ListView to use it when a column header is clicked on and tell your new sorter class to use the column number you clicked on:
' ColumnClick event handler.
Private Sub ColumnClick(ByVal o As Object, ByVal e As ColumnClickEventArgs)
   ' Set the ListViewItemSorter property to a new ListViewItemComparer object.
   ListView1.ListViewItemSorter = New ListViewItemComparer(e.Column)
   ' Call the sort method to manually sort the column based on the ListViewItemComparer implementation.
   ListView1.Sort()
End Sub



RageInTheMachine9532
GeneralThx Pin
Tasnim6-Feb-04 19:20
Tasnim6-Feb-04 19:20 
GeneralBut ... Pin
Tasnim8-Feb-04 21:16
Tasnim8-Feb-04 21:16 
GeneralHelp Help Help Me in Combo,,Anyone Pin
Het21093-Feb-04 23:41
Het21093-Feb-04 23:41 
GeneralRe: Help Help Help Me in Combo,,Anyone Pin
John Kuhn4-Feb-04 8:05
John Kuhn4-Feb-04 8:05 
GeneralI am working in VB.Net Pin
Het21095-Feb-04 1:34
Het21095-Feb-04 1:34 
GeneralRe: Help Help Help Me in Combo,,Anyone Pin
John Kuhn5-Feb-04 7:19
John Kuhn5-Feb-04 7:19 
GeneralRuntime error Pin
GSekar3-Feb-04 20:12
GSekar3-Feb-04 20:12 
GeneralRe: Runtime error Pin
Matthew Hazlett3-Feb-04 20:40
Matthew Hazlett3-Feb-04 20:40 
GeneralRe: Runtime error Pin
Matthew Hazlett3-Feb-04 20:47
Matthew Hazlett3-Feb-04 20:47 
GeneralRe: Runtime error Pin
GSekar4-Feb-04 0:06
GSekar4-Feb-04 0:06 
GeneralSimple Problem With ListBoxes Pin
Pugman8123-Feb-04 18:04
Pugman8123-Feb-04 18:04 
GeneralRe: Simple Problem With ListBoxes Pin
John Kuhn3-Feb-04 19:18
John Kuhn3-Feb-04 19:18 
GeneralRe: Simple Problem With ListBoxes Pin
Pugman8124-Feb-04 12:22
Pugman8124-Feb-04 12:22 
GeneralRe: Simple Problem With ListBoxes Pin
John Kuhn4-Feb-04 14:54
John Kuhn4-Feb-04 14:54 
GeneralUsing Java Classes in VB appl. Pin
Sairam Samavedam3-Feb-04 9:07
Sairam Samavedam3-Feb-04 9:07 
GeneralRe: Using Java Classes in VB appl. Pin
John Kuhn3-Feb-04 12:53
John Kuhn3-Feb-04 12:53 
GeneralRe: Using Java Classes in VB appl. Pin
Dave Kreskowiak4-Feb-04 5:23
mveDave Kreskowiak4-Feb-04 5:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.