Hi guys how do I limit the units in my textbox to 15?

when the total units is already in 15, I must not be able to select an item from my checkbox anymore. I have an attachment for you to use as your guide.

Attachments c#_example.jpg 30.97 KB

You want to limit the number of checked items in a CheckedListBox. What property of CheckedListBox tells you what is checked? Once you have that, is there some way to Count them?

You want to limit the number of checked items in a CheckedListBox. What property of CheckedListBox tells you what is checked? Once you have that, is there some way to Count them?

What I want to do is to prevent the selection of another item in the checkbox once the total units textbox has already reached 15. those items with from the checkbox with "-lab" are equivalent to 1 unit and those without "-lab" are equivalent to 2 units.

I made use of this code:

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int total = 0;

            string s = "";
            for (int x = 0; x <= checkedListBox1.CheckedItems.Count - 1; x++)
            {
                s = checkedListBox1.CheckedItems[x].ToString();
                if (s.IndexOf("-Lab") != -1)
                {
                    total += 1;
                }
                else
                {
                    total += 2;
                }


            }
            textBox3.Text = total.ToString();

textbox3 is the total units textbox.

Edited 5 Years Ago by king03: n/a

Test the Count Momerath mentions in the ItemCheck event.
There you can block the check by setting the e.NewValue to Unchecked.

Test the Count Momerath mentions in the ItemCheck event.
There you can block the check by setting the e.NewValue to Unchecked.

can you show me how to do it>?

This is rather embarrassing and disappointing. You have already asked two questions on one project. most of the times it's better to learn from your own mistakes, besides from what I can tell I assume you're a beginner program and probably using VS,SharpDev or some IDE which most probably has support for Intellisense and have access to a book / msdn documentation.

Mate, most collection classes in .NET have one or more properties of counting or enumerating over their collection. given that or your knowledge with loops you can archive whatever is that you're looking for. I won't give you a straight answer but as a hint that control you're using probably has a SelectedIndexes / SelectedItems proteries which in most cases will return an integer which you can using in comparing if you have reached your maximum or not. if you're good you can keep count using event handlers which will notify you each time an item is check or unchecked and then you can disable or do some house keeping you need to do.

http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.aspx

Comments
Well said.
I agree!
This article has been dead for over six months. Start a new discussion instead.