Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to get the last line of a rtb when the vertical scroolbar becomes visible.

WordWrap is true


I allready have a methode which tells me when the scroolbar becomes visible.

I measured the string of the last line and compared it to the width > everything which is bigger is in the last line. My Problem is, that when it reaches about the middle, it says it allready reached the end.

Heres my code:
C#
float sum = 0;
int zeichen = 0;
Graphics g = rtb.CreateGraphics();
g.PageUnit = GraphicsUnit.Pixel;
for (int i = 0; i < rtb.Lines[rtb.Lines.Length - 1].Length && sum < rtb.Width; i++)
{
    zeichen = i;
    rtb.Select(rtb.GetFirstCharIndexFromLine(rtb.Lines.Length - 1) + i, 1);
    sum = sum + g.MeasureString(rtb.SelectedText, rtb.SelectionFont).Width;
}
string s = rtb.Text.Substring(zeichen);
rtb.Text = rtb.Text.Substring(0, zeichen);


SelectionFont = new System.Drawing.Font("Agency FB", 10)

rtb.width = 605

Thanks :)
Posted
Comments
Sergey Alexandrovich Kryukov 15-Apr-14 16:36pm    
"About the middle" can be just a bug, but you should understand one more thing: MeasureString gives very inaccurate results, by some reasons related to font hinting. It's a shame, of course, but this is so...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900