Results 1 to 7 of 7

Thread: How to check the checkboxes in the listview?

  1. #1
    Hyperactive Member
    Join Date
    Sep 10
    Posts
    309

    How to check the checkboxes in the listview?

    Hi guys,

    I am working on my listview which I have almost finish it, but i need to work on the button event to check for each string in the listview whether if the checkbox is true or false. When I tick and untick on the checkboxes to add the strings in the label, I want to know how i can check them out through on the listview to see if the checkbox is true or false?


    Here's the code:

    Code:
    Private Function GetEnabledDisabledText(ByVal enabled As Boolean) As String
    	Return (If(enabled, "Enabled", "Disabled"))
    End Function
    
    Private Shared subItems1 As New List(Of String)()
    
    Private Sub listView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    	Dim strItem As String = listView1.HitTest(e.Location).Item.SubItems(1).Text
    
    	If subItems1.Contains(strItem) Then
    	   subItems1.Remove(strItem)
    	Else
    	   subItems1.Add(listView1.HitTest(e.Location).Item.SubItems(1).Text)
    	End If
    	
    	label3.Text = String.Join(", ", subItems1.ToArray())
    End Sub
    
    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    	button1.Enabled = False
    	Dim a() As String = Nothing
    	a = label3.Text.Split(","c)
    	Dim j As Integer = 0
    	Dim str As Object = GetEnabledDisabledText(listView1.Items(j).Checked)
    	MessageBox.Show(a(j).Trim() & " is " & str.ToString())
    End Sub
    Do you know how I can get the messagebox to display and tells me if the checkboxes in the listview compare to each string that I add in the label is true or false?

    Can anyone help me with this?

  2. #2
    Addicted Member
    Join Date
    Sep 11
    Location
    Seattle
    Posts
    191

    Re: How to check the checkboxes in the listview?

    I have absolutely no idea what you are asking.

    Can you break down the steps of your program using:

    1.
    2.
    3.
    4.

    And then tell us what part of that sequence you are having trouble with.

    Right now, the best I can gather is:

    1. The program loads up
    2. You add items to a listbox
    3. You can check or uncheck items in the listbox
    4. You click a button
    5. If the item is unchecked it is deleted?
    6. A message box appears with what items are checked.

    I have no idea where the label fits in, so be more descriptive when you write out the steps

  3. #3
    Hyperactive Member
    Join Date
    Sep 10
    Posts
    309

    Re: How to check the checkboxes in the listview?

    Ok, here is a break down of a step of what I want to do:

    1. The program loads up
    2. I add the items to a listview
    3. I tick and untick the items in the listview to add each strings in the label like mystrings 1, mystrings 2
    4. I click a button to split the strings in the label and check each strings from the label to the listview to see if each strings in the listview have checked and unchecked checkboxes
    5. A message box appears with each items if the checkboxes are checked or unchecked.


    Hope it will make sense of what i am trying to do.

  4. #4
    Burning Member Niya's Avatar
    Join Date
    Nov 11
    Posts
    3,114

    Re: How to check the checkboxes in the listview?

    If you are adding Strings from clicking checks in the ListView, then its not necessary to go over the list in the Label to match them to a ListView item since it was the ListView that created them in the first place. That's really not efficient. Why would you want to do that anyway ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | Create Sortable BindingList(not mine) | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  5. #5
    PowerPoster Edgemeal's Avatar
    Join Date
    Sep 06
    Location
    WindowFromPoint(x,y)
    Posts
    3,135

    Re: How to check the checkboxes in the listview?

    Quote Originally Posted by PillKilla View Post
    3. I tick and untick the items in the listview to add each strings in the label like mystrings 1, mystrings 2

    #3 - So the listview string item is added to a label when you tick a checkbox and also when you untick a checkbox?

    BTW you asked this same question a couple days ago here, whats changed? Why not reply to that thread?

  6. #6
    Hyperactive Member
    Join Date
    Sep 10
    Posts
    309

    Re: How to check the checkboxes in the listview?

    Quote Originally Posted by Edgemeal View Post

    #3 - So the listview string item is added to a label when you tick a checkbox and also when you untick a checkbox?

    BTW you asked this same question a couple days ago here, whats changed? Why not reply to that thread?

    Yes, when i tick and untick a checkbox, it will add the string item to a label.

    So now I am working on the button event to compare the string item from a label and the listview as i want to check if the listview items is true or false. Can you please help me with that?

    As for my previous thread, nobody is answers so i think i would start a new one and explains a bit better.

  7. #7
    PowerPoster Edgemeal's Avatar
    Join Date
    Sep 06
    Location
    WindowFromPoint(x,y)
    Posts
    3,135

    Re: How to check the checkboxes in the listview?

    Quote Originally Posted by PillKilla View Post
    Yes, when i tick and untick a checkbox, it will add the string item to a label.

    So now I am working on the button event to compare the string item from a label and the listview as i want to check if the listview items is true or false. Can you please help me with that?
    You could try this, it would only work if there were no duplicate subitems(1), but I'm still not exactly sure what you're trying to archive. Just forget about using a label to compare data, use a list of (T), where T is a string, or a class that holds more then one property, etc, good luck!

    Code:
    Private Shared subItems1 As New List(Of String)()
    
    Private Sub ListView1_ItemCheck(sender As Object, e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
        ' add subitem(1) to subItems1 list if item is not already,
        ' in the list when a checkbox is ticked or unticked!
        If Not subItems1.Contains(ListView1.Items(e.Index).SubItems(1).Text) Then
            subItems1.Add(ListView1.Items(e.Index).SubItems(1).Text)
        End If
    
        ' dipsplay the ticked/unticked items
        Label3.Text = String.Join(", ", subItems1.ToArray())
    End Sub
    
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim output As New System.Text.StringBuilder
    
        ' Show which items were ticked/unticked and curernt checked state.
        For i = 0 To ListView1.Items.Count - 1
            With ListView1.Items(i)
                If subItems1.Contains(.SubItems(1).Text) Then
                    If .Checked Then
                        output.Append("row " & i.ToString & " (" & .SubItems(1).Text & ") " & "is checked" & vbCrLf)
                    Else
                        output.Append("row " & i.ToString & " (" & .SubItems(1).Text & ") " & "is not checked" & vbCrLf)
                    End If
                End If
            End With
        Next
    
        ' show results
        If output.Length > 0 Then
            MessageBox.Show(output.ToString)
        Else
            MessageBox.Show("List is empty")
        End If
    
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •