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 / October 2004

Tip: Looking for answers? Try searching our database.

Get the number of visible characters in textbox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nacho Nachev - 01 Oct 2004 11:46 GMT
Hello,

I have a readonly textbox that is limited in width. There are cases when the
text in it exceeds this limit. In such cases I want to add "..." at the end
(for example: "somelongt..."). The problem is that I don't know how many
characters the control cancontain, because its seems that there is not
concept of font width.

Is there some pattern for doing this in .NET WinForms? Or is it safe to
count the number of characters that the control can hold on my machine and
hardcode it, assuming that it will be this on all configuration? The form is
not sizeable.

Thank you for your time,
Nacho
Stoitcho Goutsev \(100\) [C# MVP] - 01 Oct 2004 14:32 GMT
Hi Nacho,

AFIAK there is no such property in TextBox nor in Label. You can use this
functionality out of the box only when you draw text directly using Graphics
objects (see StringFormat.Trimming property). However in the case where you
want to trim with ellipsis at the end of the string it is not hard to
calculate the text length by yourself. Other trimming are not hard either,
but need more work.

So here is a sample that does this

string longStr = "This is some string that doesn't fit in the textbox";
  Font f = this.textBox1.Font;
  Rectangle rect = textBox1.ClientRectangle;
  int charFitted;
  int linesFitted;
  using(Graphics g = textBox1.CreateGraphics())
  {

   StringFormat sf = new StringFormat(StringFormatFlags.NoWrap);
   sf.LineAlignment = StringAlignment.Center;
   sf.Alignment = StringAlignment.Near;
   sf.Trimming = StringTrimming.EllipsisCharacter;
   g.MeasureString(longStr, f, rect.Size, sf, out charFitted, out
linesFitted);
  }
  textBox1.Text = longStr.Substring(0, charFitted) + ((charFitted <
longStr.Length)?"...":"");

The results are not the best possible, but are pretty decent.
Signature

HTH
i kasmet
Stoitcho Goutsev (100) [C# MVP]

> Hello,
>
[quoted text clipped - 14 lines]
> Thank you for your time,
> Nacho
Nacho Nachev - 01 Oct 2004 15:18 GMT
Stoitcho,

Thank you for providing so complete solution. It works fine for me!

Kind Regards,
Nacho

/* Mnogo blagodarq! */

> Hi Nacho,
>
[quoted text clipped - 44 lines]
> > Thank you for your time,
> > Nacho

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.