Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CheckedListBox SetItemChecked method not working.

534 views
Skip to first unread message

Adam Denby

unread,
May 14, 2003, 9:24:44 AM5/14/03
to
Hi -

I have a databound CheckedListBox and I want to default
all the items in the list to checked. Can anyone please
explain to me why this code does not work? It populates
the list but the items aren't checked.

JobTypes_L has two columns : JobTypeID(int) & Description
(varchar)

clbJobTypes.DataSource = objParent.JobManagerDataSet.Tables
("JobTypes_L")
clbJobTypes.DisplayMember = "Description"
clbJobTypes.ValueMember = "JobTypeID"
For i = 0 To clbJobTypes.Items.Count - 1
clbJobTypes.SetItemChecked(i, True)
Next


Thanks in advance,

Adam.

Adam Denby

unread,
May 16, 2003, 9:42:10 AM5/16/03
to
MS can you please help! I am an MSDN Universal subscriber!

>.
>

Ali Eghtebas

unread,
May 21, 2003, 4:09:59 AM5/21/03
to
Hi Adam,

I think I know why your code is not working! My guess is that you have put
your code in the form load or somewhere, where the form or the panel
containing the CheckedListBox or whatever container it has, is not loaded
and visible yet. That's to say your code is executed before the
CheckedListBox is visible.
What happens is that the items actually get checked but due to a bug in a
databound CheckedListBox, all the checked items lose their checked status
when the control gets visible. I think the reason of this bug is that a
CheckedListBox inherits from ListBox and its databinding capabilities, but
doesn't support for binding to the Checked "property" for each item in the
CheckedListBox.

The issue is easy to reproduce. Just hide and show a databound
CheckedListBox and you will notice how the previously checked items get
unchecked.

So put your code where it's executed after the CheckedListBox is visible.
And if you have to hide and show your CheckedListBox of any reason, my
workaround for this issue is to put the ValueMember of the checked items in
the CheckedListBox.Tag, delimited by e.g. | or any special character that
you know the ValueMember is not equal to. Then after the CheckedListBox is
shown, re-check the items with the stored ValueMembers. Or simply set it
Location out of the form to hide it without changing its visibility.

Note that the ComboBox has a similar issue changing its SelectedIndex
from -1 to 0 if its visibility changes in the same manner. And god knows
what other controls suffer from this issue.

--
Regards
Ali Eghtebas Sweden

"Adam Denby" <adam....@pertemps.co.uk> wrote in message
news:04aa01c31a1c$2efd1410$a001...@phx.gbl...

Adam Denby

unread,
May 21, 2003, 11:51:55 AM5/21/03
to
Thank you very much Ali. Your help solved the problem.
0 new messages