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. How to read in text from a textbox

How to read in text from a textbox

Scheduled Pinned Locked Moved C#
tutorialquestion
14 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.
  • Y Yustme

    Hi, I've been looking for like 2 hours for a method or anything that reads the text which is typed in a textbox. I don't really know much about buttons and textfields. I usually work with the console. Can anyone tell me how to read in text from a textbox plz? Thanks in advance!

    N Offline
    N Offline
    Neha_Gupta
    wrote on last edited by
    #2

    suppose that u want to retrieve the data of the textbox to a variable u can do it by the foll code: string data=textboxname.Text;

    NEHA GUPTA

    1 Reply Last reply
    0
    • Y Yustme

      Hi, I've been looking for like 2 hours for a method or anything that reads the text which is typed in a textbox. I don't really know much about buttons and textfields. I usually work with the console. Can anyone tell me how to read in text from a textbox plz? Thanks in advance!

      T Offline
      T Offline
      Tamimi Code
      wrote on last edited by
      #3

      hi textBox1.text will give you the text is typed in textBox1:)

      Tamimi - Code

      Y 1 Reply Last reply
      0
      • T Tamimi Code

        hi textBox1.text will give you the text is typed in textBox1:)

        Tamimi - Code

        Y Offline
        Y Offline
        Yustme
        wrote on last edited by
        #4

        Hi Tamimi, That worked! Thank you! I declared readIn to read what is typed in the textbox like this: string readIn = this.texBox1.Text; And attaching it to the eventhandler like this: readIn += new KeyEventHandler(texBox1_KeyPress); It gives me the error: No overload for 'texBox1_KeyPress' matches delegate 'System.Windows.Forms.KeyEventHandler' What am i doing wrong?

        T 1 Reply Last reply
        0
        • Y Yustme

          Hi Tamimi, That worked! Thank you! I declared readIn to read what is typed in the textbox like this: string readIn = this.texBox1.Text; And attaching it to the eventhandler like this: readIn += new KeyEventHandler(texBox1_KeyPress); It gives me the error: No overload for 'texBox1_KeyPress' matches delegate 'System.Windows.Forms.KeyEventHandler' What am i doing wrong?

          T Offline
          T Offline
          Tamimi Code
          wrote on last edited by
          #5

          hi could you give more details about what you are trying to do ?? if simply you want the text in a textbox the .Text property its enough and no need to attaching anything. what exactly you want to do ??

          Tamimi - Code

          Y 1 Reply Last reply
          0
          • T Tamimi Code

            hi could you give more details about what you are trying to do ?? if simply you want the text in a textbox the .Text property its enough and no need to attaching anything. what exactly you want to do ??

            Tamimi - Code

            Y Offline
            Y Offline
            Yustme
            wrote on last edited by
            #6

            Hi, I want to accept only 0, 1, 2 .... 9 as input. In order to do this, i have to attach texbBox1.Text to the keypressed handler. But i got it fixed now, Thank you!

            T J 2 Replies Last reply
            0
            • Y Yustme

              Hi, I want to accept only 0, 1, 2 .... 9 as input. In order to do this, i have to attach texbBox1.Text to the keypressed handler. But i got it fixed now, Thank you!

              T Offline
              T Offline
              Tamimi Code
              wrote on last edited by
              #7

              hi is there any wrong with this: private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { int intKeyValue = (int)e.KeyChar; if (((intKeyValue<48 || intKeyValue>57 ) && intKeyValue!=46)) { e.Handled = true; } }

              Tamimi - Code

              Y 1 Reply Last reply
              0
              • Y Yustme

                Hi, I want to accept only 0, 1, 2 .... 9 as input. In order to do this, i have to attach texbBox1.Text to the keypressed handler. But i got it fixed now, Thank you!

                J Offline
                J Offline
                JacquesDP
                wrote on last edited by
                #8

                You could also try using the textchanged event of the textbox.

                He who laughs last is a bit on the slow side

                1 Reply Last reply
                0
                • T Tamimi Code

                  hi is there any wrong with this: private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { int intKeyValue = (int)e.KeyChar; if (((intKeyValue<48 || intKeyValue>57 ) && intKeyValue!=46)) { e.Handled = true; } }

                  Tamimi - Code

                  Y Offline
                  Y Offline
                  Yustme
                  wrote on last edited by
                  #9

                  Hi Tamimi, There is nothing wrong with that method. In fact, thats the eventhandler i was trying to attach 'readIn' to. If a key is pressed 'readIn' will call that eventhandler to check if its a valid one. Sorry for the confusion Tamimi!

                  T 1 Reply Last reply
                  0
                  • Y Yustme

                    Hi Tamimi, There is nothing wrong with that method. In fact, thats the eventhandler i was trying to attach 'readIn' to. If a key is pressed 'readIn' will call that eventhandler to check if its a valid one. Sorry for the confusion Tamimi!

                    T Offline
                    T Offline
                    Tamimi Code
                    wrote on last edited by
                    #10

                    hi are you trying to do this: this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress); if not feel free to ask again

                    Tamimi - Code

                    Y 1 Reply Last reply
                    0
                    • T Tamimi Code

                      hi are you trying to do this: this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress); if not feel free to ask again

                      Tamimi - Code

                      Y Offline
                      Y Offline
                      Yustme
                      wrote on last edited by
                      #11

                      Hi Tamimi, Thats exactly what i meant. But i fixed that half an hour ago :) I do have another question... If i want to get everything between 2 chars, how would i do that? Example: [1] [12] [123] How would i get everything between the '[ ]' chars? Substring wont help, coz the characters inside the [] can be sometimes 1, 3 or more. Thanks in advance!

                      T 1 Reply Last reply
                      0
                      • Y Yustme

                        Hi Tamimi, Thats exactly what i meant. But i fixed that half an hour ago :) I do have another question... If i want to get everything between 2 chars, how would i do that? Example: [1] [12] [123] How would i get everything between the '[ ]' chars? Substring wont help, coz the characters inside the [] can be sometimes 1, 3 or more. Thanks in advance!

                        T Offline
                        T Offline
                        Tamimi Code
                        wrote on last edited by
                        #12

                        hi try this: int intStartIndex = textBox1.Text.IndexOf("[") + 1; int intEndIndex = textBox1.Text.IndexOf("]") - 1; string strBetween = textBox1.Text.Substring(intStartIndex,intEndIndex); but you should be sure that the chars "[" and "]" are exists if not you will get an error index or something like this

                        Tamimi - Code

                        Y 1 Reply Last reply
                        0
                        • T Tamimi Code

                          hi try this: int intStartIndex = textBox1.Text.IndexOf("[") + 1; int intEndIndex = textBox1.Text.IndexOf("]") - 1; string strBetween = textBox1.Text.Substring(intStartIndex,intEndIndex); but you should be sure that the chars "[" and "]" are exists if not you will get an error index or something like this

                          Tamimi - Code

                          Y Offline
                          Y Offline
                          Yustme
                          wrote on last edited by
                          #13

                          Hi Tamimi, That worked great! I thank you for all your help and patience! Yustme

                          T 1 Reply Last reply
                          0
                          • Y Yustme

                            Hi Tamimi, That worked great! I thank you for all your help and patience! Yustme

                            T Offline
                            T Offline
                            Tamimi Code
                            wrote on last edited by
                            #14

                            you are welcome:)

                            Tamimi - Code

                            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