Search code examples
c#winformslistboxsplitcontainer

How to make ListBoxes in SplitContainers resize properly?


To reproduce this problem,

  • Create a new WinForms project
  • Using the WinForms designer, add a SplitContainer
    • Set its orientation to horizontal
    • Set its dock to fill
  • In each of the containers of the SplitContainer, add a ListBox
    • Set its dock to fill
    • Increase its font size so that the behaviour in question can be observed more clearly
    • Add a few items to the list boxes

After those steps, the designer should look like this:

enter image description here

  • Now run the program
  • Resize the containers in the SplitContainer

You should see that with some sizes of the upper container, there will be a space between the two list boxes (sorry for my bad mouse-writing).

enter image description here

For other sizes of the upper container, there is little to no space.

I hypothesized that this is because list boxes can't show "half an item" so it reduces its size to not show that half of an item. Is this true?

I have thought of the following solution:

Restricting the resizability of the split container so the user can only resize in "steps" where each step is equal to the height of one item in the list box.

However, I don't quite like this because when the item height is large, the user experience feels unnatural (at least to me).

How can I make it so that the list boxes resize properly (i.e. leaving no gaps)?


Solution

  • You can set the IntegralHeight property to false to achieve the desired effect.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.integralheight(v=vs.110).aspx

    Gets or sets a value indicating whether the control should resize to avoid showing partial items.