none
how to count values in a listbox ? RRS feed

  • Question

  • hello, I want my program to count the values in an row in listview but how ?

    I already searched on google but I could only find how to count all items :/

    (for more info see picture)

    So that he counts all the values (in this case the price) and then returns the total in a textbox.


    Just a developer.

    Thursday, July 18, 2019 4:43 PM

Answers

  • Hi

    Here is some code that may help - it shows how to Sum columns. I just illustrate the Sum of two columns plus the count of items.

    It is a stand alone example using a BLANK Form1.

    ' Blank Form1
    
    ' LISTVIEW EXAMPLE
    ' COPY ITEMS FROM LV1 to LV2
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim WithEvents listView1 As New ListView()
    	Dim l1, l2, l3 As New Label
    	Dim WithEvents b1 As New Button
    	Dim rand As New Random
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		DoubleBuffered = True
    		CreateMyListView()
    		With b1
    			.Text = "Click"
    			.Location = New Point(listView1.Left, listView1.Bottom + 30)
    		End With
    		With l1
    			.Font = New Font(l1.Font.FontFamily, 14)
    			.Location = New Point(b1.Right + 10, b1.Top)
    			.Text = "Label1"
    		End With
    		With l2
    			.Font = New Font(l1.Font.FontFamily, 14)
    			.Location = New Point(l1.Right + 10, l1.Top)
    			.Text = "Label2"
    		End With
    		With l3
    			.Font = New Font(l1.Font.FontFamily, 14)
    			.Location = New Point(l2.Right + 10, l1.Top)
    			.Text = "Label1"
    		End With
    		Controls.AddRange({listView1, b1, l1, l2, l3})
    	End Sub
    	Private Sub CreateMyListView()
    		' Create new ListView.
    		With listView1
    			.Bounds = New Rectangle(New Point(10, 10), New Size(500, 200))
    			'	.Dock = DockStyle.Fill
    			' Set to details.
    			.View = View.Details
    			' Allow item edit.
    			.LabelEdit = True
    			' Allow rearrange columns.
    			.AllowColumnReorder = True
    			' check boxes.
    			.CheckBoxes = True
    			' Select whole row.
    			.FullRowSelect = True
    			' Show grid lines.
    			.GridLines = True
    			' Sort ascending.
    			.LabelWrap = True
    			.Sorting = SortOrder.Ascending
    			listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
    			.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
    			.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
    			.Columns.Add("Column 4", -2, HorizontalAlignment.Center)
    			.Columns.Add("Column 5", -2, HorizontalAlignment.Center)
    			.Columns.Add("Column 6", -2, HorizontalAlignment.Center)
    
    			' make some test data
    			For itm As Integer = 0 To 9
    				Dim item As New ListViewItem("abcdefghijkl" & vbNewLine & "mnopqrstuvwxyz", rand.Next(0, 2))
    				item.Checked = True
    				item.SubItems.Add((rand.NextDouble * rand.Next(100, 199)).ToString())
    				item.SubItems.Add((rand.NextDouble * rand.Next(200, 299)).ToString())
    				item.SubItems.Add((rand.NextDouble * rand.Next(300, 399)).ToString())
    				item.SubItems.Add((rand.NextDouble * rand.Next(300, 399)).ToString())
    				item.SubItems.Add("abcdefghijkl" & vbCrLf & vbCrLf & "mnopqrstuvwxyz")
    				.Items.Add(item)
    			Next
    
    			' Create ImageList objects.
    			Dim imageListSmall As New ImageList()
    			Dim imageListLarge As New ImageList()
    
    			' Initialize ImageList
    			imageListSmall.Images.Add(Image.FromFile(Application.StartupPath & "\Images\42small.png"))
    			imageListSmall.Images.Add(Image.FromFile(Application.StartupPath & "\Images\43small.png"))
    			imageListLarge.Images.Add(Image.FromFile(Application.StartupPath & "\Images\42.png"))
    			imageListLarge.Images.Add(Image.FromFile(Application.StartupPath & "\Images\43.png"))
    
    			'Assign ImageList to ListView.
    			.LargeImageList = imageListLarge
    			.SmallImageList = imageListSmall
    		End With
    	End Sub
    
    	Dim htInfo As ListViewHitTestInfo
        Dim intLast As Integer = 0
        Private Sub LvMain_MouseMove(sender As Object, e As MouseEventArgs) Handles listView1.MouseMove
    		If Not listView1.View = View.Details Then
    			listView1.Items(intLast).BackColor = listView1.BackColor
    		Else
    			htInfo = listView1.HitTest(e.Location)
    
    			If htInfo.Item Is Nothing Then Exit Sub
    			If htInfo.Item.Index <> intLast Then
    				listView1.Items(intLast).Selected = False
    
    				htInfo.Item.Selected = True
    
    				intLast = htInfo.Item.Index
    			End If
    		End If
    	End Sub
    
    	Private Sub B1_Click(sender As Object, e As EventArgs) Handles b1.Click
    		l1.Text = listView1.Items.Count.ToString
    
    		Dim v As Decimal = 0D
    		Dim Total1Value As Decimal = 0D
    		Dim Total2Value As Decimal = 0D
    		For i As Integer = 0 To listView1.Items.Count - 1
    			If Decimal.TryParse(listView1.Items(i).SubItems(2).Text, v) Then
    				Total1Value += v
    			End If
    			If Decimal.TryParse(listView1.Items(i).SubItems(3).Text, v) Then
    				Total2Value += v
    			End If
    		Next
    		l2.Text = Total1Value.ToString("0.00")
    		l3.Text = Total2Value.ToString("0.00")
    	End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by DJJASPER21 Thursday, July 18, 2019 5:47 PM
    Thursday, July 18, 2019 5:37 PM

All replies

  • Hello,

    What do you want a count of, total rows as there is a Count property or do you want something else?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, July 18, 2019 5:14 PM
    Moderator
  • hello, he must add the 2's, the intention is that he makes a invoice with this data.

    Just a developer.

    Thursday, July 18, 2019 5:32 PM
  • Hi

    Here is some code that may help - it shows how to Sum columns. I just illustrate the Sum of two columns plus the count of items.

    It is a stand alone example using a BLANK Form1.

    ' Blank Form1
    
    ' LISTVIEW EXAMPLE
    ' COPY ITEMS FROM LV1 to LV2
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim WithEvents listView1 As New ListView()
    	Dim l1, l2, l3 As New Label
    	Dim WithEvents b1 As New Button
    	Dim rand As New Random
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		DoubleBuffered = True
    		CreateMyListView()
    		With b1
    			.Text = "Click"
    			.Location = New Point(listView1.Left, listView1.Bottom + 30)
    		End With
    		With l1
    			.Font = New Font(l1.Font.FontFamily, 14)
    			.Location = New Point(b1.Right + 10, b1.Top)
    			.Text = "Label1"
    		End With
    		With l2
    			.Font = New Font(l1.Font.FontFamily, 14)
    			.Location = New Point(l1.Right + 10, l1.Top)
    			.Text = "Label2"
    		End With
    		With l3
    			.Font = New Font(l1.Font.FontFamily, 14)
    			.Location = New Point(l2.Right + 10, l1.Top)
    			.Text = "Label1"
    		End With
    		Controls.AddRange({listView1, b1, l1, l2, l3})
    	End Sub
    	Private Sub CreateMyListView()
    		' Create new ListView.
    		With listView1
    			.Bounds = New Rectangle(New Point(10, 10), New Size(500, 200))
    			'	.Dock = DockStyle.Fill
    			' Set to details.
    			.View = View.Details
    			' Allow item edit.
    			.LabelEdit = True
    			' Allow rearrange columns.
    			.AllowColumnReorder = True
    			' check boxes.
    			.CheckBoxes = True
    			' Select whole row.
    			.FullRowSelect = True
    			' Show grid lines.
    			.GridLines = True
    			' Sort ascending.
    			.LabelWrap = True
    			.Sorting = SortOrder.Ascending
    			listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
    			.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
    			.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
    			.Columns.Add("Column 4", -2, HorizontalAlignment.Center)
    			.Columns.Add("Column 5", -2, HorizontalAlignment.Center)
    			.Columns.Add("Column 6", -2, HorizontalAlignment.Center)
    
    			' make some test data
    			For itm As Integer = 0 To 9
    				Dim item As New ListViewItem("abcdefghijkl" & vbNewLine & "mnopqrstuvwxyz", rand.Next(0, 2))
    				item.Checked = True
    				item.SubItems.Add((rand.NextDouble * rand.Next(100, 199)).ToString())
    				item.SubItems.Add((rand.NextDouble * rand.Next(200, 299)).ToString())
    				item.SubItems.Add((rand.NextDouble * rand.Next(300, 399)).ToString())
    				item.SubItems.Add((rand.NextDouble * rand.Next(300, 399)).ToString())
    				item.SubItems.Add("abcdefghijkl" & vbCrLf & vbCrLf & "mnopqrstuvwxyz")
    				.Items.Add(item)
    			Next
    
    			' Create ImageList objects.
    			Dim imageListSmall As New ImageList()
    			Dim imageListLarge As New ImageList()
    
    			' Initialize ImageList
    			imageListSmall.Images.Add(Image.FromFile(Application.StartupPath & "\Images\42small.png"))
    			imageListSmall.Images.Add(Image.FromFile(Application.StartupPath & "\Images\43small.png"))
    			imageListLarge.Images.Add(Image.FromFile(Application.StartupPath & "\Images\42.png"))
    			imageListLarge.Images.Add(Image.FromFile(Application.StartupPath & "\Images\43.png"))
    
    			'Assign ImageList to ListView.
    			.LargeImageList = imageListLarge
    			.SmallImageList = imageListSmall
    		End With
    	End Sub
    
    	Dim htInfo As ListViewHitTestInfo
        Dim intLast As Integer = 0
        Private Sub LvMain_MouseMove(sender As Object, e As MouseEventArgs) Handles listView1.MouseMove
    		If Not listView1.View = View.Details Then
    			listView1.Items(intLast).BackColor = listView1.BackColor
    		Else
    			htInfo = listView1.HitTest(e.Location)
    
    			If htInfo.Item Is Nothing Then Exit Sub
    			If htInfo.Item.Index <> intLast Then
    				listView1.Items(intLast).Selected = False
    
    				htInfo.Item.Selected = True
    
    				intLast = htInfo.Item.Index
    			End If
    		End If
    	End Sub
    
    	Private Sub B1_Click(sender As Object, e As EventArgs) Handles b1.Click
    		l1.Text = listView1.Items.Count.ToString
    
    		Dim v As Decimal = 0D
    		Dim Total1Value As Decimal = 0D
    		Dim Total2Value As Decimal = 0D
    		For i As Integer = 0 To listView1.Items.Count - 1
    			If Decimal.TryParse(listView1.Items(i).SubItems(2).Text, v) Then
    				Total1Value += v
    			End If
    			If Decimal.TryParse(listView1.Items(i).SubItems(3).Text, v) Then
    				Total2Value += v
    			End If
    		Next
    		l2.Text = Total1Value.ToString("0.00")
    		l3.Text = Total2Value.ToString("0.00")
    	End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by DJJASPER21 Thursday, July 18, 2019 5:47 PM
    Thursday, July 18, 2019 5:37 PM
  • It worked thanks !


    Just a developer.

    Thursday, July 18, 2019 5:53 PM