GetPositionFromCharIndex(int index) in RichTextBox v.s. TextBox

J

JamesDastard

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 <[email protected]>
 
C

ClayB

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top