How to change CheckedListBox item vertical space

2019-01-20 12:14发布

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 http://i40.tinypic.com/358vt52.png 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

2条回答
Summer. ? 凉城
2楼-- · 2019-01-20 12:26

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
查看更多
手持菜刀,她持情操
3楼-- · 2019-01-20 12:43

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

查看更多
登录 后发表回答