Get the number of visible characters in textbox

  • Thread starter Thread starter Nacho Nachev
  • Start date Start date
N

Nacho Nachev

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
 
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.
 
Stoitcho,

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

Kind Regards,
Nacho

/* Mnogo blagodarq! */

Stoitcho Goutsev (100) said:
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.
--
HTH
i kasmet
Stoitcho Goutsev (100) [C# MVP]


Nacho Nachev said:
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
 
Back
Top