如何更改CheckedListBox项目的垂直空间

时间:2012-01-22 05:15:20

标签: c# winforms checkedlistbox

我需要更改CheckedListBox项的垂直空间,以便它们与另一侧的文本框相符:

CheckedListBox and "TextBox"s side to side http://i40.tinypic.com/358vt52.png 怎么做?

在做了一些研究后,我发现CheckedListBox继承了ListBox,所以它必须有公共属性ItemHeight,但由于某种原因它不会

我试过了:

ListBox l = CheckedList as ListBox;
        l.ItemHeight = 30;

但它无效

2 个答案:

答案 0 :(得分:16)

CheckedListBox的ItemHeight属性的默认实现是

public override int ItemHeight { 
        get {
            // this should take FontHeight + buffer into Consideration.
            return Font.Height + 2;
        } 
        set {
        } 
    } 

您可以在新类中干净地覆盖此属性。

public sealed class  MyListBox:CheckedListBox
    {
        public MyListBox()
        {
            ItemHeight = 30;
        }
        public override int ItemHeight { get; set; }
    }

这应该允许您设置自己的ItemHeight。

enter image description here

答案 1 :(得分:1)

这适用于VS2013网络FrameWork4.5代码是VB

将声明和常量放在类

的顶部

用法将其余代码放在Form_Load中,如示例代码中所示。

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