Community discussion forum

ListView-How to use it?

  • 2 years ago

    Dear friends,

    I'm new to VB.net(2005). I already had "Visual Basic 2005 for Dummies"( i confest to it)

    But, in that book doesn't show how to create and use ListView.

    I tried learned it from the internet. But the code I paste doesn't show any effect.

    I had been trying over and over again.

    How I do it?

    I create a new form...Then I select ListView Control from the toolbox. Doubleclick the ListView control. Then the form would show :

    '*********************************************

    Public

    Class Form1

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    End

    Class

    '*********************************************

    Then I paste the code I got(http://msdn2.microsoft.com/en-us/library/system.windows.forms.listviewgroup.aspx) to it and it looks like this. Ctrl-A then Ctrl-C

    I'm sorry if the way I'm doing it is not correct.

    '*********************************************

    Imports System Imports System.Collections
    Imports System.Windows.Forms

    Public Class ListViewGroupsExample
        Inherits Form

        Private myListView As ListView

        ' Determine whether Windows XP or a later
        ' operating system is present.
        Private isRunningXPOrLater As Boolean = _
            OSFeature.Feature.IsPresent(OSFeature.Themes)
       
        ' Declare a Hashtable array in which to store the groups.
        Private groupTables() As Hashtable
       
        ' Declare a variable to store the current grouping column.
        Private groupColumn As Integer = 0
       
        Public Sub New()
            ' Initialize myListView.
            myListView = New ListView()
            myListView.Dock = DockStyle.Fill
            myListView.View = View.Details
            myListView.Sorting = SortOrder.Ascending
           
            ' Create and initialize column headers for myListView.
            Dim columnHeader0 As New ColumnHeader()
            columnHeader0.Text = "Title"
            columnHeader0.Width = -1
            Dim columnHeader1 As New ColumnHeader()
            columnHeader1.Text = "Author"
            columnHeader1.Width = -1
            Dim columnHeader2 As New ColumnHeader()
            columnHeader2.Text = "Year"
            columnHeader2.Width = -1
           
            ' Add the column headers to myListView.
            myListView.Columns.AddRange( New ColumnHeader() _
                {columnHeader0, columnHeader1, columnHeader2} )
           
            ' Add a handler for the ColumnClick event.
            AddHandler myListView.ColumnClick, AddressOf myListView_ColumnClick
           
            ' Create items and add them to myListView.
            Dim item0 As New ListViewItem( New String() _
                {"Programming Windows", _
                "Petzold, Charles", _
                "1998"} )
            Dim item1 As New ListViewItem( New String() _
                {"Code: The Hidden Language of Computer Hardware and Software", _
                "Petzold, Charles", _
                "2000"} )
            Dim item2 As New ListViewItem( New String() _
                {"Programming Windows with C#", _
                "Petzold, Charles", _
                "2001"} )
            Dim item3 As New ListViewItem( New String() _
                {"Coding Techniques for Microsoft Visual Basic .NET", _
                "Connell, John", _
                "2001"} )
            Dim item4 As New ListViewItem( New String() _
                {"C# for Java Developers", _
                "Jones, Allen / Freeman, Adam", _
                "2002"} )
            Dim item5 As New ListViewItem( New String() _
                {"Microsoft .NET XML Web Services Step by Step", _
                "Jones, Allen / Freeman, Adam", _
                "2002"} )
            myListView.Items.AddRange( _
                New ListViewItem() {item0, item1, item2, item3, item4, item5})
           
            If isRunningXPOrLater
                ' Create the groupsTable array and populate it with one
                ' hash table for each column.
                groupTables = New Hashtable(myListView.Columns.Count) {}
                Dim column As Integer
                For column = 0 To myListView.Columns.Count - 1
                    ' Create a hash table containing all the groups
                    ' needed for a single column.
                    groupTables(column) = CreateGroupsTable(column)
                Next column
               
                ' Start with the groups created for the Title column.
                SetGroups(0)
            End If
           
            ' Initialize the form.
            Me.Controls.Add(myListView)
            Me.Size = New System.Drawing.Size(550, 330)
            Me.Text = "ListView Groups Example"
        End Sub 'New
       
        <STAThread()> _
        Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New ListViewGroupsExample())
        End Sub 'Main
       
        ' Groups the items using the groups created for the clicked
        ' column.
        Private Sub myListView_ColumnClick( _
            sender As Object, e As ColumnClickEventArgs)

            ' Set the sort order to ascending when changing
            ' column groups; otherwise, reverse the sort order.
            If myListView.Sorting = SortOrder.Descending OrElse _
                isRunningXPOrLater And e.Column <> groupColumn Then
                myListView.Sorting = SortOrder.Ascending
            Else
                myListView.Sorting = SortOrder.Descending
            End If
            groupColumn = e.Column
           
            ' Set the groups to those created for the clicked column.
            If isRunningXPOrLater Then
                SetGroups(e.Column)
            End If
        End Sub 'myListView_ColumnClick
       
        ' Sets myListView to the groups created for the specified column.
        Private Sub SetGroups(column As Integer)
            ' Remove the current groups.
            myListView.Groups.Clear()
           
            ' Retrieve the hash table corresponding to the column.
            Dim groups As Hashtable = CType(groupTables(column), Hashtable)
           
            ' Copy the groups for the column to an array.
            Dim groupsArray(groups.Count - 1) As ListViewGroup
            groups.Values.CopyTo(groupsArray, 0)
           
            ' Sort the groups and add them to myListView.
            Array.Sort(groupsArray, New ListViewGroupSorter(myListView.Sorting))
            myListView.Groups.AddRange(groupsArray)
           
            ' Iterate through the items in myListView, assigning each
            ' one to the appropriate group.
            Dim item As ListViewItem
            For Each item In myListView.Items
                ' Retrieve the subitem text corresponding to the column.
                Dim subItemText As String = item.SubItems(column).Text
               
                ' For the Title column, use only the first letter.
                If column = 0 Then
                    subItemText = subItemText.Substring(0, 1)
                End If

                ' Assign the item to the matching group.
                item.Group = CType(groups(subItemText), ListViewGroup)
            Next item
        End Sub 'SetGroups

        ' Creates a Hashtable object with one entry for each unique
        ' subitem value (or initial letter for the parent item)
        ' in the specified column.
        Private Function CreateGroupsTable(column As Integer) As Hashtable
            ' Create a Hashtable object.
            Dim groups As New Hashtable()
           
            ' Iterate through the items in myListView.
            Dim item As ListViewItem
            For Each item In myListView.Items
                ' Retrieve the text value for the column.
                Dim subItemText As String = item.SubItems(column).Text
               
                ' Use the initial letter instead if it is the first column.
                If column = 0 Then
                    subItemText = subItemText.Substring(0, 1)
                End If

                ' If the groups table does not already contain a group
                ' for the subItemText value, add a new group using the
                ' subItemText value for the group header and Hashtable key.
                If Not groups.Contains(subItemText) Then
                    groups.Add( subItemText, New ListViewGroup(subItemText, _
                        HorizontalAlignment.Left) )
                End If
            Next item
           
            ' Return the Hashtable object.
            Return groups
        End Function 'CreateGroupsTable

        ' Sorts ListViewGroup objects by header value.
        Private Class ListViewGroupSorter
            Implements IComparer
           
            Private order As SortOrder
           
            ' Stores the sort order.
            Public Sub New(theOrder As SortOrder)
                order = theOrder
            End Sub 'New
           
            ' Compares the groups by header value, using the saved sort
            ' order to return the correct value.
            Public Function Compare(x As Object, y As Object) As Integer _
                Implements IComparer.Compare
                Dim result As Integer = String.Compare( _
                    CType(x, ListViewGroup).Header, _
                    CType(y, ListViewGroup).Header )
                If order = SortOrder.Ascending Then
                    Return result
                Else
                    Return -result
                End If
            End Function 'Compare
        End Class 'ListViewGroupSorter

    End Class 'ListViewGroupsExample
    '*********************************************


















































































































































































































    When I run the form, it only show 1 box...nothing else...

    Please guide me...as if i doesn't know how to use the code i get to implement the example given.

    Many sites I had referred, an I as a beginner need gudance in handling the example gibven.

    I'm trying to implement ListView in this following code that I use as a guidance to learn VB.net.

    '*********************************************

    Private Sub CountVG(ByVal sr As StreamReader)

    Dim aLine, s As String

    Dim strTemp As String

    Dim myArray() As String

    Dim i, iWhile, iIF As Integer

    i = 0

    While sr.Peek <> -1

    aLine = sr.ReadLine().Trim()

    If aLine.StartsWith("(*") Then

    While sr.Peek <> -1

    If aLine.IndexOf("*)") <> -1 Then

    Exit While

    End If

    aLine = sr.ReadLine

    End While

    Else

    strTemp = Replace(aLine, vbTab,

    " ")

    strTemp = Replace(strTemp, vbCr,

    " ")

    strTemp = Replace(strTemp, vbLf,

    " ")

    strTemp = Trim(strTemp)

    Do While InStr(1, strTemp, " ", 1) <> 0

    strTemp = Replace(strTemp,

    " ", " ")

    Loop

    myArray = (Split(strTemp,

    " ", -1, 1))

    For Each s In myArray

    If (UCase(s) = "IF") Then

    iIF += 1

    ElseIf (UCase(s) = "WHILE") Then

    iWhile += 1

    End If

    Next

    End If

    End While

    lvwLoop.Items.Clear()

    Dim Impurity As Double

    Dim listitem As New ListViewItem(1)

    listitem.SubItems.Add(

    "If then else")

    listitem.SubItems.Add(iIF)

    lvwLoop.Items.Add(listitem)

    listitem =

    New ListViewItem(2)

    listitem.SubItems.Add(

    "While do")

    listitem.SubItems.Add(iWhile)

    lvwLoop.Items.Add(listitem)

    txtConstruct.Text = iIF + iWhile

    'Cyclomatic Complexity v(G) = 1 + d

    txtVG.Text = (iIF + iWhile) + 1

    'calculate Tree Impurity

    Impurity = (2 * (iIF + iWhile)) / ((Val(txtLine.Text) - 1) * (Val(txtLine.Text) - 2))

    txtImpurity.Text = Impurity.ToString(

    "####0.0000")

    End Sub

    '*********************************************

    Kindly guide me step-by-step.....as I couldn't found any sites that could helped me.

    TQVM

    azlina

     

  • 2 years ago

    I think I got the same problem you did while using the code in that example.  However, I got around the problem by doing this. 

    1) Create a new project
    2) Copy and paste all the code AFTER the 'inherits form' line and BEFORE the last 'End class'
    3) Double click the form to bring up the code window and paste the code in between the to lines of code already there.
    4) Copy the imports statements in the supplied code and paste them before the class declaration.
    5) Change this code:



        <STATHREAD()> _
        Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New ListViewGroupsExample())
        End Sub 'Main
    To This
        <STATHREAD()> _
        Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub 'Main
    If you did everything right that should work. Sorry, I can't look at the rest of your problem right now. I'll try to take a look later on. Good luck.
  • 2 years ago

    Dear TwoFaced,

    I tried to do as you guide me.When finish executing, the output only displays a blank form.

    Do I need to add the ListView controls or something else on the form? I only copy the code and paste it to the form in CodeView in between the

    Public

    Class Form1

    End

    Class

    After doing step 4) and 5) my CodeView looks like below. When I run the form only blank for, as mentioned, appears.Where had I done wrong? Is there any simple code that I can refer. I just want to see how does the output in order to understand how the code works. Please help me TowFaced.

    Imports

    System

    Imports

    System.Collections

    Imports

    System.Windows.Forms

    Public

    Class Form1

     

    Public Class ListViewGroupsExample

    Inherits Form

    Private myListView As ListView

    ' Determine whether Windows XP or a later

    ' operating system is present.

    Private isRunningXPOrLater As Boolean = _

    OSFeature.Feature.IsPresent(OSFeature.Themes)

    ' Declare a Hashtable array in which to store the groups.

    Private groupTables() As Hashtable

    ' Declare a variable to store the current grouping column.

    Private groupColumn As Integer = 0

    Public Sub New()

    ' Initialize myListView.

    myListView =

    New ListView()

    myListView.Dock = DockStyle.Fill

    myListView.View = View.Details

    myListView.Sorting = SortOrder.Ascending

    ' Create and initialize column headers for myListView.

    Dim columnHeader0 As New ColumnHeader()

    columnHeader0.Text =

    "Title"

    columnHeader0.Width = -1

    Dim columnHeader1 As New ColumnHeader()

    columnHeader1.Text =

    "Author"

    columnHeader1.Width = -1

    Dim columnHeader2 As New ColumnHeader()

    columnHeader2.Text =

    "Year"

    columnHeader2.Width = -1

    ' Add the column headers to myListView.

    myListView.Columns.AddRange(

    New ColumnHeader() _

    {columnHeader0, columnHeader1, columnHeader2})

    ' Add a handler for the ColumnClick event.

    AddHandler myListView.ColumnClick, AddressOf myListView_ColumnClick

    ' Create items and add them to myListView.

    Dim item0 As New ListViewItem(New String() _

    {

    "Programming Windows", _

    "Petzold, Charles", _

    "1998"})

    Dim item1 As New ListViewItem(New String() _

    {

    "Code: The Hidden Language of Computer Hardware and Software", _

    "Petzold, Charles", _

    "2000"})

    Dim item2 As New ListViewItem(New String() _

    {

    "Programming Windows with C#", _

    "Petzold, Charles", _

    "2001"})

    Dim item3 As New ListViewItem(New String() _

    {

    "Coding Techniques for Microsoft Visual Basic .NET", _

    "Connell, John", _

    "2001"})

    Dim item4 As New ListViewItem(New String() _

    {

    "C# for Java Developers", _

    "Jones, Allen / Freeman, Adam", _

    "2002"})

    Dim item5 As New ListViewItem(New String() _

    {

    "Microsoft .NET XML Web Services Step by Step", _

    "Jones, Allen / Freeman, Adam", _

    "2002"})

    myListView.Items.AddRange( _

    New ListViewItem() {item0, item1, item2, item3, item4, item5})

    If isRunningXPOrLater Then

    ' Create the groupsTable array and populate it with one

    ' hash table for each column.

    groupTables =

    New Hashtable(myListView.Columns.Count) {}

    Dim column As Integer

    For column = 0 To myListView.Columns.Count - 1

    ' Create a hash table containing all the groups

    ' needed for a single column.

    groupTables(column) = CreateGroupsTable(column)

    Next column

    ' Start with the groups created for the Title column.

    SetGroups(0)

    End If

    ' Initialize the form.

    Me.Controls.Add(myListView)

    Me.Size = New System.Drawing.Size(550, 330)

    Me.Text = "ListView Groups Example"

    End Sub 'New

    <STAThread()> _

    Shared Sub Main()

    Application.EnableVisualStyles()

    Application.Run(

    New Form1())

    End Sub 'Main

    ' Groups the items using the groups created for the clicked

    ' column.

    Private Sub myListView_ColumnClick( _

    ByVal sender As Object, ByVal e As ColumnClickEventArgs)

    ' Set the sort order to ascending when changing

    ' column groups; otherwise, reverse the sort order.

    If myListView.Sorting = SortOrder.Descending OrElse _

    isRunningXPOrLater

    And e.Column <> groupColumn Then

    myListView.Sorting = SortOrder.Ascending

    Else

    myListView.Sorting = SortOrder.Descending

    End If

    groupColumn = e.Column

    ' Set the groups to those created for the clicked column.

    If isRunningXPOrLater Then

    SetGroups(e.Column)

    End If

    End Sub 'myListView_ColumnClick

    ' Sets myListView to the groups created for the specified column.

    Private Sub SetGroups(ByVal column As Integer)

    ' Remove the current groups.

    myListView.Groups.Clear()

    ' Retrieve the hash table corresponding to the column.

    Dim groups As Hashtable = CType(groupTables(column), Hashtable)

    ' Copy the groups for the column to an array.

    Dim groupsArray(groups.Count - 1) As ListViewGroup

    groups.Values.CopyTo(groupsArray, 0)

    ' Sort the groups and add them to myListView.

    Array.Sort(groupsArray,

    New ListViewGroupSorter(myListView.Sorting))

    myListView.Groups.AddRange(groupsArray)

    ' Iterate through the items in myListView, assigning each

    ' one to the appropriate group.

    Dim item As ListViewItem

    For Each item In myListView.Items

    ' Retrieve the subitem text corresponding to the column.

    Dim subItemText As String = item.SubItems(column).Text

    ' For the Title column, use only the first letter.

    If column = 0 Then

    subItemText = subItemText.Substring(0, 1)

    End If

    ' Assign the item to the matching group.

    item.Group =

    CType(groups(subItemText), ListViewGroup)

    Next item

    End Sub 'SetGroups

    ' Creates a Hashtable object with one entry for each unique

    ' subitem value (or initial letter for the parent item)

    ' in the specified column.

    Private Function CreateGroupsTable(ByVal column As Integer) As Hashtable

    ' Create a Hashtable object.

    Dim groups As New Hashtable()

    ' Iterate through the items in myListView.

    Dim item As ListViewItem

    For Each item In myListView.Items

    ' Retrieve the text value for the column.

    Dim subItemText As String = item.SubItems(column).Text

    ' Use the initial letter instead if it is the first column.

    If column = 0 Then

    subItemText = subItemText.Substring(0, 1)

    End If

    ' If the groups table does not already contain a group

    ' for the subItemText value, add a new group using the

    ' subItemText value for the group header and Hashtable key.

    If Not groups.Contains(subItemText) Then

    groups.Add(subItemText,

    New ListViewGroup(subItemText, _

    HorizontalAlignment.Left))

    End If

    Next item

    ' Return the Hashtable object.

    Return groups

    End Function 'CreateGroupsTable

    ' Sorts ListViewGroup objects by header value.

    Private Class ListViewGroupSorter

    Implements IComparer

    Private order As SortOrder

    ' Stores the sort order.

    Public Sub New(ByVal theOrder As SortOrder)

    order = theOrder

    End Sub 'New

    ' Compares the groups by header value, using the saved sort

    ' order to return the correct value.

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _

    Implements IComparer.Compare

    Dim result As Integer = String.Compare( _

    CType(x, ListViewGroup).Header, _

    CType(y, ListViewGroup).Header)

    If order = SortOrder.Ascending Then

    Return result

    Else

    Return -result

    End If

    End Function 'Compare

    End Class 'ListViewGroupSorter

    End Class 'ListViewGroupsExample

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    End

    Class
  • 2 years ago

    Here is the code I used.  I also had trouble just copying and pasting the example you had so I tweaked it just a bit.  Create a new project, then double click the form and replace all the code with this.

    Public Class Form1
        Private myListView As ListView
    
        ' Determine whether Windows XP or a later
        ' operating system is present.
        Private isRunningXPOrLater As Boolean = _
            OSFeature.Feature.IsPresent(OSFeature.Themes)
    
        ' Declare a Hashtable array in which to store the groups.
        Private groupTables() As Hashtable
    
        ' Declare a variable to store the current grouping column.
        Private groupColumn As Integer = 0
    
        Public Sub New()
            ' Initialize myListView.
            myListView = New ListView()
            myListView.Dock = DockStyle.Fill
            myListView.View = View.Details
            myListView.Sorting = SortOrder.Ascending
    
            ' Create and initialize column headers for myListView.
            Dim columnHeader0 As New ColumnHeader()
            columnHeader0.Text = "Title"
            columnHeader0.Width = -1
            Dim columnHeader1 As New ColumnHeader()
            columnHeader1.Text = "Author"
            columnHeader1.Width = -1
            Dim columnHeader2 As New ColumnHeader()
            columnHeader2.Text = "Year"
            columnHeader2.Width = -1
    
            ' Add the column headers to myListView.
            myListView.Columns.AddRange(New ColumnHeader() _
                {columnHeader0, columnHeader1, columnHeader2})
    
            ' Add a handler for the ColumnClick event.
            AddHandler myListView.ColumnClick, AddressOf myListView_ColumnClick
    
            ' Create items and add them to myListView.
            Dim item0 As New ListViewItem(New String() _
                {"Programming Windows", _
                "Petzold, Charles", _
                "1998"})
            Dim item1 As New ListViewItem(New String() _
                {"Code: The Hidden Language of Computer Hardware and Software", _
                "Petzold, Charles", _
                "2000"})
            Dim item2 As New ListViewItem(New String() _
                {"Programming Windows with C#", _
                "Petzold, Charles", _
                "2001"})
            Dim item3 As New ListViewItem(New String() _
                {"Coding Techniques for Microsoft Visual Basic .NET", _
                "Connell, John", _
                "2001"})
            Dim item4 As New ListViewItem(New String() _
                {"C# for Java Developers", _
                "Jones, Allen / Freeman, Adam", _
                "2002"})
            Dim item5 As New ListViewItem(New String() _
                {"Microsoft .NET XML Web Services Step by Step", _
                "Jones, Allen / Freeman, Adam", _
                "2002"})
            myListView.Items.AddRange( _
                New ListViewItem() {item0, item1, item2, item3, item4, item5})
    
            If isRunningXPOrLater Then
                ' Create the groupsTable array and populate it with one 
                ' hash table for each column.
                groupTables = New Hashtable(myListView.Columns.Count) {}
                Dim column As Integer
                For column = 0 To myListView.Columns.Count - 1
                    ' Create a hash table containing all the groups 
                    ' needed for a single column.
                    groupTables(column) = CreateGroupsTable(column)
                Next column
    
                ' Start with the groups created for the Title column.
                SetGroups(0)
            End If
    
            ' Initialize the form.
            Me.Controls.Add(myListView)
            Me.Size = New System.Drawing.Size(550, 330)
            Me.Text = "ListView Groups Example"
        End Sub 'New
    
         _
        Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub 'Main
    
        ' Groups the items using the groups created for the clicked 
        ' column.
        Private Sub myListView_ColumnClick( _
        ByVal sender As Object, ByVal e As ColumnClickEventArgs)
    
            ' Set the sort order to ascending when changing
            ' column groups; otherwise, reverse the sort order.
            If myListView.Sorting = SortOrder.Descending OrElse _
                isRunningXPOrLater And e.Column <> groupColumn Then
                myListView.Sorting = SortOrder.Ascending
            Else
                myListView.Sorting = SortOrder.Descending
            End If
            groupColumn = e.Column
    
            ' Set the groups to those created for the clicked column.
            If isRunningXPOrLater Then
                SetGroups(e.Column)
            End If
        End Sub 'myListView_ColumnClick
    
        ' Sets myListView to the groups created for the specified column.
        Private Sub SetGroups(ByVal column As Integer)
            ' Remove the current groups.
            myListView.Groups.Clear()
    
            ' Retrieve the hash table corresponding to the column.
            Dim groups As Hashtable = CType(groupTables(column), Hashtable)
    
            ' Copy the groups for the column to an array.
            Dim groupsArray(groups.Count - 1) As ListViewGroup
            groups.Values.CopyTo(groupsArray, 0)
    
            ' Sort the groups and add them to myListView.
            Array.Sort(groupsArray, New ListViewGroupSorter(myListView.Sorting))
            myListView.Groups.AddRange(groupsArray)
    
            ' Iterate through the items in myListView, assigning each 
            ' one to the appropriate group.
            Dim item As ListViewItem
            For Each item In myListView.Items
                ' Retrieve the subitem text corresponding to the column.
                Dim subItemText As String = item.SubItems(column).Text
    
                ' For the Title column, use only the first letter.
                If column = 0 Then
                    subItemText = subItemText.Substring(0, 1)
                End If
    
                ' Assign the item to the matching group.
                item.Group = CType(groups(subItemText), ListViewGroup)
            Next item
        End Sub 'SetGroups
    
        ' Creates a Hashtable object with one entry for each unique
        ' subitem value (or initial letter for the parent item)
        ' in the specified column.
        Private Function CreateGroupsTable(ByVal column As Integer) As Hashtable
            ' Create a Hashtable object.
            Dim groups As New Hashtable()
    
            ' Iterate through the items in myListView.
            Dim item As ListViewItem
            For Each item In myListView.Items
                ' Retrieve the text value for the column.
                Dim subItemText As String = item.SubItems(column).Text
    
                ' Use the initial letter instead if it is the first column.
                If column = 0 Then
                    subItemText = subItemText.Substring(0, 1)
                End If
    
                ' If the groups table does not already contain a group
                ' for the subItemText value, add a new group using the 
                ' subItemText value for the group header and Hashtable key.
                If Not groups.Contains(subItemText) Then
                    groups.Add(subItemText, New ListViewGroup(subItemText, _
                        HorizontalAlignment.Left))
                End If
            Next item
    
            ' Return the Hashtable object.
            Return groups
        End Function 'CreateGroupsTable
    
        ' Sorts ListViewGroup objects by header value.
        Private Class ListViewGroupSorter
            Implements IComparer
    
            Private order As SortOrder
    
            ' Stores the sort order.
            Public Sub New(ByVal theOrder As SortOrder)
                order = theOrder
            End Sub 'New
    
            ' Compares the groups by header value, using the saved sort
            ' order to return the correct value.
            Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
                Implements IComparer.Compare
                Dim result As Integer = String.Compare( _
                    CType(x, ListViewGroup).Header, _
                    CType(y, ListViewGroup).Header)
                If order = SortOrder.Ascending Then
                    Return result
                Else
                    Return -result
                End If
            End Function 'Compare
        End Class 'ListViewGroupSorter 
    
    End Class
    
    The example should work if you use the above code although I get the impression you just need a much simpler example. I'll try to provide one in the next post.
  • 2 years ago
    Okay, here is a very basic example I made.  It should give you an idea on how to do what you want to accomplish.  If you have any questions just ask.
    'The form requires two controls (use default names)
    '1) A listview
    '2) A button
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Code to initialize the listview (This could also be done at design time)
    
            'Add two columns
            'You can do this at design time by selecting the listview on your form and clicking
            'the arrow that appears in the top right corner.  Options should appear and one
            'of them is to edit columns appear
            ListView1.Columns.Add("ID", 30)
            ListView1.Columns.Add("Time", 75)
    
            'Change the view to Details so columns
            'Again this can be done in the designer
            ListView1.View = View.Details
        End Sub
    
        Private ID As Integer
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ID += 1
    
            'Create a  listviewitem.  I've passed the ID to the constructor.
            'This will add the value of ID to the first column.
            Dim item As New ListViewItem(ID.ToString)
    
            'This will add the time as a subitem.
            'Subitems are added from left to right.  This will go in the second column
            'because the first column has already been popullated
            item.SubItems.Add(Now.ToLongTimeString)
    
            'Add the item to the listview
            ListView1.Items.Add(item)
        End Sub
    End Class
  • 2 years ago

    Dear TwoFaced,

    I appreciated very much what had u done to me in helping me to understand ListView.Embarrassed [:$]

    It helps me a lot. I managed to have output for the example.

    Also, THANK YOU because providing me with such a simple example.

    I try to implement it to my problem. I appreciate it very much.Big Smile [:D]

    TQVMYes [Y]

  • 2 years ago

    Dear TwoFaced,

    I managed to get an output in my list view...and solved my previous problem.

    But my data is not group...I tried to use group the ListView control but still cannot solve it.

     variable

    < 

    :=

    :=

    > 

    :=

    > 

    > 

    < 

    > 

    < 

    < 

    :=

    :=

    > 

    :=

    > 

    > 

    < 

    > 

    < 

    In ListView box, my output is not group..supposed it counts the number of occurences in each of the variables...and displayed the number og occurences of each operators. Attach is my code, if you required. I just need to group the view and get the total of each operator.

    'Find operator, variables and numbers and store in the temporary list view

    Private Sub countVar(ByVal sr As StreamReader)

    Dim LOC As String

    Dim strTemp As String

    Dim myArray() As String

    Dim flag As Int16

    Dim s As String

    Dim i As Integer

    Dim listItem As ListViewItem = New ListViewItem

    lvCountVar.Items.Clear()

    lvwOp.Items.Clear()

    i = 0

    While sr.Peek <> -1

    LOC = sr.ReadLine().Trim()

    If LOC.StartsWith("/*") Then

    While sr.Peek <> -1

    If LOC.IndexOf("*/") <> -1 Then

    Exit While

    End If

    LOC = sr.ReadLine

    End While

    Else

    strTemp = Replace(LOC, vbTab,

    " ")

    strTemp = Replace(strTemp, vbCr,

    " ")

    strTemp = Replace(strTemp, vbLf,

    " ")

    strTemp = Trim(strTemp)

    Do While InStr(1, strTemp, " ", 1) <> 0

    strTemp = Replace(strTemp,

    " ", " ")

    Loop

    myArray = (Split(strTemp,

    " ", -1, 1))

    flag = 0

    For Each s In myArray

    If (s = "*" Or s = "/" Or s = "+" Or s = "-" Or s = ":=" Or s = "<" Or s = ">") And flag = 0 Then

    lvCountVar.Items.Add(myArray.GetValue(Array.IndexOf(myArray, s) - 1))

    lvCountVar.Items.Add(myArray.GetValue(Array.IndexOf(myArray, s) + 1))

    lvwOp.Items.Add(s)

    i = i + 1

    flag = 1

    ElseIf (s = "*" Or s = "/" Or s = "+" Or s = "-" Or s = ":=" Or s = "<" Or s = ">") And flag = 1 Then

    lvCountVar.Items.Add(myArray.GetValue(Array.IndexOf(myArray, s) + 1))

    lvwOp.Items.Add(s)

    i = i + 1

    End If

    Next

    End If

    End While

    CountOperator()

    CountNumber()

    End Sub

    'list out variable and numbers

    Private Sub CountNumber()

    Dim strTemp As String

    Dim strTemp2, strTemp3 As String

    Dim i, j, counter As Integer

    Dim cntNum, cntVar, totVar, totNum As Integer

    lvwVar.Items.Clear()

    lvwNum.Items.Clear()

    For i = 0 To lvCountVar.Items.Count - 1

    strTemp = Trim(lvCountVar.Items(i).Text)

    If strTemp.EndsWith(";") Then

    strTemp = Microsoft.VisualBasic.Left(strTemp, strTemp.Length - 1)

    End If

    strTemp2 = strTemp

    counter = 0

    For j = 0 To lvCountVar.Items.Count - 1

    strTemp3 = lvCountVar.Items(j).Text

    If strTemp3.EndsWith(";") Then

    strTemp3 = Microsoft.VisualBasic.Left(strTemp3, strTemp3.Length - 1)

    End If

    If strTemp3 = strTemp2 Then

    counter += 1

    End If

    Next j

    If i > 0 Then

    strTemp3 = lvCountVar.Items(i - 1).Text

    If strTemp3.EndsWith(";") Then

    strTemp3 = Microsoft.VisualBasic.Left(strTemp3, strTemp3.Length - 1)

    End If

    End If

    If IsNumeric(strTemp) Then

    If ((i > 0) And (strTemp <> Trim(strTemp3))) Or (i = 0) Then

    cntNum += 1

    totNum += counter

    Dim listitem As New ListViewItem(cntNum)

    listitem.SubItems.Add(strTemp2)

    listitem.SubItems.Add(counter)

    lvwNum.Items.Add(listitem)

    End If

    Else

    If ((i > 0) And (strTemp <> Trim(strTemp3))) Or (i = 0) Then

    cntVar += 1

    totVar += counter

    Dim listitem As New ListViewItem(cntVar)

    listitem.SubItems.Add(strTemp2)

    listitem.SubItems.Add(counter)

    lvwVar.Items.Add(listitem)

    End If

    End If

    Next i

    txtUnqOpd.Text = cntVar + cntNum

    txtVar.Text = cntVar

    txtNum.Text = cntNum

    txtOcrVar.Text = totVar

    txtOcrNum.Text = totNum

    txtOcrOpd.Text = totVar + totNum

    End Sub

    'list out the operators

    Private Sub CountOperator()

    Dim strTemp As String

    Dim strTemp2, strTemp3 As String

    Dim i, j, counter As Integer

    Dim cntVar, totVar As Integer

    lvwOcrOp.Items.Clear()

    For i = 0 To lvwOp.Items.Count - 1

    strTemp = Trim(lvwOp.Items(i).Text)

    If strTemp.EndsWith(";") Then

    strTemp = Microsoft.VisualBasic.Left(strTemp, strTemp.Length - 1)

    End If

    strTemp2 = strTemp

    counter = 0

    For j = 0 To lvwOp.Items.Count - 1

    strTemp3 = lvwOp.Items(j).Text

    If strTemp3.EndsWith(";") Then

    strTemp3 = Microsoft.VisualBasic.Left(strTemp3, strTemp3.Length - 1)

    End If

    If strTemp3 = strTemp2 Then

    counter += 1

    End If

    Next j

    If i > 0 Then

    strTemp3 = lvwOp.Items(i - 1).Text

    If strTemp3.EndsWith(";") Then

    strTemp3 = Microsoft.VisualBasic.Left(strTemp3, strTemp3.Length - 1)

    End If

    End If

     

    If ((i > 0) And (strTemp <> Trim(strTemp3))) Or (i = 0) Then

    cntVar += 1

    totVar += counter

    Dim listitem As New ListViewItem(cntVar)

    listitem.SubItems.Add(strTemp2)

    listitem.SubItems.Add(counter)

    lvwOcrOp.Items.Add(listitem)

    End If

    Next i

    txtOcrOp.Text = totVar

    txtUnqOp.Text = cntVar

    End Sub

     

  • 2 years ago

    Correct me if I'm wrong but basically your reading a file and you want to display the number of times an operator appears.  Your final list would look something like this.

    Operator          Ocurrences
    =                      10
    <                      6
    >                      6


    You can't just condense the list.  What you need to do is calculate the occurences of each operator and once your done you would popullate the listview with the result.  At the moment you are adding each operator found as an item in the listview, at least it looks like you are.  You should add the operator only once with the total number of occurences.  Calculate first...popullate second.

  • 2 years ago

    Dear TwoFaced,

    Yup, you're correct. I managed to populate the occurences, as your suggestion. At least I had achieved in previewing the list.

    Appreciate your advice very much

    TQVM

    azlina

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!