Question on a CheckedListBox.

M

Manuel Canas

Hi there,

I'm having this dilema with a checkedlistbox.

I have an array of items in there, what I want to accomplish is the
following;

The user could check all the items in the checkedlistbox, but always have at
list one item always checked. so if the user has the last item checked and
he/she wants to uncheck it, the item should reset it self to a checked state
again.

Been trying to think of the login that this should follow, but I just not
having luck here.

Any ideas, hits, advices would so much Appreciated

Thanks,

Manuel
 
G

Greg Burns

Not the answer to YOUR problem, but here is some code I have that ensures
that at least one and only one item is checked in a checkedlistbox. Might
give you some ideas to get started.

(background: my list is a list of approvers, the rule is that there can only
be one "primary" approver. you indicate primary by placing the checkmark)

Looking at this code right now is making my head hurt, but it all made sense
to me a few months ago. :^)

HTH,
Greg



Private Programatically As Boolean
Private Loading As Boolean

Sub New()
Loading = True
' bind checkedlistbox here
Loading = False
End Sub

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

If Loading Then Exit Sub

If e.NewValue = CheckState.Checked And e.CurrentValue =
CheckState.Unchecked Then

If DirectCast(sender, CheckedListBox).CheckedItems.Count > 0
Then Call ClearCheckBoxes(DirectCast(sender, CheckedListBox), False)

ds.Tables("Approvers").Rows.Find(CType(lstApprovers.Items(e.Index),
ListBoxItem).Data)("Primary") = 1
End If

If Not Programatically And e.NewValue = CheckState.Unchecked Then
e.NewValue = CheckState.Checked
ElseIf e.NewValue = CheckState.Unchecked Then

ds.Tables("Approvers").Rows.Find(CType(lstApprovers.Items(e.Index),
ListBoxItem).Data)("Primary") = 0
ElseIf e.NewValue = CheckState.Checked Then

ds.Tables("Approvers").Rows.Find(CType(lstApprovers.Items(e.Index),
ListBoxItem).Data)("Primary") = 1

End If
End Sub

Private Sub ClearCheckBoxes(ByRef CheckedList As CheckedListBox, ByVal
ClearSelected As Boolean)
Programatically = True
Dim i As Integer

If CheckedList.CheckedItems.Count > 0 Then

For i = 0 To CheckedList.Items.Count - 1

If CheckedList.GetItemChecked(i) Then

If CheckedList.SelectedIndex = i Then

If ClearSelected Then CheckedList.SetItemChecked(i,
False)

Else

CheckedList.SetItemChecked(i, False)


End If

End If

Next

End If
Programatically = False

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top