Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / November 2006

Tip: Looking for answers? Try searching our database.

Figure out how much text will fit into the form title

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eve - 24 Nov 2006 17:08 GMT
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!
Bob Powell [MVP] - 25 Nov 2006 14:05 GMT
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.

Signature

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.

> I'm assigning a description to my form title, such as:
> strDesc = "This is a test if the description will fit in"
[quoted text clipped - 9 lines]
>
> How can I do this? Thank you!
Roman Wagner - 27 Nov 2006 15:40 GMT
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

Bob Powell [MVP] schrieb:

> 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.
[quoted text clipped - 28 lines]
> >
> > How can I do this? Thank you!
Eve - 28 Nov 2006 20:11 GMT
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
ThunderMusic - 29 Nov 2006 18:18 GMT
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

> Thanks for all the help. This is what worked for me:
>
[quoted text clipped - 16 lines]
>            Me.Text = Mid(formTitle, 1, charactersFitted - 1) & "...)"
>        End If

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.