Results 1 to 7 of 7

Thread: [RESOLVED] label transparency

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Resolved [RESOLVED] label transparency

    i'm trying to place a label with "backcolor = color.transparent" over a progressbar, but it seems that the progressbar becomes transparent too, how can i avoid this?

  2. #2
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: label transparency

    i replied to the other topic by accident

    progress bar is not a container, therefore this doesn't work. (just tested, apparently it changes the forecolor to blue..)

  4. #4
    New Member
    Join Date
    Oct 2011
    Posts
    2

    Re: label transparency

    Hello, new guy here but I have been coding for a year now on my own time so work with me

    Only way you can do this is too draw it on the progressbar.
    Code:
        Private Sub SetProgressBarText(ByVal Target As System.Windows.Forms.ProgressBar, ByVal Text As String, ByVal Location As ProgressBarTextLocation, ByVal TextColor As System.Drawing.Color, ByVal TextFont As System.Drawing.Font)
    
            If Target Is Nothing Then
                Throw New ArgumentException("Null Target")
            End If
    
            If String.IsNullOrEmpty(Text) Then
    
                Dim percent As Integer = CInt(Math.Truncate((CDbl(Target.Value - Target.Minimum) / CDbl(Target.Maximum - Target.Minimum)) * 100))
                Text = percent.ToString() & "%"
            End If
    
            Using gr As Graphics = Target.CreateGraphics()
                gr.DrawString(Text, TextFont, New SolidBrush(TextColor), New PointF(If(Location = ProgressBarTextLocation.Left, 5, ProgressBar1.Width / 2 - (gr.MeasureString(Text, TextFont).Width / 2.0F)), ProgressBar1.Height / 2 - (gr.MeasureString(Text, TextFont).Height / 2.0F)))
            End Using
        End Sub
    
        Public Enum ProgressBarTextLocation
            Left
            Centered
        End Enum
    Example:
    Code:
    SetProgressBarText(ProgressBar1, "50%", ProgressBarTextLocation.Centered, Color.Black, New Font(Font.Name, 12, FontStyle.Regular))
    'Note it will not work on form load

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: label transparency

    welcome to the forum, willrulz188!

    this is one way of doing it, but if the progress bar updates too quickly, the numbers will get mixed up with previous numbers... is there a way to fix that?

  6. #6
    New Member
    Join Date
    Oct 2011
    Posts
    2

    Smile Re: label transparency

    Well, seeing how your drawing on the progressbar it will always be a little slower but I can cut it down so it is a little faster.

    Code:
        Private Sub SetProgressBarText(ByVal Location As ProgressBarTextLocation)
            Using gr As Graphics = ProgressBar1.CreateGraphics()
                gr.DrawString(ProgressBar1.Value & "%", New Font(Font.Name, 10, FontStyle.Regular), New SolidBrush(Color.Black), New PointF(If(Location = ProgressBarTextLocation.Left, 5, ProgressBar1.Width / 2 - (gr.MeasureString(ProgressBar1.Value, New Font(Font.Name, 10, FontStyle.Regular)).Width / 2.0F)), ProgressBar1.Height / 2 - (gr.MeasureString(ProgressBar1.Value, New Font(Font.Name, 10, FontStyle.Regular)).Height / 2.0F)))
            End Using
        End Sub
        Public Enum ProgressBarTextLocation
            Left
            Centered
        End Enum
    I had too merge most of the code making it just run instead of trying to find the values.
    Code:
    SetProgressBarText(ProgressBarTextLocation.Centered)
    'wouldn't let me cut it down any more
    What I would do is declare setprogressbartext first (before giving progressbar1 its value) to give it time to load.
    It's about 30% faster.
    This is the fastest I can make it sorry.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: label transparency

    its perfect, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width