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. Cut, Copy & Paste from variable text boxes

Cut, Copy & Paste from variable text boxes

Scheduled Pinned Locked Moved C#
help
7 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.
  • M Offline
    M Offline
    Martin31088
    wrote on last edited by
    #1

    Hi guys, What I'm trying to do is cut copy and paste the selected text from any text box in my application. I have it working if it copies the entire text from the box, and I have it working if I hard code the text box name into to function. Ive looked all over for an answer to this, and have got nowhere This is the current code I am using:

    string ActiveCon = this.ActiveControl.Name;
    string SelText = ActiveCon.SelectedText;
    Clipboard.SetText(SelText);
    this.ActiveControl.Text = "";

    It just does not like SelectedText used on a variable Any help on this would be greatly appreciated. Regards Martin

    L D L 3 Replies Last reply
    0
    • M Martin31088

      Hi guys, What I'm trying to do is cut copy and paste the selected text from any text box in my application. I have it working if it copies the entire text from the box, and I have it working if I hard code the text box name into to function. Ive looked all over for an answer to this, and have got nowhere This is the current code I am using:

      string ActiveCon = this.ActiveControl.Name;
      string SelText = ActiveCon.SelectedText;
      Clipboard.SetText(SelText);
      this.ActiveControl.Text = "";

      It just does not like SelectedText used on a variable Any help on this would be greatly appreciated. Regards Martin

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      TextBoxes don't have a SelectedText property, I believe that this is limited to RichTextBoxes. There's also a Copy[^] method that does what you want :)

      I are troll :)

      B 1 Reply Last reply
      0
      • M Martin31088

        Hi guys, What I'm trying to do is cut copy and paste the selected text from any text box in my application. I have it working if it copies the entire text from the box, and I have it working if I hard code the text box name into to function. Ive looked all over for an answer to this, and have got nowhere This is the current code I am using:

        string ActiveCon = this.ActiveControl.Name;
        string SelText = ActiveCon.SelectedText;
        Clipboard.SetText(SelText);
        this.ActiveControl.Text = "";

        It just does not like SelectedText used on a variable Any help on this would be greatly appreciated. Regards Martin

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

        Why are you using a string for the active control?

        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)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        1 Reply Last reply
        0
        • M Martin31088

          Hi guys, What I'm trying to do is cut copy and paste the selected text from any text box in my application. I have it working if it copies the entire text from the box, and I have it working if I hard code the text box name into to function. Ive looked all over for an answer to this, and have got nowhere This is the current code I am using:

          string ActiveCon = this.ActiveControl.Name;
          string SelText = ActiveCon.SelectedText;
          Clipboard.SetText(SelText);
          this.ActiveControl.Text = "";

          It just does not like SelectedText used on a variable Any help on this would be greatly appreciated. Regards Martin

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, You can't just get directly to a TextBox when all you know is its name. Here is a little snippet that would do something akin to what you want, assuming your class is a Form or whatever Container that is holding the TextBoxes:

          public string GetTextBoxText(string name) {
          string text=null;
          foreach(Control c in Controls) {
          TextBox tb=c as TextBox;
          if (tb!=null && tb.Name==name) text=tb.SelectedText;
          }
          return text;
          }

          BTW: If some of your TextBoxes are located deeper in the Control hierarchy, you would have to adapt for that. PS: as Dave said, if you would hold a reference to the ActiveControl, rather than just holding its name, you could get to its text right away! :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


          1 Reply Last reply
          0
          • L Lost User

            TextBoxes don't have a SelectedText property, I believe that this is limited to RichTextBoxes. There's also a Copy[^] method that does what you want :)

            I are troll :)

            B Offline
            B Offline
            Baeltazor
            wrote on last edited by
            #5

            Sorry to correct you eddy but TextBox's do have a SelectedText property. :-) jason

            L 1 Reply Last reply
            0
            • B Baeltazor

              Sorry to correct you eddy but TextBox's do have a SelectedText property. :-) jason

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Found it, along with the SelectionStart and the SelectionLength :) Thanks for the correction :thumbsup:

              I are troll :)

              B 1 Reply Last reply
              0
              • L Lost User

                Found it, along with the SelectionStart and the SelectionLength :) Thanks for the correction :thumbsup:

                I are troll :)

                B Offline
                B Offline
                Baeltazor
                wrote on last edited by
                #7

                You're welcome Eddy :-)

                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