Post: [C#] Have only digits entered in a textbox
05-10-2013, 04:15 AM #1
Master Ro
I make food
(adsbygoogle = window.adsbygoogle || []).push({}); There is probably some other way to have only digits entered in your textbox, but when I was making something for a project...I didn't feel like looking it up so I came up with another way.

Here is the script:
    public static void CheckIfDigit(TextBox textBox1)[B][FONT=arial narrow]
[/FONT][/B] {
//Store our characters in a list
List<char> textbox_chars = textBox1.Text.ToList();
int cursorpos = 0;


for (int i = 0; i < textbox_chars.Count; ++i)
{
if (!char.IsDigit(textbox_chars[i]))
//If our character is not a digit, remove it from the list
textbox_chars.Remove(textbox_chars[i]);
else
//If it is a digit, save the cursor position
cursorpos = textBox1.SelectionStart;
}


string newtext = null;
foreach (char c in textbox_chars)
//assign characters to newstring
newtext += c;


//set our textbox's text
textBox1.Text = newtext;
//set its cursor position
textBox1.SelectionStart = cursorpos;
}


Smile
(adsbygoogle = window.adsbygoogle || []).push({});

The following user thanked Master Ro for this useful post:

Pichu
05-10-2013, 04:38 AM #2
Pichu
RIP PICHU.
Originally posted by Princess
There is probably some other way to have only digits entered in your textbox, but when I was making something for a project...I didn't feel like looking it up so I came up with another way.

Here is the script:
    public static void CheckIfDigit(TextBox textBox1)[B][FONT=arial narrow]
[/FONT][/B] {
//Store our characters in a list
List<char> textbox_chars = textBox1.Text.ToList();
int cursorpos = 0;


for (int i = 0; i < textbox_chars.Count; ++i)
{
if (!char.IsDigit(textbox_chars[i]))
//If our character is not a digit, remove it from the list
textbox_chars.Remove(textbox_chars[i]);
else
//If it is a digit, save the cursor position
cursorpos = textBox1.SelectionStart;
}


string newtext = null;
foreach (char c in textbox_chars)
//assign characters to newstring
newtext += c;


//set our textbox's text
textBox1.Text = newtext;
//set its cursor position
textBox1.SelectionStart = cursorpos;
}


Smile


    

namespace digitsOnly
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


List<char> validNumbers = ("0123456789").ToList<char>();
private void button1_Click(object sender, EventArgs e)
{
string build = "";
foreach (char a in textBox1.Text)
{
if (validNumbers.Contains(a))
build += a.ToString();
}
textBox1.Text = build;
}
}
}


This is used with a button, you can also use this with an event handler for when text changes in the textbox. Method will be the same.

Here it is applied to the event handler:

            

private void textBox1_TextChanged(object sender, EventArgs e)
{
string build = "";
foreach (char a in textBox1.Text)
{
if (validNumbers.Contains(a))
build += a.ToString();
}
textBox1.Text = build;
}



Last edit, the thing with my code; it allows you to manage the input properties allowed for a textbox.

The only issue I can note with my code is that it takes the um, flashing edit cursor icon, whatever it is called, and puts it back to the beginning if the user enters and invalid digit.
05-12-2013, 08:01 PM #3
Both methods are somewhat wasted, using int.Parse() in a simple Try works fine.

    
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
int result = int.Parse(textBox1.Text);
}
catch
{
textBox1.Text = null; //Can be anything here, such as maybe a messagebox or even making a way to locate the letter and remove it
}
}


It's unfortunate that IsNumeric was not brought over from VB.

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo