嗨,下面是我在 vb 中创建的自定义组合框的代码
Public Class DtsStepsComboBox
Inherits Windows.Forms.ComboBox
Private status As String
Public Sub New()
Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
End Sub
Private Sub DtsStepsComboBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
Try
e.DrawBackground()
e.DrawFocusRectangle()
Dim item As DtsStepsComboBoxItem
Dim rectPoint As New System.Drawing.Point
Dim textPoint As New System.Drawing.Point
Dim stikedFont As Drawing.Font
'stikedFont = CreateFont(e.Font.OriginalFontName, e.Font.Size, False, False, True)
'Dim imageSize As New Size
'imageSize = ListaImg1.ImageSize
rectPoint.X = 1
rectPoint.Y = 1
Dim bounds As New System.Drawing.Rectangle
bounds = e.Bounds
Dim rectSize As New System.Drawing.Size
rectSize.Height = bounds.Height - 2
rectSize.Width = bounds.Width - 2
textPoint.X = rectPoint.X + rectSize.Width + 2
textPoint.Y = rectPoint.Y
item = CType(Me.Items.Item(e.Index), DtsStepsComboBoxItem)
If (item.status.Equals("0")) Then
e.Graphics.FillRectangle(Drawing.Brushes.White, New System.Drawing.Rectangle(rectPoint, rectSize))
e.Graphics.DrawString(item.text, e.Font, Drawing.Brushes.Black, textPoint)
ElseIf (item.status.Equals("1")) Then
e.Graphics.FillRectangle(Drawing.Brushes.Green, New System.Drawing.Rectangle(rectPoint, rectSize))
e.Graphics.DrawString(item.text, e.Font, Drawing.Brushes.Black, textPoint)
ElseIf (item.status.Equals("2")) Then
e.Graphics.FillRectangle(Drawing.Brushes.Red, New System.Drawing.Rectangle(rectPoint, rectSize))
e.Graphics.DrawString(item.text, e.Font, Drawing.Brushes.Black, textPoint)
ElseIf (item.status.Equals("3")) Then
e.Graphics.FillRectangle(Drawing.Brushes.White, New System.Drawing.Rectangle(rectPoint, rectSize))
e.Graphics.DrawString(item.text, e.Font, Drawing.Brushes.Black, textPoint)
ElseIf (item.status.Equals("4")) Then
e.Graphics.FillRectangle(Drawing.Brushes.White, New System.Drawing.Rectangle(rectPoint, rectSize))
e.Graphics.DrawString(item.text, e.Font, Drawing.Brushes.Black, textPoint)
End If
Catch ex As Exception
End Try
MyBase.OnDrawItem(e)
End Sub
End Class
我已将组件添加到表单并使用向其中添加对象
Dim tempItem As DtsStepsComboBoxItem
tempItem = New DtsStepsComboBoxItem("This is of type 0", "0")
sampleCombo.Items.Add(tempItem)
tempItem = New DtsStepsComboBoxItem("This is of type 1", "1")
sampleCombo.Items.Add(tempItem)
tempItem = New DtsStepsComboBoxItem("This is of type 2", "2")
sampleCombo.Items.Add(tempItem)
tempItem = New DtsStepsComboBoxItem("This is of type 3", "3")
sampleCombo.Items.Add(tempItem)
tempItem = New DtsStepsComboBoxItem("This is of type 4", "4")
sampleCombo.Items.Add(tempItem)
但是当我运行时,我在 e.DrawBackground() 上收到堆栈溢出错误
有任何想法吗?