Solved

.Net Windows Forms Button Size question

Posted on 2011-09-09
7
381 Views
Last Modified: 2012-05-12
How, given input text and font, do I calculate a button's size? For Labels and Textboxes I use the TextRenderer.Measurestring to get the text size, then set the control's ClientSize property. The control's Size property is automatically set a little larger by the system and everything works fine. However, buttons dont do this and the text gets cut off when the button is displayed.

Currently I am fudging, adding an amount to the text height and width that I found by trial and error. It doesnt work perfectly but is OK. Is there a correct way to do this calculation.

I am using framework 4, Windows 64 bit.
0
Comment
Question by:MikeBroderick
  • 3
  • 3
7 Comments
 
LVL 18

Expert Comment

by:lludden
ID: 36511335
You can use the MeasureString method

Here is the code sample from MSDN:
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
 

Author Comment

by:MikeBroderick
ID: 36511398
Tried it, doesnt work. Graphics.MeasureString doesnt calculate the string size correcly for display devices. I use the TextRenderer.MeasureText method instead. it works correctly.

The problem I am having is the fact that when you set a button's client size, the sze is not adjusted for any nonclient stuff the way labels and textboxes do.
0
 
LVL 15

Accepted Solution

by:
x77 earned 500 total points
ID: 36512428
Windows Controls uses GDI, then you need eval using Gdi functions.

.Net uses GDI+ and DrawString has some diferences with Gdi TextOut, also on Font Size

The easy solution is create the button an Layout it:

Private btn As New Button With {.AutoSize = True, .visible = False}

    Public Sub EvalSizeButton(ByVal Font As Font, ByVal Txt As String)
        Button1.SuspendLayout()
        btn.Font = Font
        btn.Text = "Esto es una Prueba"
        btn.Size = New Size(0, 0)
        btn.ResumeLayout()
        Debug.Print(btn.Size.ToString)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       Me.Controls.Add(btn)
       EvalSizeButton(New Font("Arial", 16), "Prueba")
       EvalSizeButton(New Font("Arial", 10), "Prueba 2")
       Me.Controls.Remove(btn)
    End Sub

Open in new window

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 15

Assisted Solution

by:x77
x77 earned 500 total points
ID: 36512440
Sorry, replace Line:
   Button1.SuspendLayout()
with
   btn.SuspendLayout()
0
 

Author Comment

by:MikeBroderick
ID: 36513169
Thank you, but this didnt work either. The AutoSize = True line makes the button display correcly. I do not see the need for any of the suspend, resume, or size lines. Please let me know if I am missing something.

The problem with using AutoSize is I need the button's overall size so I can place other objects on the form near each other and have correct spacing. With AutoSize = True, I now dont know the actual button size.

This seemed to work. It turns AutoSize on and then off, recording then setting the  height/width:

   
Private Shared Sub BldButton(ByRef inButton As Button, ByVal inParent As Object, _
                       ByVal inText As String, ByVal inFont As Font, _
                       ByVal inLoc As Point)
        Dim ww, hh As Int32

        inButton = New Windows.Forms.Button
        inButton.AutoSize = True
        inButton.Parent = inParent

        inButton.Font = inFont
        inButton.Text = inText
        inButton.Location = inLoc

        ww = inButton.Width
        hh = inButton.Height
        inButton.AutoSize = False
        inButton.Height = hh
        inButton.Width = ww

    End Sub

Open in new window

0
 
LVL 15

Expert Comment

by:x77
ID: 36513295
Note I do´nt suggest you to set your button autosize.

In suggest use a invisible button to eval size, then set the size to your control or draw the control with this size.

I test the evalsize method and works fine.
I SuspendLayout / resumeLayout on an auxiliar control used only to eval size.

I think you are adding / resizing controls.
0
 

Author Comment

by:MikeBroderick
ID: 36513320
Sorry I didnt understand. The DebugPrint prints the correct button size, which is what I want. Thank you.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

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

16 Experts available now in Live!

Get 1:1 Help Now