Community discussion forum
ListView-How to use it?
-
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 SubEnd
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 Integeri = 0
While sr.Peek <> -1aLine = sr.ReadLine().Trim()
If aLine.StartsWith("(*") Then While sr.Peek <> -1 If aLine.IndexOf("*)") <> -1 Then Exit While End IfaLine = sr.ReadLine
End While ElsestrTemp = Replace(aLine, vbTab,
" ")strTemp = Replace(strTemp, vbCr,
" ")strTemp = Replace(strTemp, vbLf,
" ")strTemp = Trim(strTemp)
Do While InStr(1, strTemp, " ", 1) <> 0strTemp = Replace(strTemp,
" ", " ") LoopmyArray = (Split(strTemp,
" ", -1, 1)) For Each s In myArray If (UCase(s) = "IF") TheniIF += 1
ElseIf (UCase(s) = "WHILE") TheniWhile += 1
End If Next End If End WhilelvwLoop.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 + dtxtVG.Text = (iIF + iWhile) + 1
'calculate Tree ImpurityImpurity = (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
-
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 'MainTo This<STATHREAD()> _ Shared Sub Main() Application.EnableVisualStyles() Application.Run(New Form1()) End Sub 'MainIf 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. -
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 Form1End
ClassAfter 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
SystemImports
System.CollectionsImports
System.Windows.FormsPublic
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 ThenmyListView.Sorting = SortOrder.Ascending
ElsemyListView.Sorting = SortOrder.Descending
End IfgroupColumn = e.Column
' Set the groups to those created for the clicked column. If isRunningXPOrLater ThenSetGroups(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 ListViewGroupgroups.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 ThensubItemText = 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 ThensubItemText = 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) Thengroups.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 SubEnd
Class -
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 ClassThe 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. -
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 -
Dear TwoFaced,
I appreciated very much what had u done to me in helping me to understand ListView.
![Embarrassed [:$]](/emoticons/emotion-10a.gif)
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]](/emoticons/emotion-2a.gif)
TQVM
![Yes [Y]](/emoticons/emotion-21.gif)
-
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 ListViewItemlvCountVar.Items.Clear()
lvwOp.Items.Clear()
i = 0
While sr.Peek <> -1LOC = sr.ReadLine().Trim()
If LOC.StartsWith("/*") Then While sr.Peek <> -1 If LOC.IndexOf("*/") <> -1 Then Exit While End IfLOC = sr.ReadLine
End While ElsestrTemp = Replace(LOC, vbTab,
" ")strTemp = Replace(strTemp, vbCr,
" ")strTemp = Replace(strTemp, vbLf,
" ")strTemp = Trim(strTemp)
Do While InStr(1, strTemp, " ", 1) <> 0strTemp = Replace(strTemp,
" ", " ") LoopmyArray = (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 ThenlvCountVar.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 ThenlvCountVar.Items.Add(myArray.GetValue(Array.IndexOf(myArray, s) + 1))
lvwOp.Items.Add(s)
i = i + 1
End If Next End If End WhileCountOperator()
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 IntegerlvwVar.Items.Clear()
lvwNum.Items.Clear()
For i = 0 To lvCountVar.Items.Count - 1strTemp = Trim(lvCountVar.Items(i).Text)
If strTemp.EndsWith(";") ThenstrTemp = Microsoft.VisualBasic.Left(strTemp, strTemp.Length - 1)
End IfstrTemp2 = strTemp
counter = 0
For j = 0 To lvCountVar.Items.Count - 1strTemp3 = lvCountVar.Items(j).Text
If strTemp3.EndsWith(";") ThenstrTemp3 = Microsoft.VisualBasic.Left(strTemp3, strTemp3.Length - 1)
End If If strTemp3 = strTemp2 Thencounter += 1
End If Next j If i > 0 ThenstrTemp3 = lvCountVar.Items(i - 1).Text
If strTemp3.EndsWith(";") ThenstrTemp3 = 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) ThencntNum += 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) ThencntVar += 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 itxtUnqOpd.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 IntegerlvwOcrOp.Items.Clear()
For i = 0 To lvwOp.Items.Count - 1strTemp = Trim(lvwOp.Items(i).Text)
If strTemp.EndsWith(";") ThenstrTemp = Microsoft.VisualBasic.Left(strTemp, strTemp.Length - 1)
End IfstrTemp2 = strTemp
counter = 0
For j = 0 To lvwOp.Items.Count - 1strTemp3 = lvwOp.Items(j).Text
If strTemp3.EndsWith(";") ThenstrTemp3 = Microsoft.VisualBasic.Left(strTemp3, strTemp3.Length - 1)
End If If strTemp3 = strTemp2 Thencounter += 1
End If Next j If i > 0 ThenstrTemp3 = lvwOp.Items(i - 1).Text
If strTemp3.EndsWith(";") ThenstrTemp3 = Microsoft.VisualBasic.Left(strTemp3, strTemp3.Length - 1)
End If End If
If ((i > 0) And (strTemp <> Trim(strTemp3))) Or (i = 0) ThencntVar += 1
totVar += counter
Dim listitem As New ListViewItem(cntVar)listitem.SubItems.Add(strTemp2)
listitem.SubItems.Add(counter)
lvwOcrOp.Items.Add(listitem)
End If Next itxtOcrOp.Text = totVar
txtUnqOp.Text = cntVar
End Sub -
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
> 6You 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.
-
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
Related discussion
-
MOS Protocol - Anyone used it?
by alexnavarro38 (5 replies)
-
Read HSQLDB data into a webpage
by joe90 (3 replies)
-
VB.NET Simple Client / Server Communication
by r0bbyw (3 replies)
-
Dot NET questions.
by amohanece (0 replies)
-
Any certification on asp .net in 2 months
by alohamora (3 replies)
Related articles
Quick links
Recent activity
- Van Thieu replied to C++ CATMULL-ROM
- Mark Neal replied to Which Blu-ray should i choose?
- emma limei replied to How to Download and Play Yo...
- jump tracy replied to How to transfer or copy so...
- Mark Neal replied to Pavtube Blu-ray Ripper for ...
- tt zhao replied to Which Blu-ray should i choose?
Enter your message below
Sign in or Join us (it's free).