简体   繁体   English

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

[英]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项的垂直空间,以便它们适合另一侧的文本框:

CheckedListBox与“TextBox”并排 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 在做了一些研究之后我发现CheckedListBox继承了ListBox ,所以它必须有它的公共属性ItemHeight ,但由于某种原因它不会

I tried this : 我试过这个:

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

but it didn't work 但它不起作用

The default implementation of ItemHeight property of CheckedListBox is, CheckedListBox的ItemHeight属性的默认实现是,

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. 这应该允许您设置自己的ItemHeight。

在此输入图像描述

This works in VS2013 net FrameWork4.5 code is VB 这工作在VS2013网络FrameWork4.5代码是VB

Put declare and constant at top of class 将declare和constant放在类的顶部

Usage put rest of code in Form_Load as in example code. 用法将其余代码放在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM