I have a WindowsForm application that has a FlowLayoutPanel
container with a TextBox
inside.
This control is bigger than the flow panel, I've set the flow panel to AutoScroll = true
.
The problem is I don't know how to make the flow panel scroll to the position of the text edition. If I write continuously in the textbox eventually I pass beyond of what it is visible. The scroll remains at the top and I can't see what it is written.
In consequence the question is, how can I make the container react to keep visible what it is being written?
I think I finally did it:
public partial class Form1 : Form
{
public Point InitialTextBoxLoc ;
public Form1()
{
InitializeComponent();
InitialTextBoxLoc = textBox1.Location;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
Point caretLocalLoc = textBox1.GetPositionFromCharIndex(textBox1.Text.Length-1);
Point caretLoc = new Point(caretLocalLoc.X + InitialTextBoxLoc.X,
caretLocalLoc.Y + InitialTextBoxLoc.Y);
Point scrollLoc = flowLayoutPanel1.AutoScrollPosition;
if (caretLoc.X >= flowLayoutPanel1.Size.Width-10)
{
scrollLoc.X = caretLoc.X;
}
if (caretLoc.Y >= flowLayoutPanel1.Size.Height-10)
{
scrollLoc.Y = caretLoc.Y;
}
flowLayoutPanel1.AutoScrollPosition = scrollLoc;
}
}