Figure out how much text will fit into the form title

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm assigning a description to my form title, such as:
strDesc = "This is a test if the description will fit in"
frmTest.Text = "(" & strDesc & ")"

If strDesc doesn't fit in the title, I don't see the closing paranthesis:
("This is a test if the description will fi..."

I would like to be able to determine if strDesc will fit into the space
provided, and if it won't fit, I would like display only as much as would fit:
("This is a test if the description will f...)"

How can I do this? Thank you!
 
You can use Graphics.MeasureString to get a size. You'll need to maybe use a
fudge factor to account for the buttons and the icons.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Measuring the string will help you to find out whether the string fits
or not, but if you want to get the max string that fits the width of
your form u have a problem.

There ar 2 solutions in my mind:
1) use a font with a fixed character width, measure the width of one
char and display
x chars of your string where FormWidth > charWidth * x

2) do something like a binary search:
a) while the string is too long retry with the first half of your
string after that
b) while the string is not too long add the half of the remaining
string

not very nice but it will run
 
Thanks for all the help. This is what worked for me:

Dim formTitle As String = frmWizardTitle
Dim textFont As New Font("Trebuchet MS", 10, FontStyle.Bold,
GraphicsUnit.Point)
Dim textSize As New SizeF
Dim textFormat As New StringFormat
Dim charactersFitted As Integer
Dim linesFilled As Integer

' See how many characters will fit into the form's title
' 100 is approximately how much space in the form's title are taken
by the icon and the Minimize, Maximize, and Close buttons
textSize = e.Graphics.MeasureString(formTitle, textFont, _
New SizeF(Me.Width - 100, 10), _
textFormat, charactersFitted, linesFilled)

If charactersFitted < Len(frmWizardTitle) Then
Me.Text = Mid(formTitle, 1, charactersFitted - 1) & "...)"
End If
 
hi,

will it still work if I use Large fonts? If I change the font in my Windows
preferences, will work too? I don't know what you want to do with your
application, but I think those 2 questions should be worth looking at...

the answer to both questions here is 'no'... because you use a font with a
given name instead of getting the font from the user's preferences... ;)
it's not a big deal because most users won't change these settings.
Actually, most users don't even know they can change these settings, but for
those who know, it would be kind to think about them too... ;)

I hope it helps

ThunderMusic
 
Back
Top