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 make a Text Box only Numbers

How to make a Text Box only Numbers

Scheduled Pinned Locked Moved C#
csharptutorial
3 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.
  • G Offline
    G Offline
    gr8tushar
    wrote on last edited by
    #1

    Hii I want to make a text box which only accepts numbers. If any other key is pressed then it is discarded. In VB6 it's done in somewhat like this .. on the KeyPress event we check if KeyAscii < '0' or KeyAscii >'9' then KeyAscii = 0 How can we implement this thing in C#

    W J 2 Replies Last reply
    0
    • G gr8tushar

      Hii I want to make a text box which only accepts numbers. If any other key is pressed then it is discarded. In VB6 it's done in somewhat like this .. on the KeyPress event we check if KeyAscii < '0' or KeyAscii >'9' then KeyAscii = 0 How can we implement this thing in C#

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

      You can use KeyPressed event handler: void textbox_KeyPressed(Object o, KeyPressEventArgs e) { if(e.KeyChar < '0' || e.KeyChar > '9') e.Handled=true; }

      1 Reply Last reply
      0
      • G gr8tushar

        Hii I want to make a text box which only accepts numbers. If any other key is pressed then it is discarded. In VB6 it's done in somewhat like this .. on the KeyPress event we check if KeyAscii < '0' or KeyAscii >'9' then KeyAscii = 0 How can we implement this thing in C#

        J Offline
        J Offline
        Judah Gabriel Himango
        wrote on last edited by
        #3

        There are several ways to do this. One way is to do an override of the Form's ProcessDialogKey method:

        	protected override bool ProcessDialogKey(Keys keyData)
        	{
        		if(this.textBox1.Focused)
        		{
        			char c = (char)keyData;
        			if(!char.IsNumber(c))
        				return true;
        		}
        
        		return base.ProcessDialogKey (keyData);
        	}
        

        The graveyards are filled with indispensible men.

        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