What im trying to do is when someone checks a certain amount of items say like 6 then they try and select 7 it removes the 7th then a label says "Too many selections" and ive bin searching for like a day now and couldnt find anything and any help would be greatly appreciated

Recommended Answers

All 12 Replies

There is no limitation to the amount of items you can select in a checkedlistbox. This sounds more like something in your code that is causing this.

Here lemme explain this better

Say i have 1 checked list box but i want to limit how many options people can click to 6 but when they hit 7 a label appears and say "To many options selected" but then gets rid of the 7th option they clicked

Here lemme explain this better

Say i have 1 checked list box but i want to limit how many options people can click to 6 but when they hit 7 a label appears and say "To many options selected" but then gets rid of the 7th option they clicked

LOL, ok then its the reverse, through coding you would have to check the selected item count and prevent this.

Private Sub CheckedListBox1_ItemCheck( ) 

        If CheckedListBox1.CheckedItems.Count >= 7 AndAlso e.NewValue = CheckState.Checked Then
            Label1.Text = "To many items selected"
            e.NewValue = CheckState.Unchecked
        End If

    End Sub

alright thanks for that but what would i dim e as?

because it says Name e is not declared?

My coding was in the CheckListBox's ItemCheck event, e is the event parameter that is automatically present if you are in the right control event. I truncated the control event parameters to save text but will show the whole line since it confused you.

Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck

thanks that was very helpful ive done many many projects before this but this one is my hardest due to all the stuff thats in it plus im fairly inexperienced in vb

Anytime :)

lol one more thing im stuck on with this i got the code working but it only works if theres 2 instances of it like for example

If CheckedListBox2.CheckedItems.Count >= 7 Then

            Label73.Text = ("To Many Items Selected")
            e.NewValue = CheckState.Unchecked = CheckState.Unchecked = True
        End If
        If CheckedListBox2.CheckedItems.Count >= 6 Then

            Label73.Text = ("")
            e.NewValue = CheckState.Unchecked = CheckState.Unchecked = True
        End If

But i cant edit anything in the code or the label wont disappear
or even show up

Why is there two blocks of code for the same CheckedListBox control?

Also this is not needed
e.NewValue = CheckState.Unchecked = CheckState.Unchecked = True

Change to simply:
e.NewValue = CheckState.Unchecked

and your label is not showing, because your setting it to a blank string

lmao nevermind i did what you said and it worked :P they way i was doing it just made it seem like it needed two blocks of the same code to work cause whenever i removed it it stopped :P but thanks again

one last question before i mark as solved but when it brings the label up at 7 checks and says "Too Many Items Selected" how would i make it so that when i get rid of the 7th one the label would disappear?

Private Sub CheckedListBox1_ItemCheck( ) 

        If CheckedListBox1.CheckedItems.Count >= 7 AndAlso e.NewValue = CheckState.Checked Then
            lblStatus.Text = "To many items selected"
            e.NewValue = CheckState.Unchecked
        Else
            'Items selected are under 7
            'or they are in the action of deselecting an item
            'clear error message
            lblStatus.Text = ""
        End If

    End Sub

And just as an additional tip, any control that you are assigning values too, you should give a meaningfull name too. Label37 means little when your scrolling through hundreds if not thousands of lines of code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.