Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 87
  • Last Modified:

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!
0
BlakeMcKenna
Asked:
BlakeMcKenna
  • 4
  • 4
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Perhaps you can use Graphics.MeasureString() to help determine an appropriate size for your Label/Form?
0
 
BlakeMcKennaAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
BlakeMcKennaAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
BlakeMcKennaAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
BlakeMcKennaAuthor Commented:
Thanks Mike...that worked!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now