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. TextBox for number

TextBox for number

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

    Hi I want to create a text box only for number i create a user control that inherits from textbox and override onkeypress to enter only number but i can't do it . I try with processKeyMessage but i can't again is nobody can help me. Regards' Amirjalaly

    H 1 Reply Last reply
    0
    • A Amirjalaly

      Hi I want to create a text box only for number i create a user control that inherits from textbox and override onkeypress to enter only number but i can't do it . I try with processKeyMessage but i can't again is nobody can help me. Regards' Amirjalaly

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      What can't you do? Please be more specific. A simple example using OnKeyPress would look like:

      public class NumericTextBox : TextBox
      {
      // ...
      protected override void OnKeyPress(KeyEventArgs e)
      {
      if (e.KeyCode < Keys.D0 || e.KeyCode > D9 ||
      e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
      e.Handled = true;

      base.OnKeyPress(e);
      

      }
      }

      You can do this by overriding ProcessDialogChar or ProcessDialogKey as well.

      Microsoft MVP, Visual C# My Articles

      W 1 Reply Last reply
      0
      • H Heath Stewart

        What can't you do? Please be more specific. A simple example using OnKeyPress would look like:

        public class NumericTextBox : TextBox
        {
        // ...
        protected override void OnKeyPress(KeyEventArgs e)
        {
        if (e.KeyCode < Keys.D0 || e.KeyCode > D9 ||
        e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
        e.Handled = true;

        base.OnKeyPress(e);
        

        }
        }

        You can do this by overriding ProcessDialogChar or ProcessDialogKey as well.

        Microsoft MVP, Visual C# My Articles

        W Offline
        W Offline
        Werdna
        wrote on last edited by
        #3

        The problem with this approach is that it doesn't stop anyone from pasting a string, and this will block Backspace key as well. It will also not accept decimal numbers. I needed a numeric text field control a while ago, and I thought it would be as simple as overriding OnKeyPress, but there is more to it.

        H S 2 Replies Last reply
        0
        • W Werdna

          The problem with this approach is that it doesn't stop anyone from pasting a string, and this will block Backspace key as well. It will also not accept decimal numbers. I needed a numeric text field control a while ago, and I thought it would be as simple as overriding OnKeyPress, but there is more to it.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          You can change the sample to accept backspace, delete, and the decimal point. In order to validate pasted text, override OnTextChanged or OnValidating. It's possible to do what you need, you just have to override the right methods as necessary.

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          • W Werdna

            The problem with this approach is that it doesn't stop anyone from pasting a string, and this will block Backspace key as well. It will also not accept decimal numbers. I needed a numeric text field control a while ago, and I thought it would be as simple as overriding OnKeyPress, but there is more to it.

            S Offline
            S Offline
            Stefan Troschuetz
            wrote on last edited by
            #5

            Try this one: private void textBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { this.validValue = this.Text; } private void textBox_TextChanged(object sender, System.EventArgs e) { try { int.Parse(this.Text); } catch (System.FormatException) { this.Text = this.validValue; } }

            I 1 Reply Last reply
            0
            • S Stefan Troschuetz

              Try this one: private void textBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { this.validValue = this.Text; } private void textBox_TextChanged(object sender, System.EventArgs e) { try { int.Parse(this.Text); } catch (System.FormatException) { this.Text = this.validValue; } }

              I Offline
              I Offline
              indranilbanerjee
              wrote on last edited by
              #6

              Almost... private void textBox_TextChanged(object sender, System.EventArgs e) { try { if(this.Text.Length > 0) int.Parse(this.Text); this.validValue = this.Text } catch (System.FormatException) { this.Text = this.validValue; } }

              S 1 Reply Last reply
              0
              • I indranilbanerjee

                Almost... private void textBox_TextChanged(object sender, System.EventArgs e) { try { if(this.Text.Length > 0) int.Parse(this.Text); this.validValue = this.Text } catch (System.FormatException) { this.Text = this.validValue; } }

                S Offline
                S Offline
                Stefan Troschuetz
                wrote on last edited by
                #7

                Interesting, I will consider this in my application. Never needed to look for an empty string, because all my TextBoxes, i use this for, have default values. But better be prepared for all cases :-) By the way, what happens, if the Parse method is called with a string with Length = 0?

                I 1 Reply Last reply
                0
                • S Stefan Troschuetz

                  Interesting, I will consider this in my application. Never needed to look for an empty string, because all my TextBoxes, i use this for, have default values. But better be prepared for all cases :-) By the way, what happens, if the Parse method is called with a string with Length = 0?

                  I Offline
                  I Offline
                  indranilbanerjee
                  wrote on last edited by
                  #8

                  int.Parse throws a FormatException when given a zero length string so that check is definitely necessary (at least it is for my application)

                  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