Removing All Numbers in the TextBox using C#

July 2, 2020

Today’s tutorial we will create a program in C# wherein if we input a number/s in the textbox, it will remove or clear those numbers if we will click the remove button.

Let’s start this tutorial!

1. Create a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application.

2. Add only one Button named Button1 and name it as “Remove All Numbers” and one TextBox named TextBox1 that will serve as our input. Create your interface like this:


3. Now put this code for your code module. This code is for Button1_Click:

public void Button1_Click(System.Object sender, System.EventArgs e)
    string tmpString = null;
    for (int i = 0; i <= TextBox1.Text.Length - 1; i++)
        if (!Information.IsNumeric(TextBox1.Text.Substring(i, 1)))
            tmpString = tmpString + TextBox1.Text.Substring(i, 1);
    TextBox1.Text = tmpString;


We initialized variable tmpString as an empty string. Afterward, we formulated a For Next loop that variable i as integer will be equal to zero up to the length of the textbox minus 1. Inside the loop, we input an If statement that if the textbox contains a number then it will automatically remove it. The Not keyword is for negation, IsNumeric returns a Boolean value showing whether an expression can be evaluated as a number, Substring – restores the substring of the first argument beginning at the position specified in the second argument and the length specified in the third argument.




Facebook Comments

Leave a Reply

Your email address will not be published.