Results 1 to 9 of 9

Thread: listview - count checked items

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Posts
    336

    listview - count checked items

    Hope someone can help..........

    Code:
        Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
            If CheckBox1.Checked = True Then
                For Each CheckedApp As ListViewItem In App_List.Items
                    CheckedApp.Checked = True
                    Button1.Text = "Start installing (" + App_List.Items.Count > -1 + " apps.)"
                Next
            ElseIf CheckBox1.Checked = False Then
                For Each CheckedItem As ListViewItem In App_List.Items
                    CheckedItem.Checked = False
                Next
            End If
        End Sub
    problem, checkbox1 isn't checking items and the button text, i not sure works....

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: listview - count checked items

    Your question (and I use the term loosely) doesn't really make sense. The title mentions counting checked items but your code doesn't seem to have anything to do with counting. I'm guessing that English is not your first language but please try to provide a FULL and CLEAR explanation of what you're actually trying to achieve instead of expecting us to work it out from code that doesn't actually work.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: listview - count checked items

    I don't understand the question either...

  4. #4

    Re: listview - count checked items

    Code:
    Button1.Text = "Start installing (" + App_List.Items.Count > -1 + " apps.)"
    Shouldn't that be
    Code:
    Button1.Text = "Start installing (" + (App_List.Items.Count - 1).ToString() + " apps.)"
    ?

    Other than that, I can't really guess what's wrong.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: listview - count checked items

    formlesstree4 is right, but also you're resetting Button1.Text for every item in your loop. in vb2012:

    Code:
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        Array.ForEach(App_List.Items.Cast(Of ListViewItem).ToArray, Sub(lvi) lvi.Checked = CheckBox1.Checked)
        Button1.Text = If(CheckBox1.Checked, "Start installing (" + (App_List.Items.Count > -1).ToString + " apps.)", "some other string")
    End Sub

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Posts
    336

    Re: listview - count checked items

    Checkbox1 should check allitems in listtview but is not...

    and am not sure if the button text that counts, works..

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: listview - count checked items

    try my code from post #5
    I tested it + I know it works, as does your original code with formlesstree4's modification

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Posts
    336

    Re: listview - count checked items

    I got error: Error 1 'Cast' is not a member of 'System.Windows.Forms.ListView.ListViewItemCollection'. H:\X17-59183\PostInstall\PostInstall\SelectPrograms.vb 40 23 PostInstall

    plus what exactly does the code do?

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: listview - count checked items

    It checks or unchecks all items + sets the button text.

    Which version of vb are you using + which framework are you targetting?
    Last edited by .paul.; May 11th, 2013 at 12:39 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width