Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

Number only text box

I wish to create a textbox that only accepts numbers and decimals.  What is the best way to design this?  
0
jjacksn
Asked:
jjacksn
1 Solution
 
gena17Commented:
Hi,

You can try NumericUpDown control.

Gena
0
 
kolluCommented:
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

class TextBoxDemo:Form
{
  public TextBoxDemo()
  {
    NumberBox n1=new NumberBox();
    n1.TabIndex=0;
    Button b1=new Button();
    n1.Location=new Point(10,10);
    b1.Location=new Point(n1.Left+n1.Width+20,10);
    b1.Size=new Size(150,24);
    b1.Text="Wanna Give me Focus";    
    this.Controls.Add(n1);
    this.Controls.Add(b1);
  }
  public static void Main()
  {
    Application.Run(new TextBoxDemo());
  }
}

class NumberBox:TextBox
{
  public NumberBox()
  {
    this.CausesValidation=true;
    this.Validating+=new CancelEventHandler(TextBox_Validation);
  }

  private void TextBox_Validation(object sender,CancelEventArgs ce)
  {
    try
    {
      int value=Int32.Parse(this.Text);
    }
    catch(Exception)
    {
      ce.Cancel=true;
      MessageBox.Show("Please Enter Numeric Value");
    }
  }
}



See this
http://www.c-sharpcorner.com/Language/VisualInterfaceInCSPSD.asp
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
laotzi2000Commented:
I guess a Regular Expression Validator is the simplest one
0
 
Mohammed NasmanSoftware DeveloperCommented:
Hello

  Here's a numBox control that inherit from TextBox class, it will allow only numbers, dots and backspace to be used

//======

using System;
      public class NumBox : System.Windows.Forms.TextBox
      {
            protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
            {
                  int asc = Convert.ToInt32(e.KeyChar);

                  if ((asc >= 47 && asc <= 57) )
                  {
                        e.Handled = false;
                        return;
                  }
                  if (asc == 8 || asc ==46)
                  {
                        e.Handled = false;
                        return;
                  }
                  e.Handled = true;
            }
      }
//=======


There's more advanced one in this article
http://www.thecodeproject.com/cs/miscctrl/NumEditBox.asp


HTH

Regards,
Mohammed
0
 
melodiesoflifeCommented:
This is a old question, which I already anwser for another member:
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21261213.html
0
 
jjacksnAuthor Commented:
hehe, that "other member" is me.  just wanted to see if there was a more .NET specific way to do it.  
0
 
jjacksnAuthor Commented:
Mohammed, what about the delete button?
0
 
Mohammed NasmanSoftware DeveloperCommented:
jjacksn
Delete button should work fine without problem, because I use KeyPress Event, I added (ascii = 8) for backspace button, so it should work fine too

0
 
jjacksnAuthor Commented:
Isn't delete a different ascii code than backspace?
0
 
Mohammed NasmanSoftware DeveloperCommented:
yes

KeyPress event use Ascii code for the keys, therefore it can't see the Delete button, because that I add only Backspace(ASCII =8)

if you would like to override the Delete button, you have to use KeyDown or KeyUp and use the Keycode of the keys instead of ASCII

Hope it's more clear now

Regards,
Mohammed
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now