Question ProgressBar Component, OnPaint?

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
I've basically inherited the ProgressBar component to make one that allows the placement of a Text Percent in the middle of the progressbar, but I seem to be missing something.

it is a very simple component so here is the source for it, and if someone might help explain why the text isn't displaying. Additionally, I've tried debugging it, though the control is contained in a Class Library Assemly, and it doesn't seem to break at the two methods i've overridden. Is there a way to get the debugger to break inside the source for a dll i've written and is a project dependency of my main app's solution?

VB.NET:
Imports System.ComponentModel

Public Class FlufProgBar
   Inherits System.Windows.Forms.ProgressBar

   Private x As Label

   Private _showtext As Boolean = False

   <EditorBrowsable(EditorBrowsableState.Always), Browsable(True)> _
   Public Overrides Property Font() As System.Drawing.Font
      Get
         Return MyBase.Font
      End Get
      Set(ByVal value As System.Drawing.Font)
         MyBase.Font = value
      End Set
   End Property

   Public Property ShowText() As Boolean
      Get
         Return _showtext
      End Get
      Set(ByVal value As Boolean)
         _showtext = value
      End Set
   End Property

   Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
      MyBase.OnPaintBackground(pevent)
   End Sub

   Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
      MyBase.OnPaint(e)
      If _showtext Then
         Dim pct As String = (Me.Value * 100 \ Me.Maximum).ToString & "%"
         Dim sz As System.Drawing.Size = TextRenderer.MeasureText(pct, Me.Font)
         Dim pt As New System.Drawing.Point((Me.Width \ 2) - (sz.Width \ 2), _
                                            (Me.Height \ 2) - (sz.Height \ 2))
         TextRenderer.DrawText(e.Graphics, pct, Me.Font, pt, ColorTranslator.FromWin32(KnownColor.ControlText), ColorTranslator.FromWin32(KnownColor.Transparent))
      End If

   End Sub
End Class

Basically, the OnPaint() method "should" be called when ever the control is to be drawn (ie, the value property is changed), but perhaps i'm overriding the wrong method for this.

Thanks
 
Okay,

well i found that the OnPaint() was not getting called until I added in the constructor for my component
VB.NET:
Me.SetStyle(ControlStyles.UserPaint, True)

however, though now the Text displays, the progress bar graphic does not anymore. Any suggestions that would allow me to do "additional" painting instead of having to re-write already designed GDI paint code for the component?
Thanks
 
Back
Top