c# - Delete all non-digits from textbox -
i have textbox1 enter number. example: 123 have textbox2 sum shown. example: 6 (1+2+3)
what need is. if there numbers in textbox1, fine , i'm getting sum. if there more numbers 1a2b3c want programm show message box warning , text. delete non-digits? if guy press yes, delete abc , 123 left. if no, error shows up.
my code:
private void button1_click(object sender, eventargs e) { int cipari = convert.toint32(textbox1.text); int summa = 0; (int n = cipari; n > 0; summa += n % 10, n /= 10) ; dialogresult dialogresult = messagebox.show("delete non-digits?", "warning", messageboxbuttons.yesno); if (dialogresult == dialogresult.yes) { textbox2.text = summa.tostring(); } else if (dialogresult == dialogresult.no) { textbox2.text = "error! can't sum non-digits!"; } }
simply check prescence of non-digit characters:
foreach(char c in textbox1.text) { if( !char.isdigit( c ) ) { messagebox.show("non-digits detected"); return; } }
Comments
Post a Comment