2013-06-24 21 views
0

喜是使用自定義組合框不工作(拋出#1除外)低於

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()

任何想法?

+0

你可能想1)刪除C#標記2)提供'DtsStepsComboBoxItem'類聲明 –

回答

0

問題是我打電話給MyBase.onDrawItem(e)。我猜是遞歸的。這個電話沒有必要。刪除固定我的代碼:)

0

在您調用的方法中添加一個斷點,以瞭解遞歸調用的內容。我敢打賭,你的抽籤方法正在消耗一些事件,而另一方面,這種事件又稱爲方法本身。

你可以嘗試把一個標誌在你的方法

private _drawing=false; 

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

這樣你的方法不會調用自身永恆的開始。

+0

對不起,我把這個在C#中;只有現在我意識到你的代碼是在VB中。無論如何,我打賭你明白它的含義 –