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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -