Count the items in a listview
Solved
mylord666
Posted messages
162
Status
Member
-
mylord666 Posted messages 162 Status Member -
mylord666 Posted messages 162 Status Member -
Hello,
everything is working fine now, but there's another problem and I'm really struggling with it.
how to identify an item in a column and display the row of that item, and then I would like to sum the identified items.
thank you in advance
everything is working fine now, but there's another problem and I'm really struggling with it.
how to identify an item in a column and display the row of that item, and then I would like to sum the identified items.
thank you in advance
6 answers
Hello,
Out of courtesy, could you respond to the requests you made
https://forums.commentcamarche.net/forum/affich-31622930-listview-vb8-express
--
@+ Le Pivert
Out of courtesy, could you respond to the requests you made
https://forums.commentcamarche.net/forum/affich-31622930-listview-vb8-express
--
@+ Le Pivert
mylord666
Posted messages
162
Status
Member
I'm sorry, I ask you to forgive me, it was unintentional on my part.
No worries
here's an example:
http://www.cjoint.com/data3/3BBiCgapMIF.htm
Happy coding
--
@+ The Woodpecker
here's an example:
http://www.cjoint.com/data3/3BBiCgapMIF.htm
Happy coding
--
@+ The Woodpecker
It's normal I'm using VB2010:
Open a new project, put a listview and 4 buttons
3 TextBox named:
Textnom
Textprenom
Textnaissance
a label named:
Label4
Add a new Form
Form2 with a listview
Here is the code to put in Form1:
And in Form2:
By following these instructions, it should work!
--
@+ Le Pivert
Open a new project, put a listview and 4 buttons
3 TextBox named:
Textnom
Textprenom
Textnaissance
a label named:
Label4
Add a new Form
Form2 with a listview
Here is the code to put in Form1:
Imports System.IO Public Class Form1 Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter() Dim ligne As Integer Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Using fs As New System.IO.FileStream("sauvegarde.txt", IO.FileMode.Create) bf.Serialize(fs, New ArrayList(ListView1.Items)) End Using End Sub Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Button1.Text = "Add item" Button2.Text = "Remove item" Button3.Text = "Open Form2" Button4.Text = "Save data" Label4.Text = "Checked items" 'The ListView1 exists ListView1.View = View.Details ListView1.CheckBoxes = True ListView1.Columns.Add("Name", 80, HorizontalAlignment.Left) ListView1.Columns.Add("First Name", 80, HorizontalAlignment.Left) ListView1.Columns.Add("Year of Birth", 100, HorizontalAlignment.Left) Try If File.Exists("sauvegarde.txt") Then Using fs As New System.IO.FileStream("sauvegarde.txt", IO.FileMode.Open) ListView1.Items.AddRange(bf.Deserialize(fs).ToArray(GetType(ListViewItem))) End Using End If Catch End Try End Sub 'add item Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click If Textnom.Text = "" Or Textprenom.Text = "" Or Textnaissance.Text = "" Then Exit Sub Dim MyLine As ListViewItem = New ListViewItem(New String() {Textnom.Text, Textprenom.Text, Textnaissance.Text}) ListView1.Items.Add(MyLine) End Sub 'remove item Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click If ListView1.SelectedItems.Count > 0 Then ListView1.Items.RemoveAt(ligne) End If End Sub 'select line Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged If ListView1.SelectedItems.Count > 0 Then ligne = ListView1.SelectedItems(0).Index End If End Sub 'check item by item Private Sub ListView1_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles ListView1.ItemChecked If ListView1.Items.Count > 0 Then ' if listview1 is not empty For j = 0 To ListView1.Items.Count - 1 ' color based on checked case For item 'ListView1.Items(j).ForeColor = IIf(ListView1.Items(j).Checked = False, Color.Black, Color.Red) Next j Label4.Text = ListView1.CheckedItems.Count & " selected" & IIf(ListView1.CheckedItems.Count > 1, "s", "") 'count selected files End If End Sub 'open Form2 Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click Form2.ShowDialog() End Sub 'save data Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click Using fs As New System.IO.FileStream("sauvegarde.txt", IO.FileMode.Create) bf.Serialize(fs, New ArrayList(ListView1.Items)) End Using End Sub End Class And in Form2:
Imports System.IO Public Class Form2 Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter() Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 'The ListView1 exists ListView1.View = View.Details ListView1.CheckBoxes = True ListView1.Columns.Add("Name", 80, HorizontalAlignment.Left) ListView1.Columns.Add("First Name", 80, HorizontalAlignment.Left) ListView1.Columns.Add("Year of Birth", 100, HorizontalAlignment.Left) ListView1.Items.Clear() Try If File.Exists("sauvegarde.txt") Then Using fs As New System.IO.FileStream("sauvegarde.txt", IO.FileMode.Open) ListView1.Items.AddRange(bf.Deserialize(fs).ToArray(GetType(ListViewItem))) End Using End If Catch End Try End Sub End Class By following these instructions, it should work!
--
@+ Le Pivert
Put a button in Form2 with this code:
There you go
--
@+ Le Pivert
Dim ligne As Integer 'selection line Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged If ListView1.SelectedItems.Count > 0 Then ligne = ListView1.SelectedItems(0).Index End If End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim var1 As String = ListView1.Items(ligne).Text Dim var2 As String = ListView1.Items(ligne).SubItems(1).Text Dim var3 As String = ListView1.Items(ligne).SubItems(2).Text ' Do whatever you want with the variables End Sub
There you go
--
@+ Le Pivert
If you want to progress in VB.NET, you need to learn the fundamentals. Variable declarations, etc.
See this:
https://plasserre.developpez.com/cours/vb-net/?page=langage-vb2#LV-D
Copy-pasting won't help you move forward; on the contrary, you need to understand what you're doing. It’s by following these tips that you'll be able to go further.
I started programming at 60 years old; you see, nothing is lost, you just need to get started.
See you later, The Woodpecker
See this:
https://plasserre.developpez.com/cours/vb-net/?page=langage-vb2#LV-D
Copy-pasting won't help you move forward; on the contrary, you need to understand what you're doing. It’s by following these tips that you'll be able to go further.
I started programming at 60 years old; you see, nothing is lost, you just need to get started.
See you later, The Woodpecker
Try to download here:
https://www.commentcamarche.net/telecharger/developpement/23259-visual-basic-express/
--
@+ The Woodpecker
https://www.commentcamarche.net/telecharger/developpement/23259-visual-basic-express/
--
@+ The Woodpecker