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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Select all Text in TextBox and Set cursor to Start

Select all Text in TextBox and Set cursor to Start

Scheduled Pinned Locked Moved C#
question
8 Posts 3 Posters 9 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.
  • S Offline
    S Offline
    StyleGuide
    wrote on last edited by
    #1

    Hi there, I'm trying to Select All text in a text box and set to cursor position to 0. I've tryed a few things including :

            this.textBox1.SelectionStart = this.textBox1.Text.Length;
            this.textBox1.SelectAll();
    

    however this sets the cursor position to the end of the Text. Any ideas? Thanks Jon

    realJSOPR 1 Reply Last reply
    0
    • S StyleGuide

      Hi there, I'm trying to Select All text in a text box and set to cursor position to 0. I've tryed a few things including :

              this.textBox1.SelectionStart = this.textBox1.Text.Length;
              this.textBox1.SelectAll();
      

      however this sets the cursor position to the end of the Text. Any ideas? Thanks Jon

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #2

      Try setting the SelectionStart property to 0.

      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

      S 1 Reply Last reply
      0
      • realJSOPR realJSOP

        Try setting the SelectionStart property to 0.

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        S Offline
        S Offline
        StyleGuide
        wrote on last edited by
        #3

        Thanks John, But the cursor is still placed at the end of the Text.. Jon

        D realJSOPR 2 Replies Last reply
        0
        • S StyleGuide

          Thanks John, But the cursor is still placed at the end of the Text.. Jon

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          This is a wierd one - but so are many things with the text box - they really need to give us a new proper .net one instead of wrapping the native one. Anyway - this is a 'dirty' solution but it works. Places the selection point at the end and Shift + Left Arrows for every character.

          private void SelectAllReverse(TextBox control)
          {
          if (control.TextLength > 0)
          {
          control.SelectionStart = control.TextLength;
          for (int i = 0; i < control.TextLength; i++)
          {
          SendKeys.Send("+{LEFT}");
          }
          }
          }

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

          S 1 Reply Last reply
          0
          • S StyleGuide

            Thanks John, But the cursor is still placed at the end of the Text.. Jon

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            Try reversing the order of your statements so that you're highlighting first, and setting the cursor position second (the position still needs to be 0).

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            S 1 Reply Last reply
            0
            • D DaveyM69

              This is a wierd one - but so are many things with the text box - they really need to give us a new proper .net one instead of wrapping the native one. Anyway - this is a 'dirty' solution but it works. Places the selection point at the end and Shift + Left Arrows for every character.

              private void SelectAllReverse(TextBox control)
              {
              if (control.TextLength > 0)
              {
              control.SelectionStart = control.TextLength;
              for (int i = 0; i < control.TextLength; i++)
              {
              SendKeys.Send("+{LEFT}");
              }
              }
              }

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

              S Offline
              S Offline
              StyleGuide
              wrote on last edited by
              #6

              Great - Thanks for the Tip Dave

              D 1 Reply Last reply
              0
              • realJSOPR realJSOP

                Try reversing the order of your statements so that you're highlighting first, and setting the cursor position second (the position still needs to be 0).

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                S Offline
                S Offline
                StyleGuide
                wrote on last edited by
                #7

                Ok cheers, I'll have a further play. Jon

                1 Reply Last reply
                0
                • S StyleGuide

                  Great - Thanks for the Tip Dave

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #8

                  Just discovered :doh: Shift + Home does the same thing so set the caret to the end and send that key combination and it will do it in one shot. :-D

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                  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