Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. TextBox setting caret at end of text

TextBox setting caret at end of text

Scheduled Pinned Locked Moved C#
questionworkspace
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    ormonds
    wrote on last edited by
    #1

    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?

    D B 2 Replies Last reply
    0
    • O ormonds

      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?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      O M 2 Replies Last reply
      0
      • D 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

        O Offline
        O Offline
        ormonds
        wrote on last edited by
        #3

        Again, thank you.

        1 Reply Last reply
        0
        • D 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

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          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

          D 1 Reply Last reply
          0
          • M Mycroft Holmes

            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

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • O ormonds

              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?

              B Offline
              B Offline
              BillWoodruff
              wrote on last edited by
              #6

              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.

              «

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups