Solved

Resizing a Custom Label control?

Posted on 2014-12-03
8
85 Views
Last Modified: 2014-12-04
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!
0
Comment
Question by:BlakeMcKenna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 40479620
Perhaps you can use Graphics.MeasureString() to help determine an appropriate size for your Label/Form?
0
 

Author Comment

by:BlakeMcKenna
ID: 40479738
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

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 40479750
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.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:BlakeMcKenna
ID: 40479767
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

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 40479787
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

0
 

Author Comment

by:BlakeMcKenna
ID: 40480724
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
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 40480976
Not sure what went wrong there.

Try out this code:
    Private Sub ShowLoadStatusMessage(ByVal strMsg As String, ByVal blnShowStatus As Boolean)
        Try
            EH.ErrorMessage = String.Empty

            Me.AutoSize = True
            Me.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink

            mlLoadStatus.Text = strMsg
            mlLoadStatus.Visible = blnShowStatus
            mlLoadStatus.AutoSize = False
            mlLoadStatus.Location = New Point(0, 0)
            mlLoadStatus.TextAlign = ContentAlignment.MiddleCenter
            mlLoadStatus.BringToFront()

            mlLoadStatus.Font = New Font(mlLoadStatus.Font, Font.Bold)
            Using G As Graphics = mlLoadStatus.CreateGraphics()
                Dim stringSize As SizeF = G.MeasureString(strMsg, mlLoadStatus.Font)
                mlLoadStatus.Width = stringSize.Width + 50
                mlLoadStatus.Height = stringSize.Height + 50
            End Using
        Catch ex As Exception
            EH.ErrorMessage = "frmCalibration_3/ShowLoadStatusMessage() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try
    End Sub

Open in new window

0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40481076
Thanks Mike...that worked!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question