private void setCaretPosAndSelect(int pos, int length = 0)
{
textBox1.Focus();
textBox1.Select(pos, length);
Rectangle textBoxBounds = this.textBox1.Bounds;
textBoxBounds = textBox1.Parent.RectangleToScreen(textBoxBounds);
Point caretLocation = textBox1.GetPositionFromCharIndex(textBox1.SelectionStart);
caretLocation = textBox1.Parent.PointToScreen(caretLocation);
Log.Debug("textBounds.Top: " + textBoxBounds.Top +
", caretLocation.Y : " + caretLocation.Y +
", scrolllines*textheight: " + (ScrollThresholdLines * _textHeight));
if ((textBoxBounds.Bottom - caretLocation.Y) <= ScrollThresholdLines * _textHeight)
{
Log.Debug("Scrolling down...");
SendMessage(textBox1.Handle, EM_LINESCROLL, 0, ScrollNumberOfLines);
}
else
{
if (caretLocation.Y < 0)
{
Log.Debug("caretLocation is negative. Calling scroll to caret");
textBox1.ScrollToCaret();
}
caretLocation = textBox1.GetPositionFromCharIndex(textBox1.SelectionStart);
caretLocation = textBox1.Parent.PointToScreen(caretLocation);
if (caretLocation.Y - textBoxBounds.Top <= ScrollThresholdLines * _textHeight)
{
Log.Debug("Scrolling up...");
SendMessage(textBox1.Handle, EM_LINESCROLL, 0, -ScrollNumberOfLines);
}
}
Log.Debug("setCaretPos. SelectionStart: " + textBox1.SelectionStart);
}