473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Get the 'size' of a line of text to set width of control

Tom
I want to get the 'size' of a line of text so that I can set the width of a
control to show all that text. For instance, say my text is 'The dog chased
the cat around the room' and I want to set a Button control's width equal to
the length of this text so that all the text shows up on the button (I know,
some controls have auto-sizing, but I need to do this manually at run time).

How can one determine and translate the 'size' of a text string into the
proper width for a control? Thanks.

Tom
Nov 20 '05 #1
2 1294
jim
Hi Tom, try this...

\\
Dim txt As String = "My shoes are too tight!"
Dim g As Graphics = myButton.CreateGraphics
Dim controlSize As SizeF = g.MeasureString(txt, selectGrid.Font)

myButton.Width = CInt(controlSize.Width) ' option strict on requires
this cast
myButton.Height = CInt(controlSize.Height)
myButton.Text = txt
//

hope this helps,

jim
"Tom" <to*@nospam.com> wrote in message
news:e2**************@TK2MSFTNGP09.phx.gbl...
I want to get the 'size' of a line of text so that I can set the width of a control to show all that text. For instance, say my text is 'The dog chased the cat around the room' and I want to set a Button control's width equal to the length of this text so that all the text shows up on the button (I know, some controls have auto-sizing, but I need to do this manually at run time).
How can one determine and translate the 'size' of a text string into the
proper width for a control? Thanks.

Tom

Nov 20 '05 #2
* "Tom" <to*@nospam.com> scripsit:
I want to get the 'size' of a line of text so that I can set the width of a
control to show all that text. For instance, say my text is 'The dog chased
the cat around the room' and I want to set a Button control's width equal to
the length of this text so that all the text shows up on the button (I know,
some controls have auto-sizing, but I need to do this manually at run time).


Have a look at the 'Graphics.MeasureString' method.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Penguin | last post by:
I am still using Access 97 which is very stubborn about using a whole number for font size - even in code. Wondering if 2000 will let you do otherwise?
8
by: Jack | last post by:
Hi, I have a asp page where multiple rows for a client is generated using asp. Some of these are combo boxes and some are text and are coming from a recordset. Now, the following is the code to...
2
by: Dominique | last post by:
Hello, Im want to fix the width of a dashboard. I have a field which has a field (in this dashboard on the left part of my screen) which could be long and make the right side of the screen out...
18
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.