?
Solved

Making a textbox only accept numbers and commas

Posted on 2009-05-01
7
Medium Priority
?
354 Views
Last Modified: 2012-05-06
I want to make a textbox only accept numbers and commas. What is the best way to do this?
0
Comment
Question by:bertino12
  • 4
  • 2
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24282950
You could implement a custom TextBox by inheriting the original and adding some custom code (or just use the custom KeyPress code on your existing box).
class NumberTextBox : System.Windows.Forms.TextBox
{
    private readonly List<char> valids = new List<char>() { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',' };
 
    public NumberTextBox() : base()
    {
        this.KeyPress += new KeyPressEventHandler(NumberTextBox_KeyPress);
    }
 
    void NumberTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!valids.Contains(e.KeyChar))
        {
            e.Handled = true;
        }
    }
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24282969
void NumberTextBox_KeyPress() should be implemented as below. Sorry, I posted incorrect code for that.
void NumberTextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    if (!valids.Contains(e.KeyChar))
    {
        e.Handled = true;
    }
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24283011
If you want backspace to be allowed, then change the char list to the following:
private readonly List<char> valids = new List<char>() { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '\b' };

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 24283105
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 24283120
My apologies again, I didn't initially see that it was for VB.
Public Class NumberTextBox
    Inherits System.Windows.Forms.TextBox
    Private ReadOnly valids() As Char = {"0"c, "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, "8"c, "9"c, ","c, ControlChars.Back}
 
    Public Sub New()
        MyBase.New()
    End Sub
 
    Protected Sub NumberTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
 
        If (Not valids.Contains(e.KeyChar)) Then
            e.Handled = True
        End If
    End Sub
 
End Class

Open in new window

0
 

Author Comment

by:bertino12
ID: 24295689
I get the error:
'Contains' is not a member of 'System.Array'.      
0
 

Author Comment

by:bertino12
ID: 24297940
Okay, Just had to change  valids()  to an arraylist so i could use the contains method. Thanks. It seems to be working just fine.
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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

862 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