Search code examples
c#winformscheckedlistbox

How to change CheckedListBox item vertical space


I need to change the vertical space for CheckedListBox items so they fit with the text boxes on the other side:

CheckedListBox and "TextBox"s side to side How to do this ?

After doing some research I found out that CheckedListBox inherits ListBox, so it must have its public property ItemHeight, but for some reason it doesn't

I tried this :

ListBox l = CheckedList as ListBox;
        l.ItemHeight = 30;

but it didn't work


Solution

  • The default implementation of ItemHeight property of CheckedListBox is,

    public override int ItemHeight { 
            get {
                // this should take FontHeight + buffer into Consideration.
                return Font.Height + 2;
            } 
            set {
            } 
        } 
    

    you can cleanly override this property in a new class.

    public sealed class  MyListBox:CheckedListBox
        {
            public MyListBox()
            {
                ItemHeight = 30;
            }
            public override int ItemHeight { get; set; }
        }
    

    this should allow you to set your own ItemHeight.

    enter image description here