Results 1 to 7 of 7

Thread: ListView problem

Threaded View

  1. #1

    Thread Starter
    Addicted Member parminder's Avatar
    Join Date
    Apr 2003
    Location
    India
    Posts
    168

    ListView problem

    Hi to all,

    Here is a ListView problem :

    I have a ListView control on a form. The control's View is "Detail". I am using netsted class ( I named it ListViewItemComparer ) to sort ListView control to use with its "ListViewItemSorter" property and "Sort" method.

    The PROBLEM is when I click on column to sort, it works fine; BUT to browse the ListViewControl I have to use mouse ONCE. Without using mouse, browsing of ListView control is not possible with Keyboard like first alphabet of a item, arrow keys (except DOWN arrow key, which moves conrol to first item in ListView control).

    So the requirement is, user can click on column-header to sort the column user can use keyboard to select item in ListView control.

    Thanks in advance

    The code is written below and the simple project also is ATTACHED with this message.

    The code :
    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.     Dim sortColumn As Integer = -1
    4. 'Region " Windows Form Designer generated code (Removed)"
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         Me.ListView1.Columns.Add("IName", 200, HorizontalAlignment.Right)
    8.         Me.ListView1.Columns.Add("ICode", 50, HorizontalAlignment.Left)
    9.         Me.ListView1.Columns.Add("Rate", 50, HorizontalAlignment.Right)
    10.  
    11.         Dim lvwi As ListViewItem
    12.  
    13.         lvwi = NewListViewItem("Ice Cream")
    14.         lvwi.SubItems.Add("101")
    15.         lvwi.SubItems.Add("25")
    16.         Me.ListView1.Items.Add(lvwi)
    17.  
    18.         lvwi = NewListViewItem("Soft Drink")
    19.         lvwi.SubItems.Add("102")
    20.         lvwi.SubItems.Add("20")
    21.         Me.ListView1.Items.Add(lvwi)
    22.  
    23.         lvwi = NewListViewItem("Pizza")
    24.         lvwi.SubItems.Add("103")
    25.         lvwi.SubItems.Add("60")
    26.         Me.ListView1.Items.Add(lvwi)
    27.  
    28.         lvwi = NewListViewItem("Choclate Milk Shake")
    29.         lvwi.SubItems.Add("104")
    30.         lvwi.SubItems.Add("50")
    31.         Me.ListView1.Items.Add(lvwi)
    32.     End Sub
    33.  
    34.     Private Function NewListViewItem(ByVal pItemName As String) As ListViewItem
    35.         Dim llvwI As New ListViewItem(pItemName.ToString)
    36.         Return llvwI
    37.     End Function
    38.  
    39.     'Nested Class to support column sorting
    40.     Class ListViewItemComparer
    41.         Implements IComparer
    42.         Private col As Integer
    43.         Private order As SortOrder
    44.         Private IsNumeric As Boolean
    45.         Public Sub New(ByVal column As Integer, ByVal order As SortOrder)
    46.             col = column
    47.             Me.order = order
    48.         End Sub
    49.  
    50.         Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
    51.             Dim returnVal As Integer
    52.             returnVal = [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
    53.             If order = SortOrder.Descending Then
    54.                 returnVal *= -1
    55.             End If
    56.             Return returnVal
    57.         End Function
    58.     End Class
    59.  
    60.     Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
    61.         If e.Column <> sortColumn Then
    62.             sortColumn = e.Column
    63.             ListView1.Sorting = SortOrder.Ascending
    64.         Else
    65.             If ListView1.Sorting = SortOrder.Ascending Then
    66.                 ListView1.Sorting = SortOrder.Descending
    67.             Else
    68.                 ListView1.Sorting = SortOrder.Ascending
    69.             End If
    70.         End If
    71.         ListView1.Sort()
    72.         ListView1.ListViewItemSorter = New ListViewItemComparer(e.Column, ListView1.Sorting)
    73.     End Sub
    74. End Class
    Attached Files Attached Files

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