?
Solved

Number only text box

Posted on 2005-02-24
11
Medium Priority
?
313 Views
Last Modified: 2008-02-01
I wish to create a textbox that only accepts numbers and decimals.  What is the best way to design this?  
0
Comment
Question by:jjacksn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 8

Expert Comment

by:gena17
ID: 13395080
Hi,

You can try NumericUpDown control.

Gena
0
 
LVL 5

Expert Comment

by:kollu
ID: 13395472
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:laotzi2000
ID: 13397122
I guess a Regular Expression Validator is the simplest one
0
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 2000 total points
ID: 13398567
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
 
LVL 6

Expert Comment

by:melodiesoflife
ID: 13399292
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
 
LVL 5

Author Comment

by:jjacksn
ID: 13425748
hehe, that "other member" is me.  just wanted to see if there was a more .NET specific way to do it.  
0
 
LVL 5

Author Comment

by:jjacksn
ID: 13439721
Mohammed, what about the delete button?
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 13465555
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
 
LVL 5

Author Comment

by:jjacksn
ID: 13466811
Isn't delete a different ascii code than backspace?
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 13469642
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question