How to change CheckedListBox item vertical space
Asked Answered
G

2

6

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

Gouveia answered 22/1, 2012 at 5:15 Comment(0)
T
18

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

Tactician answered 22/1, 2012 at 6:0 Comment(0)
N
1

This works in VS2013 net FrameWork4.5 code is VB

Put declare and constant at top of class

Usage put rest of code in Form_Load as in example code.

Private Declare Function SendMessageByNum Lib "user32" Alias "SendMessageA" _
  (ByVal hwnd As IntPtr, ByVal wMsg As UInt32, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

Private Const lB_SETITEMHEIGHT As Integer = &H1A0

Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim ItemHeight As Integer = Me.Font.Height + 4
    SendMessageByNum(CheckedListBoxControl.Handle, lB_SETITEMHEIGHT, CType(0, IntPtr), CType(ItemHeight, IntPtr))

End Sub
Nord answered 27/2, 2015 at 7:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.