Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

.Net Windows Forms Button Size question

Posted on 2011-09-09
7
Medium Priority
?
401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
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.

 
LVL 15

Assisted Solution

by:x77
x77 earned 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

636 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