Removing All Numbers
Removing All Numbers in the TextBox using C#
Outline: Removing All Numbers
Numbers play a vital role in text boxes. While writing, it’s helpful to include numbers in your text. However, numbers make writing tedious and difficult. That’s because they take up space in text boxes and make it hard to read. Since numbers are difficult to read, it’s better to get rid of them.
Guide: Removing All Numbers
In this post, we will explore how to create a program that clears number(s) inputted in a textbox when the remove button is clicked using C# programming language.
1 – We proceed by creating a Windows Form Application in C# using Microsoft Visual Studio by following the listed steps accordingly;
– Navigate to File
– Click on New Project
– Select Windows Application.
2 – Insert a Button named Button1 and labelled it as “Remove All Numbers”
– Add a TextBox that will serve as the input.
Design the interface like the image below.
3 – Insert the below code module 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 instated the variable tmpString as an empty string. Next, we planned a For Next loop that variable i as an integer will be equivalent to zero up to the length of the textbox less than 1. Inside the loop, we input an If statement that on the off chance that the textbox contains a number, it will consequently eliminate it. The Not keyword is for negation, IsNumeric returns a Boolean value showing whether an expression can be assessed as a number, Substring – reestablishes the substring of the principal argument starting at the position determined in the subsequent argument and the length indicated in the third argument.