Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing text to progress bar OnPaint not working.

Status
Not open for further replies.

Sorwen

Technical User
Joined
Nov 30, 2002
Messages
1,641
Location
US
I've used the OnPaint even to write graphics to the screen before and it has always worked. This is the only the second time I've done text however and it doesn't want to work this time. In the past I was only drawing to the form though and not a control. I've tried a few different ways, but here is currently how the code looks.

Code:
Public Class ProgressBarWithText
    Inherits System.Windows.Forms.ProgressBar

    Private MyBrush As New System.Drawing.SolidBrush(Drawing.Color.Black)

#Region "Properties"
    Public Shadows Property Value() As Integer
        Get
            Return MyBase.Value
        End Get
        Set(ByVal value As Integer)
            MyBase.Value = value

            RaiseEvent ValueChanged(Me)
        End Set
    End Property
#End Region

#Region "Events"
    Public Event ValueChanged(ByVal sender As Object)
#End Region

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        If Me.Visible = True Then
            Dim x As Single
            Dim y As Single
            Dim text As String = Me.Value & "/" & Me.Maximum
            Dim sizeF As System.Drawing.SizeF = Me.CreateGraphics.MeasureString(text, Me.Font, Me.Width)

            x = (Me.Width / 2) - (sizeF.Width / 2)
            y = (Me.Height / 2) - (sizeF.Height / 2)
            e.Graphics.DrawString(text, Me.Font, Me.MyBrush, x, y)
        End If

        MyBase.OnPaint(e)
    End Sub

End Class
Is it something about the OnPaint event itself that is causing it? I have found another way to do this, but I would prefer the control to write this to itself rather than always needing to remember to write it else where. Also, I wanted the OnPaint event so if something else caused it to repaint before the value updated it would still show the progress values and not wait until the value was updated.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Cool. I'll have to take a good look at it later. The reason MyBase.OnPaint(e) does nothing (and you don't need it) is because you don't inherit from another control. Took another quick look and I assume you are using 2003 or you have some options off because you don't need the all of the Overrides either. They normally should error since it doesn't inherit from anything there is nothing to Override. Still that could just be a difference if you are using 2003.

Still it is nice and I'll look more later. Thanks for posting it.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorry there is another way. If you make a user control it does not mention that you inherit from controls, but that is what is happening when you do. So then mybase and overrides could/would be needed. My bad if that is the case.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
The code I posted is 2008 and inherits from Control.

My library contains three different types of progress control, which inherit from a base progress control which contains much of the common functionality. Additionally the library contains various other controls many of which share several pieces of code. As such I don't have a "stand-alone" progress bar control, and have tried to pick out the "bits" that would make it stand-alone. For example the TextParts handler and the Brush handler are separate classes and are simply provided as single properties in the controls that use them. The full Enum from which I extracted ProgressBarTextParts is almost a full sheet of A4 (slightly longer than Letter if you are from the US). Additionally, TypeConverters are extensively used to handle the PropertyGrid and to remove anomolies such as hiding the properties for HatchStyle and GradientStyle when Solid is chosen etc.
 
By the way, I only ever work with Option Strict On and Option Explicit On except when working with LateBinding (when I set Option Strict Off for that particular class only).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top