Groups | Blog | Home
all groups > dotnet windows forms > january 2007 >

dotnet windows forms : GetPositionFromCharIndex(int index) in RichTextBox v.s. TextBox


JamesDastard NO[at]SPAM gmail.com
1/30/2007 11:12:38 PM
Dear all,

I found the GetPositionFromCharIndex(int index) is a little different
from that in TextBox. And I have a question.

Explain:
If the length of TextBox & RichTextBox are both 2.
textBox1.GetPositionFromCharIndex(2) -> invalid, return 0.
textBox1.GetPositionFromCharIndex(1) -> valid, return the
left bound position of the last
richTextBox1.GetPositionFromCharIndex(2) -> valid, return the right
bound position of the last character.

However, because each character are not in the same width, I cannot
count the bound manually.

Thus, does anybody know how could I get the "right bound position" of
the last character in "TextBox" but not "RichTextBox" ?

Wait for your help.

Sincerely,
James JAN <JamesDastard@gmail.com>
ClayB
2/1/2007 3:01:35 AM
Maybe code like this will give you what you want:

using (Graphics g = textBox1.CreateGraphics())
{
int len = textBox1.TextLength;
int rightEdge = textBox1.GetPositionFromCharIndex(len - 1).X
+ (int)
g.MeasureString(textBox1.Text.Substring(len - 1, 1),
textBox1.Font).Width;
}

===============================
Clay Burch
Syncfusion, Inc.
AddThis Social Bookmark Button