Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 937
Alaa Jabre Avatar asked Jan 22 '12 05:01

Alaa Jabre


2 Answers

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

like image 121
crypted Avatar answered Nov 16 '22 17:11

crypted


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
like image 45
Doug Peterson Avatar answered Nov 16 '22 18:11

Doug Peterson