By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,990 Members | 2,138 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,990 IT Pros & Developers. It's quick & easy.

Question on a CheckedListBox.

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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(lstApprover s.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(lstApprover s.Items(e.Index),
ListBoxItem).Data)("Primary") = 0
ElseIf e.NewValue = CheckState.Checked Then

ds.Tables("Approvers").Rows.Find(CType(lstApprover s.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
"Manuel Canas" <mc*****@hotmail.com> wrote in message
news:xdDNc.123017$eO.64363@edtnps89...
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

Nov 20 '05 #2

P: n/a
Thanks Greg. This is giving some ideas for sure.

Thanks again,

Manuel

"Manuel Canas" <mc*****@hotmail.com> wrote in message
news:xdDNc.123017$eO.64363@edtnps89...
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

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.