VB.NET中怎么实现下拉列表折行显示,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
设计能自动折行的下拉列表
VB.NET实现下拉列表,在ComboBox控件中每项占用一行,如果有选择项的内容长度超过下拉列表的宽度,则超过部分不显示,这样就可能造成用户所见的内容不完全而无法选择的情况。我们对该控件进行改进,当一行显示不完全某项时进行折行显示,为了防止用户将折行的项误认为是两个选择项,我们将不同的选项用相互间隔的颜色区分。类代码如下:
Public Class myComboBox Inherits System.Windows.Forms.ComboBox #Region " Windows 窗体设计器生成的代码 " … #End Region '下面代码用不同的颜色显示选项 Private Sub myComboBox_DrawItem(ByVal sender As Object, ByVal e As _ System.Windows.Forms.DrawItemEventArgs) Handles MyBase.DrawItem If e.Index < 0 Then Exit Sub Dim txtColor As SolidBrush Dim bgColor As SolidBrush Dim txtfnt As Font txtColor = New SolidBrush(Color.Black) If e.Index / 2 = CInt(e.Index / 2) Then bgColor = New SolidBrush(Color.White) Else bgColor = New SolidBrush(Color.LightYellow) End If If e.State And DrawItemState.Selected Then txtColor = New SolidBrush(Color.Blue) End If e.Graphics.FillRectangle(bgColor, e.Bounds) e.Graphics.DrawRectangle(Pens.Black, e.Bounds) Dim r As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) e.Graphics.DrawString(Items(e.Index).ToString, Me.Font, txtColor, r) End Sub '下面代码计算每行选项需要的尺寸 Private Sub myComboBox_MeasureItem(ByVal sender As Object, ByVal e As _ System.Windows.Forms.MeasureItemEventArgs) Handles MyBase.MeasureItem Dim lsize As SizeF lsize = e.Graphics.MeasureString(Items(e.Index).ToString, Me.Font, New SizeF(Me.Width, 200)) e.ItemHeight = lsize.Height e.ItemWidth = lsize.Width End Sub End Class
关于VB.NET中怎么实现下拉列表折行显示问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。