TextBox setting caret at end of text
-
I am trying to set the caret at the end of a textbox so it scrolls up as new lines get added. These are added by:
this.textBox1.Text += ThisText + System.Environment.NewLine;
However even though the value of this.textBox1.SelectionStart is correct (i.e. a large number), when I get to these lines:-
this.textBox1.SelectionStart = this.textBox1.Text.Length;
this.Refresh();The textbox doesn't scroll. Can someone tell me what I am missing?
-
I am trying to set the caret at the end of a textbox so it scrolls up as new lines get added. These are added by:
this.textBox1.Text += ThisText + System.Environment.NewLine;
However even though the value of this.textBox1.SelectionStart is correct (i.e. a large number), when I get to these lines:-
this.textBox1.SelectionStart = this.textBox1.Text.Length;
this.Refresh();The textbox doesn't scroll. Can someone tell me what I am missing?
As easy way to do this is to just call
textBox1.AppendText(ThisText + Environment.NewLine);
Stop putting
this
on everything. You don't need it as it's already implied.Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
As easy way to do this is to just call
textBox1.AppendText(ThisText + Environment.NewLine);
Stop putting
this
on everything. You don't need it as it's already implied.Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
As easy way to do this is to just call
textBox1.AppendText(ThisText + Environment.NewLine);
Stop putting
this
on everything. You don't need it as it's already implied.Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakDave Kreskowiak wrote:
Stop putting
this
on everything.That was my idea :-O to remind him where the control lives (he was creating a new form and lost the plot) Implied can be confusing the this newbie, thought it might help.
Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP
-
Dave Kreskowiak wrote:
Stop putting
this
on everything.That was my idea :-O to remind him where the control lives (he was creating a new form and lost the plot) Implied can be confusing the this newbie, thought it might help.
Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP
Ah. I did not know.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
I am trying to set the caret at the end of a textbox so it scrolls up as new lines get added. These are added by:
this.textBox1.Text += ThisText + System.Environment.NewLine;
However even though the value of this.textBox1.SelectionStart is correct (i.e. a large number), when I get to these lines:-
this.textBox1.SelectionStart = this.textBox1.Text.Length;
this.Refresh();The textbox doesn't scroll. Can someone tell me what I am missing?
This is a swamp I have waded in, before. The cheap solution: put code to update the TextBox caret in the Form's 'Shown event handler. But, if you want to make the TextBox as independent as possible of its container, and, assuming: 1. this is a WinForm app with a TextBox set to show a vertical scrollbar 2. you will set the Text property at design time, or in the 'Load event of its container. 3. when the Form is shown, you want the TextBox scrolled to the end, and the TextBox selected/focused so if you start typing, what you type is added to the TextBox:
using System;
using System.Windows.Forms;namespace YOURNAMESPACE
{
public partial class AutoScrolledTbx : UserControl // contains a TextBox
{
public AutoScrolledTbx()
{
InitializeComponent();
}// for use creating an instance in code at run-time public AutoScrolledTbx(string txt) { textBox1.Text = txt; this.textBox1.Select(this.textBox1.TextLength + 1, 0); this.textBox1.ScrollToCaret(); this.textBox1.Capture = true; this.textBox1.Focus(); } private void AutoScrolledTbx\_Load(object sender, EventArgs e) { // Marc Gravell this.BeginInvoke((MethodInvoker)this.ForceScroll); } // adapted from Marc Gravell // https://stackoverflow.com/a/218740/133321 private void ForceScroll() { // UI thread update this.Invoke( (MethodInvoker)delegate { this.textBox1.Select(this.textBox1.TextLength + 1, 0); this.textBox1.ScrollToCaret(); this.textBox1.Focus(); ActiveControl = this.textBox1; } ); } // public method to add text public void AppendText(string txt) { this.textBox1.AppendText(txt); } }
}
Discussion: When a Form is being 'Loaded, then displayed, Controls may not appear in an expected state. By pushing a task onto the UI thread using Marc Gravell's technique shown here, you have achieved the equivalent of executing the update code in the Form's 'Shown event. In case you wonder why a UserControl is used here, rather than a sub-classed TextBox: a TextBox offers no 'Load or other initialization event to manipulate.
«