As other’s have mentioned, this isn’t very clear. Although I think I understand your issue, I can’t relate it to the context without an example of what you’ve tried already.
Anyway, you can use regex for this.
using System.Text.RegularExpressions;
Then, assuming you’ve stored the input as a variable, let’s say “userInput”
Convert.ToInt32(Regex.Replace(userInput, "[^0-9]+", string.Empty));
This will return a integer of the extracted numbers. I’ll update my answer once you’ve updated your question.
EDIT: Literally as soon as I posted this you updated it. My above code will still work, if you assign textBox1.Text to a variable.
private void button1_MouseClick(object sender, MouseEventArgs e)
{
string userInput = textBox1.Text;
int sum = 0;
if (IsAllLetters(userInput) == true)
{
for (int i = 0; i < userInput.Length; i++)
{
sum += (int)char.GetNumericValue(userInput[i]);
}
label2.Text = "Summa: " + sum;
}
else
{
DialogResult dialogResult = MessageBox.Show("Delete all letters??", "Alert", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
userInput = Regex.Replace(userInput, "[^0-9]+", string.Empty);
for (int i = 0; i < userInput.Length; i++)
{
sum += (int)char.GetNumericValue(userInput[i]);
}
label2.Text = "Summa: " + sum;
}
else if (dialogResult == DialogResult.No)
{
//do something else
}
}
}
public static bool IsAllLetters(string s)
{
foreach (char c in s)
{
if (Char.IsLetter(c))
return false;
}
return true;
}
0
solved c# Windows Forms Application clear only text