Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

asked on 

Resizing a Custom Label control?

I have a Custom Label control on a form that is used to display messages. This Label is dynamic in that the user also has the ability to change the color and FontSize. With that in mind, what is the VB.Net code that will automatically calculate the width and height based on the FontSize?

Thanks!
Visual Basic.NET

Avatar of undefined
Last Comment
BlakeMcKenna
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Perhaps you can use Graphics.MeasureString() to help determine an appropriate size for your Label/Form?
Avatar of BlakeMcKenna
BlakeMcKenna
Flag of United States of America image

ASKER

Mike,

Using the example that MSDN provided...if I call this routine, what do I need to pass as an argument into this Sub? Is there a way to avoid using that argument?

Private Sub MeasureStringMin(ByVal e As PaintEventArgs)

    ' Set up string. 
    Dim measureString As String = "Measure String" 
    Dim stringFont As New Font("Arial", 16)

    ' Measure string. 
    Dim stringSize As New SizeF
    stringSize = e.Graphics.MeasureString(measureString, stringFont)

    ' Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
    stringSize.Width, stringSize.Height)

    ' Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
    New PointF(0, 0))
End Sub

Open in new window

For the example, it'd work best to pass "e.Graphics" from the Paint() event of the Form (or something else).

For your actual scenario, it would suffice to use "Me.CreateGraphics" and use that for measuring.  Be sure to Dispose of any Graphics that YOU create.  *Don't try to dispose of the graphics from the Paint() event.
Avatar of BlakeMcKenna
BlakeMcKenna
Flag of United States of America image

ASKER

Ok, I think I'm pretty lost. Here is my code:

    Private Sub ShowLoadStatusMessage(ByVal strMsg As String, ByVal blnShowStatus As Boolean, ByVal e As PaintEventArgs)

        Try
            'mlStatusMessage is the Label Control
            Dim stringFont As New Font(mlStatusMessage.Font, Font.Bold)
            Dim stringSize As New SizeF

            EH.ErrorMessage = String.Empty

            mlLoadStatus.Text = strMsg
            mlLoadStatus.Left = (mlLoadStatus.Width - mlLoadStatus.Width) / 2
            mlLoadStatus.Top = (mlLoadStatus.Height - mlLoadStatus.Height) / 2
            mlLoadStatus.BringToFront()

            If blnShowStatus Then
                mlLoadStatus.Visible = True
            Else
                mlLoadStatus.Visible = False
            End If

            stringSize = e.Graphics.MeasureString(strMsg, stringFont)

            ' Draw rectangle representing size of string.
            e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height)

            ' Draw string to screen.
            'e.Graphics.DrawString(strMsg, stringFont, Brushes.Black, New PointF(0, 0))

        Catch ex As Exception
            EH.ErrorMessage = "frmCalibration_3/ShowLoadStatusMessage() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try
    End Sub

Open in new window

Try something like:
    Private Sub ShowLoadStatusMessage(ByVal strMsg As String, ByVal blnShowStatus As Boolean)

        Try
            'mlStatusMessage is the Label Control
            Dim stringFont As New Font(mlStatusMessage.Font, Font.Bold)
            Dim stringSize As New SizeF

            EH.ErrorMessage = String.Empty

            mlLoadStatus.Text = strMsg
            mlLoadStatus.Left = (mlLoadStatus.Width - mlLoadStatus.Width) / 2
            mlLoadStatus.Top = (mlLoadStatus.Height - mlLoadStatus.Height) / 2
            mlLoadStatus.BringToFront()

            If blnShowStatus Then
                mlLoadStatus.Visible = True
            Else
                mlLoadStatus.Visible = False
            End If

            Dim G As Graphics = Me.CreateGraphics()
            stringSize = G.MeasureString(strMsg, stringFont)

            ' ... now do something in here with "stringSize" and your label, "mlStatusMessage" ...

            G.Dispose()

        Catch ex As Exception
            EH.ErrorMessage = "frmCalibration_3/ShowLoadStatusMessage() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try
    End Sub

Open in new window

Avatar of BlakeMcKenna
BlakeMcKenna
Flag of United States of America image

ASKER

Hi Mike,

That worked great with just one exception. If you look at the attached Screenshot.jpg, you'll notice that the wording is not centered. Also, look at the coding screenshot (Screenshot2.jpg) and you'll see what I did to try and rectify that issue. It's not working so I'm not sure how to handle that. If this were a web form, I could just manipulate the Label using the Padding attributes (I think that's the properties to use?).
Screenshot.jpg
Screenshot2.jpg
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of BlakeMcKenna
BlakeMcKenna
Flag of United States of America image

ASKER

Thanks Mike...that worked!
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo