Solved

Resizing a Custom Label control?

Posted on 2014-12-03
8
79 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now