programmatically setting checks in CheckedListBox

D

David C

The goal here is given an instance of CheckedListBox, I want to have a
button, when pressed will check all the checkboxes inside the
CheckedListBox. Simple enough?

As a side note, I am massively confused with the general programming model
of list controls. Let me try to list them out.

1. Items - The "Items" property is documented as "ObjectCollection" which
makes it very vague as to what exactly is stored. I did some research and
found out that incidents of DataRowView are stored.

2. So what about DataRowView. How is information stored? I looped
through to see what is there. Looked at .Row[0], ..Row[1], .Row[2],
..Row[3], .Row[4], .Row[5], without running into out of bounds error.

It appears that .Row[0] stores the Value (such as the ID)
, have no idea what .Row[1] is. It has some number which I don't
recognize, or sometimes its blank
, .Row[2] has the displayed value
, .Row[3] and others seem to have other columns from the table that I used
to populate.

Why, then are other columns stored there? The way I bounded the control
was.

string sql = "SELECT * FROM Organizations WHERE treeLevel = " + treeLevel;

DataTable organizations = db.QueryIntoDataTable(sql);
lctr.DisplayMember = "name";
lctr.ValueMember = "orgnizationID";
lctr.DataSource = organizations;
db.Close();

I am only intersted in the two columns (name for display and OrganizationID
for Value).

But still, I cannot programmatically get to the check boxes inside
CheckedListBox.

Sorry about rambling on. If anyone can help me understand how List Controls
in Windows works (I've completely mastered their web counterparts), I'd
appreciate it. I just don't understand how to work with them. I just don't
get it. It seems very complicated, but does very little. Access 95
dropdowns are better!!!! For example, the dropdown control. I can't get it
to whittle down the list as the user types in into the box. Thanks in
advance.
 
M

Morten Wennevik

Hi David,

In a CheckedListBox you use SetItemChecked to check the items.

for(int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}

Alternately with a checkbox you can do

checkedListBox1.SetItemChecked(i, checkBox1.Checked);

and have it check or uncheck all items (btw, you can make it look like a
button).


The goal here is given an instance of CheckedListBox, I want to have a
button, when pressed will check all the checkboxes inside the
CheckedListBox. Simple enough?

As a side note, I am massively confused with the general programming
model
of list controls. Let me try to list them out.

1. Items - The "Items" property is documented as "ObjectCollection"
which
makes it very vague as to what exactly is stored. I did some research
and
found out that incidents of DataRowView are stored.

An ObjectCollection is just that, a collection of objects. It can contain
anything and in your case DataRowViews.
2. So what about DataRowView. How is information stored? I looped
through to see what is there. Looked at .Row[0], ..Row[1], .Row[2],
.Row[3], .Row[4], .Row[5], without running into out of bounds error.

It appears that .Row[0] stores the Value (such as the ID)
, have no idea what .Row[1] is. It has some number which I don't
recognize, or sometimes its blank
, .Row[2] has the displayed value
, .Row[3] and others seem to have other columns from the table that I
used
to populate.

Why, then are other columns stored there? The way I bounded the control
was.

string sql = "SELECT * FROM Organizations WHERE treeLevel = "
+ treeLevel;

DataTable organizations = db.QueryIntoDataTable(sql);
lctr.DisplayMember = "name";
lctr.ValueMember = "orgnizationID";
lctr.DataSource = organizations;
db.Close();

I am only intersted in the two columns (name for display and
OrganizationID
for Value).

When binding a DataTable to the listcontrol you will fill it with rows
from that table (DataRowView rows). With DisplayMember, you tell the
listcontrol to display the "name" column for each row. If no
DisplayMember was specified it would just call Object.ToString() which for
DataRowView is the name of the class ("DataRowView").
But still, I cannot programmatically get to the check boxes inside
CheckedListBox.

As I said earlier, use SetItemChecked()
Sorry about rambling on. If anyone can help me understand how List
Controls
in Windows works (I've completely mastered their web counterparts), I'd
appreciate it. I just don't understand how to work with them. I just
don't
get it. It seems very complicated, but does very little. Access 95
dropdowns are better!!!! For example, the dropdown control. I can't
get it
to whittle down the list as the user types in into the box. Thanks in
advance.

Sorry, can't help you there.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi David,

I sow that you have already the answer of the qestion "How to check items in
the checked list box". So I'll try to give you an explanation about the list
box items

1. Items - The "Items" property is documented as "ObjectCollection" which
makes it very vague as to what exactly is stored.

All list controls (listnbox, checkedlistbox, combobox) in .NET keep objects
instead of strings. What you see on the control is the string returned from
the object's ToString method. This could be really convinient because you
don't have to have some "Tags" associated with the list items, you can store
anything.

For the sake of data binding there are two more properties in the list
controls. ValueMember and DisplayMember
In those properties you can set names (string) of the properties. When
displaing the item text in the list the list controls uses reflection and
reads the value of the property which name is set in DisplayMember property.
When selecting an item there is one property called SelectedValue. When read
this property the list control uses reflection to get the value for the
selected object's property, which name is set in ValueMember.
 

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