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. Text Box Selection

Text Box Selection

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

    Hi there, Does anybody know how I can select the entire contents of a text box from the end to the start? (thus allowing the start to be displayed in the text box, if the text is too long to fit). Below is (obviously) selecting from left to right: this.txtName.SelectionStart = 0; this.txtName.SelectionLength = txtName.Text.Length; so I need something like: this.txtName.SelectionStart = txtName.Text.Length; this.txtName.SelectionEnd = 0; but Text Box has no SelectionEnd Property... Thanks!

    C S 2 Replies Last reply
    0
    • S StyleGuide

      Hi there, Does anybody know how I can select the entire contents of a text box from the end to the start? (thus allowing the start to be displayed in the text box, if the text is too long to fit). Below is (obviously) selecting from left to right: this.txtName.SelectionStart = 0; this.txtName.SelectionLength = txtName.Text.Length; so I need something like: this.txtName.SelectionStart = txtName.Text.Length; this.txtName.SelectionEnd = 0; but Text Box has no SelectionEnd Property... Thanks!

      C Offline
      C Offline
      CKnig
      wrote on last edited by
      #2

      I didn't test it but try something like // this should select all this.txtName.SelectAll(); // this should set the caret to pos 0 this.txtName.SelectionStart = 0; // and this should scroll: this.txtName.ScrollToCaret(); I know this kind of problems (ever tried implementing syntaxhighlighting in a richttextbox ... well you wouldn't want to use the Selects! you will end using the RTF-code itself ... argh) - so I feel you pain ;)

      S 1 Reply Last reply
      0
      • C CKnig

        I didn't test it but try something like // this should select all this.txtName.SelectAll(); // this should set the caret to pos 0 this.txtName.SelectionStart = 0; // and this should scroll: this.txtName.ScrollToCaret(); I know this kind of problems (ever tried implementing syntaxhighlighting in a richttextbox ... well you wouldn't want to use the Selects! you will end using the RTF-code itself ... argh) - so I feel you pain ;)

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

        Thanks for this CKnig, but no joy :-(

        1 Reply Last reply
        0
        • S StyleGuide

          Hi there, Does anybody know how I can select the entire contents of a text box from the end to the start? (thus allowing the start to be displayed in the text box, if the text is too long to fit). Below is (obviously) selecting from left to right: this.txtName.SelectionStart = 0; this.txtName.SelectionLength = txtName.Text.Length; so I need something like: this.txtName.SelectionStart = txtName.Text.Length; this.txtName.SelectionEnd = 0; but Text Box has no SelectionEnd Property... Thanks!

          S Offline
          S Offline
          Skippums
          wrote on last edited by
          #4

          I cannot reproduce your problem. Specifically, calling this.txtName.SelectAll() outside of the constructor retains the current position of the text within the TextBox. This means that if you are scrolled to the start, then call .SelectAll(), you will still be at the start after the call. If this is not the case for you, please be more specific. If you are scrolling the text or selecting all of it in the constructor for your form, then move that code into the OnShown event for the Form. Good luck,

          Sounds like somebody's got a case of the Mondays -Jeff

          S 1 Reply Last reply
          0
          • S Skippums

            I cannot reproduce your problem. Specifically, calling this.txtName.SelectAll() outside of the constructor retains the current position of the text within the TextBox. This means that if you are scrolled to the start, then call .SelectAll(), you will still be at the start after the call. If this is not the case for you, please be more specific. If you are scrolling the text or selecting all of it in the constructor for your form, then move that code into the OnShown event for the Form. Good luck,

            Sounds like somebody's got a case of the Mondays -Jeff

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

            Thanks Jeff, this.txtName.SelectionStart = 0; Correctly sets the cursor position to 0, but when I add: this.txtName.ScrollToCaret(); this.txtName.SelectAll(); All text inside the textbox is selected, but the cursor position is now at the end of the selection.. Any ideas? Thanks.

            S 1 Reply Last reply
            0
            • S StyleGuide

              Thanks Jeff, this.txtName.SelectionStart = 0; Correctly sets the cursor position to 0, but when I add: this.txtName.ScrollToCaret(); this.txtName.SelectAll(); All text inside the textbox is selected, but the cursor position is now at the end of the selection.. Any ideas? Thanks.

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

              Where in your code are you calling .SelectAll()? Are you calling it prior to the object being displayed? If so, then that is your problem. The GUI state of the textbox stores info about things like where to scroll. On my own computer running VS 2005 with .Net v2.0, I don't see this error at all, unless I make the .SelectAll() call from one of the following locations:

              1. Within the constructor of any object that is initially loaded by the form
              2. Within the Load() event of any object that is initially loaded by the form
              3. Anytime the textbox is not visible

              If this still does not answer your question, then in your next post give me the stack trace from which you are calling the .SelectAll() method. Good luck,

              Sounds like somebody's got a case of the Mondays -Jeff

              S 1 Reply Last reply
              0
              • S Skippums

                Where in your code are you calling .SelectAll()? Are you calling it prior to the object being displayed? If so, then that is your problem. The GUI state of the textbox stores info about things like where to scroll. On my own computer running VS 2005 with .Net v2.0, I don't see this error at all, unless I make the .SelectAll() call from one of the following locations:

                1. Within the constructor of any object that is initially loaded by the form
                2. Within the Load() event of any object that is initially loaded by the form
                3. Anytime the textbox is not visible

                If this still does not answer your question, then in your next post give me the stack trace from which you are calling the .SelectAll() method. Good luck,

                Sounds like somebody's got a case of the Mondays -Jeff

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

                hmmmm.. Thanks Jeff, I'm calling it in the Enter event of the text box..

                S 1 Reply Last reply
                0
                • S StyleGuide

                  hmmmm.. Thanks Jeff, I'm calling it in the Enter event of the text box..

                  S Offline
                  S Offline
                  Skippums
                  wrote on last edited by
                  #8

                  hmmm. I missed that one, but mine did finally do the same thing you described. I got it to work how you requested by doing the following:

                  textBox1_Enter(object sender, EventArgs e) {
                  textBox1.Select(textBox1.TextLength, -textBox1.TextLength);
                  }

                  Hope this helps!

                  Sounds like somebody's got a case of the Mondays -Jeff

                  S 1 Reply Last reply
                  0
                  • S Skippums

                    hmmm. I missed that one, but mine did finally do the same thing you described. I got it to work how you requested by doing the following:

                    textBox1_Enter(object sender, EventArgs e) {
                    textBox1.Select(textBox1.TextLength, -textBox1.TextLength);
                    }

                    Hope this helps!

                    Sounds like somebody's got a case of the Mondays -Jeff

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

                    Great! Thanks Jeff

                    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