Introduction
This is an article to solve a simple problem. I mean to set scrollbar at the end of the text in textbox when dynamically updating the text value on that textbox.
Description
Hi all, I have come to give a very simple solution for the below question. The question is …
How to set scrollbar at the end of the text in textbox when dynamically updating the text value?
Actually I have been developing a big application. In that application, I needed to update the status of every action in my application. For the purpose of updating the status, I have used the textbox to show the status with multiline and vertical scrollbar properties have been set. But I could only see the vertical bar not going down when updating the status as shown in the below figure which is marked in red.
After Googling for a few minutes, I found that we have some properties and methods in textboxes of Visual Studio - SelectionStart
and ScrollToCaret
methods.
By using the above property and method, I have found the solution for my question. See the below figure:
Using the Code
private void btnUpdate_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtStatus.Text))
txtStatus.Text = txtStatus.Text + Environment.NewLine;
txtStatus.Text = txtStatus.Text + "Status Updated...";
txtStatus.SelectionStart = txtStatus.Text.Length;
txtStatus.ScrollToCaret();
txtStatus.Refresh();
}
Conclusion
Hence my doubt has been solved. :)
History
- 3rd August, 2009: Initial post