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;
}
PICHU.
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;
}
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;
}
}
}
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;
}
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
}
}
Copyright © 2026, NextGenUpdate.
All Rights Reserved.