How to change CheckedListBox item vertical space

久未见 提交于 2019-11-27 07:48:54

问题


I need to change the vertical space for CheckedListBox items so they fit with the text boxes on the other 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


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/8959146/how-to-change-checkedlistbox-item-vertical-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!