I have textbox1 where i enter number. Example: 123 I also have textbox2 where the sum is shown. Example: 6 (1+2+3)
What i need is. If there is only numbers in my textbox1, then everything is fine and i'm getting sum. If there is something more than numbers like 1a2b3c i want the programm to show message box with Warning and a text. Delete all non-digits? If the guy press Yes, then it does delete abc and only 123 is left. If no, then Error shows up.
My code:
private void button1_Click(object sender, EventArgs e)
{
int cipari = Convert.ToInt32(textBox1.Text);
int summa = 0;
for (int n = cipari; n > 0; summa += n % 10, n /= 10) ;
DialogResult dialogResult = MessageBox.Show("Delete all non-digits?", "Warning", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
textBox2.Text = summa.ToString();
}
else if (dialogResult == DialogResult.No)
{
textBox2.Text = "Error! You can't sum non-digits!";
}
}