James S. Jan

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 character.
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>


Re: Windows Forms General GetPositionFromCharIndex(int index) in RichTextBox v.s. TextBox

nahguam

It's a bit long winded but you could use Graphics.MeasureString to measure the character. That will return a SizeF object. You could then add the SizeF.Width to the left bound position.



Re: Windows Forms General GetPositionFromCharIndex(int index) in RichTextBox v.s. TextBox

James S. Jan

Thanks to nahguam!
I try your method and it almost works, thought I can only do it in the PaintEvent area.
However, if i want to get the size of sub-string , it is not pricise enough.

For example:
{{{
string t1="olol";
string t2="olo";
SizeF sizeF1 = pe.Graphics.MeasureString(t1.SubString(0,3), Font);
SizeF sizeF2 = pe.Graphics.MeasureString(t2, Font);
}}}
and I found that ====> sizeF1 != sizeF2
It doesn't make any sence.
Who can tell me why









Re: Windows Forms General GetPositionFromCharIndex(int index) in RichTextBox v.s. TextBox

James S. Jan

Wow, I found the reason.
Seems that using Graphics.MeasureStinrg will reserve a margin for user.
But how could I found the margin






Re: Windows Forms General GetPositionFromCharIndex(int index) in RichTextBox v.s. TextBox

nahguam

Yes it does reserve a margin. Just like if you set a label to AutoSize.

However, I tried your code and both returned the exact same SizeF:

Graphics g = this.CreateGraphics();

string t1 = "olol";

string t2 = "olo";

SizeF sizeF1 = g.MeasureString(t1.Substring(0, 3), Font);

SizeF sizeF2 = g.MeasureString(t2, Font);

Console.WriteLine(sizeF1);

Console.WriteLine(sizeF2);

if (sizeF1 == sizeF2)

{

Console.WriteLine("equal");

}

else

{

Console.WriteLine("not equal");

}

Output:

{Width=18.85075, Height=13.8252}

{Width=18.85075, Height=13.8252}
equal





Re: Windows Forms General GetPositionFromCharIndex(int index) in RichTextBox v.s. TextBox

James S. Jan

Sorry I was wrong.
Just as you said, the values are the same.
I used the sizeF return by Graphics.Drawstring to draw text,
but am confused that even the values are the same, how come did I find out the intervals between characters are changing

p.s. The strings I typed are using Chinese characters. (UTF8)

Ex:
a= "你好"
b= "你好" in "你好ㄇ"
Graphics.DrawString (a, Font, 0.0f, 0.0f);
Graphics.DrawString (b.SubString(0,2) , Font, 0.0f, 50.0f);
After using Graphics.DrawString to draw, the intervala of "你" and "好" are different and look like as follows,

====================
你好


你 好
====================

quite a little different!


James Jan




Re: Windows Forms General GetPositionFromCharIndex(int index) in RichTextBox v.s. TextBox

James S. Jan

{{{
Ex: using "Graphics.MeasureString & Graphics.DrawString"
MeasureString (abc)==MeasureString (ab) +MeasureString (c)

but width of DrawString(abc) != width of DrawString(ab) + width of DrawString(c)
}}}

Finally, nobugz gave me an idea to try TextRenderer.DrawText to solve the problem, even this method took more time. Seems that TextRenderer can actually calculate the position and draw text very carefully. Then the time cost is worthy. Thanks for your help.

Best Regards,
James Jan