[英]VB.Net CheckedListBox automatically wrap text
这更多的是设计而不是编程问题(嗯,也许不是)。 我有4个CheckedListBoxes,其中填充了来自SQLite数据库(Visual Studio 2010)的数据,并且某些条目超出了Box的宽度。 我知道我可以在CheckedListBox中包含一个水平滚动条,但是每个人都讨厌水平滚动条(非常丑陋),因此我试图找到一个选项来自动包装不适合的文本。 因此,如果有任何解决方案可以在框的宽度太小时将文字换行,那就太好了。
我可以扩展窗口的大小,但是它的宽度已经超过1000像素,并且某些用户使用的是具有1024x768解决方案的木质计算机,因此这不是真正的选择。
Datagrid将是另一个选择,但我认为必须有一个更简单的解决方案。 有什么提示吗?
编辑:对不起,它是Windows窗体。
您可以使用上面带有一些实际CheckBox的面板轻松地编写自己的CheckedListBox
,这样您就可以完成您期望的其他事情,例如禁用某些复选框,修复其截断拖放字符的方式,对其进行迭代等等。
换行的问题是:a)确定长文本的文本范围,以便知道每个复选框的高度,以及b)必须保持累积的项目高度,以便知道在哪里添加下一个复选框。 当然,一旦您支持自动换行,就必须能够调整它们的全部内容,其中包括当文本更改导致中间的一个文本增加/缩小时移动它们。
面板的自动滚动为您处理所有滚动,包括根据需要添加HSCroll,这并不总是令人满意的。 解决此问题的一种方法(可能适用于您正在使用的实际CheckedListBox)是吃掉HScroll。
<DllImport("user32.dll")> _
Private Shared Function ShowScrollBar(hWnd As IntPtr,
wBar As Integer,
bShow As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
然后在表单加载中或在填充后:
ShowScrollBar(myControl.Handle, ScrollBarDirection.SB_HORZ, False)
您也可以仅继承现有CheckedListBox的子类,以使用OnClientSizeChanged
的滚动条
Public Class CheckedListBox2
Inherits CheckedListBox
' optionally remove the scroll bar
Public Property VerticalScrollOnly As Boolean
' PInvokes
<DllImport("user32.dll", SetLastError:=True)>
Public Shared Function GetWindowLong(ByVal hWnd As IntPtr,
ByVal nIndex As Integer) As Integer
End Function
<DllImport("user32.dll")>
Private Shared Function ShowScrollBar(hWnd As IntPtr,
wBar As Integer,
bShow As Boolean) _
As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
'// window style constants for scrollbars
Private Const WS_VSCROLL As Integer = &H200000
Private Const WS_HSCROLL As Integer = &H100000
Private Const GWL_STYLE As Integer = -16
Private Enum ScrollBarDirection
SB_HORZ = 0
SB_VERT = 1
SB_CTL = 2
SB_BOTH = 3
End Enum
' eat the HScroll when it shows up
Protected Overrides Sub OnClientSizeChanged(e As EventArgs)
Dim HScrollVis As Boolean
HScrollVis = IsHScrollVisible(Me)
If VerticalScrollOnly AndAlso HScrollVis Then
ShowScrollBar(MyBase.Handle, ScrollBarDirection.SB_HORZ, False)
End If
MyBase.OnClientSizeChanged(e)
End Sub
Friend Shared Function IsHScrollVisible(ByVal ctl As Control) As Boolean
Dim wndStyle As Integer = GetWindowLong(ctl.Handle, GWL_STYLE)
Return ((wndStyle And WS_HSCROLL) <> 0)
End Function
End Class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.