Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim lst As New MyListBox lst.Separator = "-" lst.Items.Add("This is the first TestTestTestItem") For i As Integer = 0 To 20 lst.Items.Add(String.Format("Item{0}-TestItem{1}", i.ToString, i.ToString)) Next lst.Dock = DockStyle.Fill Me.Controls.Add(lst) End SubEnd Class Public Class MyListBox Inherits System.Windows.Forms.ListBox Public Sub New() Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable End Sub Private _Separator As String = "-" Public Property Separator() As String Get Return _Separator End Get Set(ByVal value As String) _Separator = value End Set End Property Private Sub MyListBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem Dim txtBrush As SolidBrush = New SolidBrush(Color.Blue), bgBrush As SolidBrush = New SolidBrush(Color.LightCyan), txtFnt As Font = New Font(Me.Font.Name, Me.Font.Size) Try If e.Index = -1 Then Exit Sub If e.Index Mod 2 = 0 Then txtBrush = New SolidBrush(Color.Red) If e.State And System.Windows.Forms.DrawItemState.Selected Then txtFnt = New Font(Me.Font.Name, Me.Font.Size, FontStyle.Italic Or FontStyle.Bold) bgBrush = New SolidBrush(Color.LightSteelBlue) End If e.Graphics.FillRectangle(bgBrush, e.Bounds) Dim SF As New StringFormat : SF.LineAlignment = StringAlignment.Center Dim r1 As New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height / 2) Dim r2 As New Rectangle(e.Bounds.X, e.Bounds.Y + e.Bounds.Height / 2, e.Bounds.Width, e.Bounds.Height / 2) Dim item() As String = Items(e.Index).ToString.Split(_Separator) If item.Length = 2 Then e.Graphics.DrawString(item(0), txtFnt, txtBrush, r1, SF) e.Graphics.DrawString(item(1), txtFnt, txtBrush, r2, SF) Else e.Graphics.DrawString(Items(e.Index).ToString, txtFnt, txtBrush, e.Bounds, SF) End If e.DrawFocusRectangle() Catch ex As Exception End Try txtBrush.Dispose() : bgBrush.Dispose() : txtFnt.Dispose() '释放资源 End Sub Private Sub MyListBox_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Me.MeasureItem Try Dim S As New Size(Me.Width, 400) Dim itmSize As SizeF = e.Graphics.MeasureString(Items(e.Index).ToString, Me.Font, S) e.ItemHeight = itmSize.Height * 2 : e.ItemWidth = itmSize.Width Catch ex As Exception End Try End SubEnd Class c#代码不会写,仅供lz可参考一下。
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lst As New MyListBox
lst.Separator = "-"
lst.Items.Add("This is the first TestTestTestItem")
For i As Integer = 0 To 20
lst.Items.Add(String.Format("Item{0}-TestItem{1}", i.ToString, i.ToString))
Next
lst.Dock = DockStyle.Fill
Me.Controls.Add(lst)
End SubEnd Class
Public Class MyListBox
Inherits System.Windows.Forms.ListBox Public Sub New()
Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
End Sub Private _Separator As String = "-"
Public Property Separator() As String
Get
Return _Separator
End Get
Set(ByVal value As String)
_Separator = value
End Set
End Property Private Sub MyListBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
Dim txtBrush As SolidBrush = New SolidBrush(Color.Blue), bgBrush As SolidBrush = New SolidBrush(Color.LightCyan), txtFnt As Font = New Font(Me.Font.Name, Me.Font.Size) Try
If e.Index = -1 Then Exit Sub If e.Index Mod 2 = 0 Then txtBrush = New SolidBrush(Color.Red)
If e.State And System.Windows.Forms.DrawItemState.Selected Then
txtFnt = New Font(Me.Font.Name, Me.Font.Size, FontStyle.Italic Or FontStyle.Bold)
bgBrush = New SolidBrush(Color.LightSteelBlue)
End If e.Graphics.FillRectangle(bgBrush, e.Bounds) Dim SF As New StringFormat : SF.LineAlignment = StringAlignment.Center Dim r1 As New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height / 2)
Dim r2 As New Rectangle(e.Bounds.X, e.Bounds.Y + e.Bounds.Height / 2, e.Bounds.Width, e.Bounds.Height / 2)
Dim item() As String = Items(e.Index).ToString.Split(_Separator)
If item.Length = 2 Then
e.Graphics.DrawString(item(0), txtFnt, txtBrush, r1, SF)
e.Graphics.DrawString(item(1), txtFnt, txtBrush, r2, SF)
Else
e.Graphics.DrawString(Items(e.Index).ToString, txtFnt, txtBrush, e.Bounds, SF)
End If e.DrawFocusRectangle()
Catch ex As Exception
End Try txtBrush.Dispose() : bgBrush.Dispose() : txtFnt.Dispose() '释放资源
End Sub Private Sub MyListBox_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Me.MeasureItem
Try
Dim S As New Size(Me.Width, 400)
Dim itmSize As SizeF = e.Graphics.MeasureString(Items(e.Index).ToString, Me.Font, S) e.ItemHeight = itmSize.Height * 2 : e.ItemWidth = itmSize.Width
Catch ex As Exception
End Try
End SubEnd Class
c#代码不会写,仅供lz可参考一下。
新建一个vb windows应用程序项目,把上面所有代码拷贝粘贴,运行后即可看到效果。
呵呵
还写了一个