當前位置:編程學習大全網 - 編程語言 - 淺析VB.NET實現下拉列表的折行顯示

淺析VB.NET實現下拉列表的折行顯示

有很多值得學習的地方,這裏我們主要介紹實現下拉列表,包括介紹控件進行改進等方面。

.NET是Microsoft公司提供解決未來計算需要的工具。在.NET Framework中提供了許多控件,可以解決編程中用戶界面的設計和實現,但在實際應用中可能需要對系統提供的控件進行改進,如下拉列表不能折行顯示。本文將介紹用實現下拉列表折行顯示。

設計能自動折行的下拉列表

實現下拉列表,在ComboBox控件中每項占用壹行,如果有選擇項的內容長度超過下拉列表的寬度,則超過部分不顯示,這樣就可能造成用戶所見的內容不完全而無法選擇的情況。我們對該控件進行改進,當壹行顯示不完全某項時進行折行顯示,為了防止用戶將折行的項誤認為是兩個選擇項,我們將不同的選項用相互間隔的顏色區分。類代碼如下:

1.Public Class myComboBox

2.Inherits System.Windows.Forms.ComboBox

3.

4.#Region " Windows 窗體設計器生成的代碼 "

5.

6.#End Region

7.'下面代碼用不同的顏色顯示選項

8.Private Sub myComboBox_DrawItem(ByVal sender As Object,

ByVal e As _ System.Windows.Forms.DrawItemEventArgs) Handles MyBase.DrawItem

9.If e.Index

0 Then Exit Sub

10.Dim txtColor As SolidBrush

11.Dim bgColor As SolidBrush

12.Dim txtfnt As Font

13.txtColor = New SolidBrush(Color.Black)

14.If e.Index / 2 = CInt(e.Index / 2) Then

15.bgColor = New SolidBrush(Color.White)

16.Else

17.bgColor = New SolidBrush(Color.LightYellow)

18.End If

19.If e.State And DrawItemState.Selected Then

20.txtColor = New SolidBrush(Color.Blue)

21.End If

22.e.Graphics.FillRectangle(bgColor, e.Bounds)

23.e.Graphics.DrawRectangle(Pens.Black, e.Bounds)

24.Dim r As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)

25.e.Graphics.DrawString(Items(e.Index).ToString, Me.Font, txtColor, r)

26.End Sub

27.'下面代碼計算每行選項需要的尺寸

28.Private Sub myComboBox_MeasureItem(ByVal sender As Object,

ByVal e As _ System.Windows.Forms.MeasureItemEventArgs) Handles MyBase.MeasureItem

29.Dim lsize As SizeF

30.lsize = e.Graphics.MeasureString(Items(e.Index).ToString, Me.Font, New SizeF(Me.Width, 200))

31.e.ItemHeight = lsize.Height

32.e.ItemWidth = lsize.Width

33.End Sub

34.End Class

以上介紹實現下拉列表折行顯示。

  • 上一篇:黑客編程遊戲
  • 下一篇:請問以WJ18B開頭的車牌是那個部隊的車?
  • copyright 2024編程學習大全網