We help IT Professionals succeed at work.

System.Windows.Forms.Button Question

LazyStudent
LazyStudent asked
on
323 Views
Last Modified: 2006-11-17
I would like to know - how can I resize programaticaly width of the button accordingly to text it contains?
I tried to do it by this way:
----------------------- CODE -----------------------
Graphics grfx = btn.CreateGraphics();
int iWidth = grfx.MeasureString(btn.Text, btn.Font).ToSize().Width;
btn.Size = new Size(iWidth+20, 32);
-------------------- END OF CODE -----------------

but seems like this width didn't fit at all... I assume that I have count border width of the button ( +20 - is trying to do so ) but I don't know how to get border width precisely also.

If somebody know the robust way ( dependent on Font size, type, bold/normal etc. ) to resize button width - please let me know
Comment
Watch Question

Dim g As Grapics = Button1.CreateGraphics()
  Dim size As SizeF = g.MeasureString(string1, Button1.Font)

Then size.Width will have the width in pixels of the string as it would be if
drawn in Button1

Now if you need convert pixels to whatever you need
Typo

must be
Dim g As Graphics = Button1.CreateGraphics()
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
It appears that the button is too wide, is this a correct assumption?  How much of a margin do you want for the text?  Is the TextAlign centered for button?

Author

Commented:
it doesn't differ from the code fragment I wrote... and it DOESN'T work good. The text is longer that button width measured by this way...

Author

Commented:
TheLearnedOne  - no - it appears that button is to small. TextAlign has nothing to do with it - since is notthing to align - text just doesn't fit button width:-(
it is different
I dim size as  Dim size As SizeF
 while you have it as integer

I have tried this code and it works fine

Author

Commented:
iboutchkine : come on... method ToSize() is intended to convert SizeF to Size which performs floor convertion on float values of the SizeF to integers so (if you tested it) there is no way (as well as I know) to set width for the button with no convertion from float.
When I tried my code ( and it is the same like your proposal ) it didn't worked for me. Can you publish your test code please?
try this

       Dim g As Graphics = Button1.CreateGraphics()
        Dim size As SizeF = g.MeasureString(Button1.Text, Button1.Font)

        Dim WidthOld As Double = Button1.Width
        Dim WidthNew As Double
        Button1.Text = "this is a long text that does not fit the button"
        Dim sizeNew As SizeF = g.MeasureString(Button1.Text, Button1.Font)
        WidthNew = sizeNew.Width * WidthOld / size.Width
        Button1.Width = WidthNew

Author

Commented:
iboutchkine : hm... in your example button width is much much more than needed for string "this is a long text that does not fit the button" and you use previous text of the button as proportion measure that can be not right....
I am looking for way to define button width that, say, is 5 pixel border from right and left side of the text for example.
In other word - I need to know EXACT width of the button that fits 100% ( and not approximately ) to text it contains.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.