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. select all the text in the textbox c# winform

select all the text in the textbox c# winform

Scheduled Pinned Locked Moved C#
csharpquestion
6 Posts 5 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.
  • F Offline
    F Offline
    fady_sayegh
    wrote on last edited by
    #1

    hi to all, i'm trying to select all the text in a textbox control (winform) on the enter event. textBox1.SelectionStart = 0; textBox1.SelectionLength = textBox1.TextLength; when the enter event of the textbox si fired with the tab, the text is selected correctly. but when the enter event of the textbox si fired with the mouse click, no text will be selected in textbox does any body knows why ? if yes can you tell me what the solution is ? best regards and thanks in advance fady sayegh

    E E J L 4 Replies Last reply
    0
    • F fady_sayegh

      hi to all, i'm trying to select all the text in a textbox control (winform) on the enter event. textBox1.SelectionStart = 0; textBox1.SelectionLength = textBox1.TextLength; when the enter event of the textbox si fired with the tab, the text is selected correctly. but when the enter event of the textbox si fired with the mouse click, no text will be selected in textbox does any body knows why ? if yes can you tell me what the solution is ? best regards and thanks in advance fady sayegh

      E Offline
      E Offline
      Ed Poore
      wrote on last edited by
      #2

      That's by design I think. I'll let you know if I think of a work-a-round. Ed

      1 Reply Last reply
      0
      • F fady_sayegh

        hi to all, i'm trying to select all the text in a textbox control (winform) on the enter event. textBox1.SelectionStart = 0; textBox1.SelectionLength = textBox1.TextLength; when the enter event of the textbox si fired with the tab, the text is selected correctly. but when the enter event of the textbox si fired with the mouse click, no text will be selected in textbox does any body knows why ? if yes can you tell me what the solution is ? best regards and thanks in advance fady sayegh

        E Offline
        E Offline
        Eric Dahlvang
        wrote on last edited by
        #3

        The MouseClick event happens after the Enter event. This is so that the user can click where they want the cursor to be positioned. Try placing your code in the MouseClick event. ---------- There go my people. I must find out where they are going so I can lead them. - Alexander Ledru-Rollin

        1 Reply Last reply
        0
        • F fady_sayegh

          hi to all, i'm trying to select all the text in a textbox control (winform) on the enter event. textBox1.SelectionStart = 0; textBox1.SelectionLength = textBox1.TextLength; when the enter event of the textbox si fired with the tab, the text is selected correctly. but when the enter event of the textbox si fired with the mouse click, no text will be selected in textbox does any body knows why ? if yes can you tell me what the solution is ? best regards and thanks in advance fady sayegh

          J Offline
          J Offline
          Josh Smith
          wrote on last edited by
          #4

          You can postpone the text selection until after the mouse event processing occurs, which ensures that clicking into the TextBox will have the same effect as tabbing into it.

          private void textBox1_Enter(object sender, System.EventArgs e)
          {
          this.textBox1.BeginInvoke( new MethodInvoker( this.textBox1.SelectAll ) );
          }

          Josh

          F 1 Reply Last reply
          0
          • J Josh Smith

            You can postpone the text selection until after the mouse event processing occurs, which ensures that clicking into the TextBox will have the same effect as tabbing into it.

            private void textBox1_Enter(object sender, System.EventArgs e)
            {
            this.textBox1.BeginInvoke( new MethodInvoker( this.textBox1.SelectAll ) );
            }

            Josh

            F Offline
            F Offline
            fady_sayegh
            wrote on last edited by
            #5

            thanks josh, it works.

            1 Reply Last reply
            0
            • F fady_sayegh

              hi to all, i'm trying to select all the text in a textbox control (winform) on the enter event. textBox1.SelectionStart = 0; textBox1.SelectionLength = textBox1.TextLength; when the enter event of the textbox si fired with the tab, the text is selected correctly. but when the enter event of the textbox si fired with the mouse click, no text will be selected in textbox does any body knows why ? if yes can you tell me what the solution is ? best regards and thanks in advance fady sayegh

              L Offline
              L Offline
              LongRange Shooter
              wrote on last edited by
              #6

              X| X| X| lots of frustration and angst!!! I have the answer for you...just solved it in my game I'm writing. Unfortunately my machine died last night (and is now on the curb for trash pickup) and the answer is sitting on my couch in the drive cage!!! I had to tinker with things a bit but I did get it to work. Ahhh I remember now....

              private void myTextbox_Enter( object sender, MouseEventArgs args )
              {
              myTextBox.SelectAll();
              }

              That should do it for you. Darn inconsistant behaviour!!

              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