0

嗨,下面是我在 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() 上收到堆栈溢出错误

有任何想法吗?

4

2 回答 2

0

在您调用的方法中放置一个断点,以了解是什么使它递归调用。我敢打赌,你的 draw 方法正在处理一些事件,另一方面,它调用了方法本身。

您可能会尝试在方法的开头放置一个标志

private _drawing=false;

private method()
{
  if (_drawing) return; // do nothing
  try
  {
    _drawing=true;
    //call or paste your logic here
  }
  finally
  {
    _drawing=false
  }
}

这样,您的方法就不会自称是永恒的。

于 2013-06-24T10:44:37.840 回答
0

问题是我正在调用 MyBase.onDrawItem(e)。我猜这是递归的。没有必要打电话。删除修复了我的代码:)

于 2013-06-29T04:33:57.193 回答