简体   繁体   中英

dynamic checklistbox in windowsform on c#

i want to have a group of check boxes like CheckListBox with different back colores . is there any way for me to have this group and edit at run time?

i use the code bellow but it is cant be in different back colors:

            foreach (var color in _colors)
            {
                var tmpCheckBox = new CheckBox
                                    {
                                       // Location = objLocation,
                                        BackColor = color,
                                        Text = color.Name
                                    };
                objLocation.X = objOffset;
                objLocation.Y += tmpCheckBox.Height + objOffset;
                clbColorAnalyzeResult.Items.Add(tmpCheckBox);
            }

just to mention this: clbColorAnalyzeResult.Controls.Add(tmpCheckBox); will not help because there is no scroll bar and i cant use the selected index!

thanx in advance.

You have to add the checkbox to the Controls property of your form.

Maybe you will have to create you own custom list box and override the OnDrawItem method. Something like this:

class MyCheckedListBox : CheckedListBox
    {
        protected override void OnDrawItem(DrawItemEventArgs e)
        {

        }
    }

Do this!

You should add the checkboxes to a Panel , and set AutoScroll to true, to get the scrollbars. You can subscribe to the GotFocus and LostFocus event to determine which checkbox is selected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM