Windows Forms: Keeping Check Beside Checked Box list item and displaying separate message for each

202 views Asked by At

When I run my program currently, Within any checked box list I am unable to actually select an option (i.e. the box beside it, to tick).

I would also like each individual option to display a messagebox when ticked (i.e. "User story one added"), But currently my program only displays a general messagebox ("User Story selected") once the checked box list is clicked on. Any help would be much appreciated!

Current Code:

Public Class Form2

    Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
        MessageBox.Show("User Story Selected")
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Form3.Show()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Hide()
        Form1.Show()
    End Sub

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class
1

There are 1 answers

7
Ňɏssa Pøngjǣrdenlarp On

You are responding to the wrong event and might be likely to examine the wrong property.

Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, _
  ByVal e As System.Windows.Forms.ItemCheckEventArgs)
    MessageBox.Show("User Story Selected")
End Sub

Checks fire the ItemCheck event, and checked items are in the CheckedItems Collection. The SelectedItems collection is literally those selected (highlighted) which might not also be checked. It is not really a list of checkboxes, but a list of items drawn as checks - thats why they look different than regular checks. To see which item:

For n as Integer = 0  to CheckedListBox1.CheckedItems.Count-1
    userWants = CheckedListBox1.CheckedItems(n)
Next n

Like a ListBox you can put anything in there, not just strings, so it was a list of stories, you might be able to do:

 userWants = CheckedListBox1.CheckedItems(n).StoryName