Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CheckedListBox not checking...

Status
Not open for further replies.

Dannybe2

Programmer
Jan 31, 2001
136
GB
Hi,

I have a problem with CheckedListBoxes that is confusing me.

This is the code I have in the constructor, where the designer code just adds the checkedlistbox:

Code:
InitializeComponent();

array2 = (ArrayList) array.Clone();

this.checkedListBox1.DataSource = array2;
this.checkedListBox1.CheckOnClick = true;

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

I have tried refreshing the checkedListBox and hiding and showing between this code, but whenever the form comes up, none of the checked boxes are checked.

By stepping through the for loop all the items are being checked as it goes, so why are they not being show to be checked?

Thanks,
Dan
 
I also cannot get the checkBoxList to respond quickly enough.

I have the SelectedIndexChanged tied to an event handler, so whenever a box is checked or unchecked a method is called adding or removing the item from the listbox.

However, if you click twice in quick succession, despite the check box checking and unchecking, the listbox does not add or remove the item as it should on the second click.

I am using the CheckOnClick option.
 
I don't understand. How is this a feature? Surely it is not supposed to do this, so there must be a way around this...

Dan
 
I have solved the second problem, by changing the eventhandler to link to checkedListBox.ItemCheck as opposed to checkedListBox.SelectedItemCheck, and this now seems to cover every check event.

That side of things is working very well, its just loading in some data when the form first loads that is causing problems.
 
Dear Dan,
As far as i'm concerned in my short experience in Programming, it's not a good practice to set GUI transactions in the constructor. Also I guess that the form is loaded after constructor is finished and so you can set static properties of controls such as 'Enabled', 'Visible'... but to set controls variables such as selected items and checked items it should be after form load. Anyway it worked here in form_load().

I hope i could help.
 
Yes, I managed to get it to work by doing this.Show() before the transactions.

Well, where are the GUI transactions supposed to go if they don't go in the constructor, because you certainly cannot put them in the Windows Designer Code?
 
In a case like the one u posted I usually set in the form_load() event and other transactions are done through other controls event handlers. I can't admit it's the standard way but i sually do so and i never face problems in this phase(setting controls values) as i do it away from contructor and designer code.
 
Ok,

Coolstep - I don't know where you heard about the GUI transactions in the constructor.

The first thing in a form's constructor is InitializeComponents. InitializeComponents goes through, creates the items on a form and then makes modifications to them as needed. To keep your code clean, I would recommend putting a method in the constructor rather than the actual modifications. For example:

public MyClass()
{
InitializeComponents();
InitializeCheckBoxes();
}

private void InitializeCheckBoxes()
{
}


Dannybe2: This next part is for you.

May I recommend a ListView instead of a checkedListBox? They are much much much easier to use and have WAAAAY more flexibility.

You might want to consider using a different practice than setting the DataSource. I always create my own method AddItems(string[] items) which I add to the listview myself. While it doesn't automatically update, it does give you more flexibility.
Code:
public void AddItems(string[] items)
{
    for (int i = 0; i < items.Length; i++)
    {
        ListViewItem tempItem = listView1.Items.Add(items[i]);
        tempItem.Checked = true;
    }
}
It's magical.
 
Fair comments.

I agree it looks a lot nicer to use a method in the constructor than to have all the transactions there.

I didn't actually explain all of what I was doing, just the relevant bits. In fact I am using a checklistbox next to a listbox, the checklist obtains its data from an array I have created which doesn't change. Upon ticking items in the checklistbox, they are added to the listbox and ticked in the checklistbox. On closing the form down, the source of the array is updated from the listbox.

I would have used a listview, but because I only needed the checkboxes, I felt it better to keep it simpler and more intuitive as I did not need the flexibility.

Thanks nonetheless for your comments.

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top