Changing the Cheked List Box

nabbyg

Member
Joined
May 10, 2005
Messages
16
Programming Experience
1-3
In a real pickle here guys, I want to limit the user to a maximum of three selections in the checked list box. And I am using the following code.

1 If count = 3 Then

2 Lowestindex = CheckedListBox3.CheckedIndices.Item(0)

3 CheckedListBox3.SetItemCheckState (Lowestindex,CheckState.Unchecked)

4 End If


I keep getting the stack overflow error, for some reason the program does want to come out of the statement on line 3.

Thanks,
Nabby

 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,321
Location
Sydney, Australia
Programming Experience
10+
Given that you probably don't know which checked item the user would want to uncheck when they attempt to check a fourth item, I would suggest preventing them from checking a fourth item at all. Make them uncheck an item first. You can accomplish this using the ItemCheck event of the CheckedListBox.
VB.NET:
[color=Blue]Private Sub[/color] CheckedListBox3_ItemCheck([color=Blue]ByVal[/color] sender [color=Blue]As Object[/color], [color=Blue]ByVal[/color] e [color=Blue]As[/color] System.Windows.Forms.ItemCheckEventArgs) [color=Blue]Handles[/color] CheckedListBox3.ItemCheck
	[color=Blue]If [color=black]e.NewValue = CheckState.Checked[/color] AndAlso Me[/color].CheckedListBox3.CheckedItems.Count = 3
		e.NewValue = CheckState.Unchecked
		MessageBox.Show("No more than 3 selections allowed. Please uncheck an item before making another selection.")
	[color=Blue]End If[/color]
[color=Blue] End Sub[/color]
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,321
Location
Sydney, Australia
Programming Experience
10+
Just for the record, line 3 of the code you posted would raise the ItemCheck event on your CheckedListBox. I assume that code was in the ItemCheck event handler, which would cause endless recursive calls to the event handler.
 
Top Bottom