Solved

.Net Windows Forms Button Size question

Posted on 2011-09-09
7
386 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error on Add method 1 38
C# HTTP GET method sample code 3 41
Vb. Net application freezes 9 30
.NET tools for adding thread safety to a web app? 3 14
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’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 …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

14 Experts available now in Live!

Get 1:1 Help Now