How to set the height of a WinForms CheckedListBox to fit to dynamic contents without scrollbars

I recently encountered a problem trying to get the Windows Forms CheckedListBox control to resize it’s height to exactly fit it’s contents (items) without showing scrollbars.

Initially I looked at using the obvious PreferredHeight property, but it quickly became evident that the preferred height didn’t give me a useable value. It was out by a few pixels over ten items and when I got up into hundreds of items it was way out.

I also had to contend with the fact that this code was going to be run across XP, Vista, Window 7/8 and potentially across multiple DPI settings (100%, 125%, 150%).

In the end the solution wasn’t too bad.

// Explicitly set height to fit options
checkBoxCtrl.ClientSize = new Size(checkBoxCtrl.ClientSize.Width, checkBoxCtrl.GetItemRectangle(0).Height * checkBoxCtrl.Items.Count);

 

The key concepts here as:

  • Set the ClientSize property rather than the Size property. ClientSize is just the internal area the items populate so you don’t have to worry about padding and border widths applied via VisualStyles
  • All items in my list are the same height so I can just measure the height of the first item and multiply by the number of items in the list
  • The GetItemRectangle property returns the height of an item in the list taking into account different DPI settings and the padding/margin between items in the list. Note: this is much simpler than trying the measure the graphic (checkbox glyph) or Text of an item and the padding/margin between items.
Advertisement

2 thoughts on “How to set the height of a WinForms CheckedListBox to fit to dynamic contents without scrollbars

Add yours

  1. Just like you I was misguided by the MSDN documentation: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.preferredheight(v=vs.110).aspx
    I didn’t like computing the height like you did, so my solution is to simply set the ClientSize(Height) to the PreferredHeight and it works.
    I had another issue which was that my CheckedListForm Visible state was false when loading the form, so the PreferredHeight was miscalculated. My workaround for this was simply to set the ClientSize right after showing the CheckedListBox.

    Like

    1. Hi David, thanks for sharing your solution. I ran up a quick WinForms app and found your solution to work nicely. When I originally had to solve this problem the preferred height was giving me the wrong value for setting the ClientSize so I had to calculate the height myself. This incorrect Preferred Height may have been coming from the fact I had my CheckBoxList control inside nested Telerik container controls. The Telerik themes change/effect a lot of the visual elements and I remember seeing some unusual padding between items in the checkbox list and between the checkbox border and the actual items themselves. Thanks again.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: