Count the items in a listview

Solved
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

6 answers

  1. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    No worries

    here's an example:

    http://www.cjoint.com/data3/3BBiCgapMIF.htm

    Happy coding
    --
    @+ The Woodpecker
    0
    1. mylord666 Posted messages 162 Status Member
       
      Hello, I'm sorry, I can't find that folder. When I click on it, another folder appears.
      Thanks again for your precious help.
      0
    2. cs_Le Pivert Posted messages 8437 Status Contributor 730
       
      I just downloaded the file; it's the correct folder.
      It's a zip file, you need to unzip it to access the folder.
      0
      1. mylord666 Posted messages 162 Status Member > cs_Le Pivert Posted messages 8437 Status Contributor
         
        I'm sorry, but it tells me that it was made with a newer software version than mine, so I can't open it. I'm really sorry and probably a novice.
        0
  2. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    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:

    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
    0
    1. mylord666 Posted messages 162 Status Member
       
      Thank you very much for your patience with me, I'll test it right away.
      0
    2. mylord666 Posted messages 162 Status Member
       
      It works very well thank you again I will progress in my program a bit complicated but it gets the neurons working thank you for your patience.
      I only want to copy the selected line in the listview2 how should I do it?
      0
  3. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    Put a button in Form2 with this code:

     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
    0
    1. mylord666 Posted messages 162 Status Member
       
      Thank you again for your help. I copied this code, but I have just a small issue: I have an error saying "line is not declared". How can I fix it? I don't know how to do it, sorry again.
      0
    2. cs_Le Pivert Posted messages 8437 Status Contributor 730
       
      You didn't copy everything!!!

      And what is this:

      Dim line As Integer

      It needs to be added with your other declarations.
      0
    3. mylord666 Posted messages 162 Status Member
       
      Yes, I copied everything and it works well; it was during the copying that an error occurred. Thank you again, this allows me to move forward a bit more. Have a nice day.
      0
    4. cs_Le Pivert Posted messages 8437 Status Contributor 730
       
      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
      0
      1. mylord666 Posted messages 162 Status Member > cs_Le Pivert Posted messages 8437 Status Contributor
         
        Hello, thank you for your advice. I'm 65 years old and I'm progressing slowly. However, I just got a new PC with Vista, of course, 64 bits, and I'm unable to load VB2010. I get the message to update to SP1, which is already installed. What can I do? Can you help me? Thank you in advance.
        0
  4. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    Here is an example in VB2008:

    http://www.cjoint.com/data3/3Ccpjse2gAK.htm

    --
    @+ The Woodpecker
    0
    1. mylord666 Posted messages 162 Status Member
       
      Thank you for your help, I was able to load vb2010, it works well, I'm ready to go further, thanks again.
      0