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 / February 2005

Tip: Looking for answers? Try searching our database.

How to calculate "physical" width of string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marcin Waligora - 02 Nov 2004 20:43 GMT
Hi all,

I'm trying to create a read-only RichTextBox that will dynamically adjust
it's width to the string I throw into it.
It should be a perfect fit. I must use RichTextBox, because some of the text
can be in different color or be
bolded.

RichTextBox.Size doesn't work, since it is only an approximation.
Does anyone know how to calculate that??

Thanks,
Marcin Waligora
Morten Wennevik - 02 Nov 2004 21:35 GMT
Hi Marcin,

Well, you can use MeasureCharacterRanges to get the exact size given the  
font and control.  Use that to set the RichTextBox size.

Signature

Happy Coding!
Morten Wennevik [C# MVP]

Stoitcho Goutsev \(100\) [C# MVP] - 02 Nov 2004 23:10 GMT
Hi,

It still is not trivial with RichTextBox. First because it may contain text
in different fonts, but mostly because there might be some other addornments
like bulet indents, indents or zoom

Signature

Stoitcho Goutsev (100) [C# MVP]

> Hi Marcin,
>
> Well, you can use MeasureCharacterRanges to get the exact size given the
> font and control.  Use that to set the RichTextBox size.
Dave - 03 Nov 2004 04:31 GMT
What about using the Graphics->MeasureString( String, Font ) method to
capture the width required? Then use the returned size to set your
RichTextBox up.

Dave

> Hi all,
>
[quoted text clipped - 10 lines]
> Thanks,
> Marcin Waligora
Justin Rogers - 03 Nov 2004 04:44 GMT
It has been identified that a RichTextBox is a mixture of many different fonts,
font sizes, and typographical stylings. He would have to call MeasureString
for every combination that exists in his RichTextBox to get the actual size.

If the insistance is still there to use this control, then I'd have to recommend
two things. The first is to ensure that there aren't any newline characters. You
can't, for instance, grow your control to fit the string, if it wraps itself. If
it
doesn't wrap itself, then grow your control in response to GetLineFromCharIndex
where you pass in the index of the last character. If you are double slick, you
can
do the entire process in without resizing more than once...

int lines = rtb.GetLineFromCharIndex(lastChar);
Point p = rtb.GetPositionFromCharIndex(lastChar);

rtb.Width = rtb.Width * (lines - 1) + (p.X);

I wrapped p.X because I'm not sure what coordinate system it is measure in. You
may have to do some math to find out how far from the edge of the control it is
in
case the point is in screen coordinates. To take a shorcut, just use the:

rtb.Width = rtb.Width * lines;

Signature

Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

> What about using the Graphics->MeasureString( String, Font ) method to capture
> the width required? Then use the returned size to set your RichTextBox up.
[quoted text clipped - 14 lines]
>> Thanks,
>> Marcin Waligora
Dotnetjunky - 20 Feb 2005 14:09 GMT
Hi,

You don't need to do any measurement at all, because RichTextBox has a very
little-known event called ContentSizeChanged, which is called everytime the
content of RichTextBox changes that causes an overflow out of the current
control size. Inside the event handler, you have access to the Rectangle
which will perfectly fit the new contents.

Hope that helps.

Dotnetjunky

> Hi all,
>
[quoted text clipped - 9 lines]
> Thanks,
> Marcin Waligora
DalePres - 20 Feb 2005 14:22 GMT
I think you're referring to the ContentsResized event.

DalePres
MCAD, MCDBA,  MCSE

> Hi,
>
[quoted text clipped - 24 lines]
>> Thanks,
>> Marcin Waligora

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.