Solved

Resizing a Custom Label control?

Posted on 2014-12-03
8
82 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
  • 4
  • 4
8 Comments
 
LVL 85

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 85

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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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 85

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 85

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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