自定义组合框无法正常工作(抛出Stackoverflow异常)

时间:2013-06-24 10:25:04

标签: vb.net visual-studio-2010 visual-studio

下面是我在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()

上遇到堆栈溢出错误

任何想法?

2 个答案:

答案 0 :(得分:0)

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

您可能会尝试在方法的开头添加标记

private _drawing=false;

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

这样你的方法就不会自称永远。

答案 1 :(得分:0)

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