如何更改CheckedListBox项目的垂直空间
我需要更改CheckedListBox
项的垂直空间,以便它们适合另一侧的文本框:
CheckedListBox和“TextBox”并排http://sofzh.miximages.com/c%23/358vt52.png如何做到这一点?
经过一些研究后我发现CheckedListBox
inheritance了ListBox
,所以它必须有它的公共属性ItemHeight
,但由于某种原因它不会
我试过这个:
ListBox l = CheckedList as ListBox; l.ItemHeight = 30;
但它不起作用
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。
这工作在VS2013网络FrameWork4.5代码是VB
将declare和constant放在类的顶部
用法将其余代码放在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