Reputation: 1353
I am making a text editor and I was wondering if there are any faster/more reliable ways to do perform these functions. What I have written here is the best I think I can do and might be able to be done, except I have a feeling FirstVisibleLine and LineCount are done but VisibleLines could return faster...?
VisibleLines should return how many lines of text are visible in the text area. FirstVisibleLine should return the first visible line in the text area LineCount should return the number of lines in the text area
private int VisibleLines()
{
int topIndex = this.GetCharIndexFromPosition(new Point(0, 0));
int bottomIndex = this.GetCharIndexFromPosition(new Point(0, this.Height - 1));
int topLine = this.GetLineFromCharIndex(topIndex);
int bottomLine = this.GetLineFromCharIndex(bottomIndex);
return bottomLine - topLine;
}
private int FirstVisibleLine()
{
return this.GetLineFromCharIndex(this.GetCharIndexFromPosition(new Point(0,0)));
}
public int LineCount
{
get
{
Message msg = Message.Create(this.Handle, EM_VISIBLELINES, IntPtr.Zero, IntPtr.Zero);
base.DefWndProc(ref msg);
return msg.Result.ToInt32();
}
}
Upvotes: 0
Views: 248
Reputation: 818
You can get the first visible line by sending an message to the text box to get the top line instead of using GetCharIndexFromPosition
and GetLineFromCharIndex
once. I'm not sure why the TextBox (or TextBoxBase) class doesn't implement this.
private const int EM_GETFIRSTVISIBLELINE = 206;
private int GetFirstVisibleLine(TextBox textBox)
{
return SendMessage(textBox.Handle, EM_GETFIRSTVISIBLELINE, 0, 0);
}
As for VisibleLines, I think you'll have to continue calculating bottomLine
.
Upvotes: 2