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)
我在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
我添加的組件形成並添加對象,它的代碼
但是當我運行我得到一個堆棧溢出錯誤的e.DrawBackground()
任何想法?
你可能想1)刪除C#標記2)提供'DtsStepsComboBoxItem'類聲明 –