Advertisement
congdantoancau

Instant alert not number input

Apr 30th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. string invalidChars = ""; // List of invalid characters
  2.         private void txtYear_TextChanged(object sender, EventArgs e)
  3.         {
  4.  
  5.             Control ctr = (Control)sender;
  6.            
  7.             // Show error alert
  8.             if (ctr.Text.Trim().Length > 0 && !char.IsDigit(ctr.Text, ctr.Text.Length - 1)) //Last character is not a digit
  9.             {
  10.                 this.errYear.SetError(txtYear, "Invalid character");
  11.                 // Add invalid character to the list
  12.                 if (!invalidChars.Contains(ctr.Text[ctr.Text.Length - 1]))
  13.                     invalidChars += ctr.Text[ctr.Text.Length - 1];
  14.             }
  15.  
  16.             // Remove the character from list if it was deleted from the textbox
  17.             if (invalidChars.Length > 0)
  18.                 if (!ctr.Text.Contains(invalidChars[invalidChars.Length-1]))
  19.                     invalidChars = invalidChars.Remove(invalidChars.Length - 1);
  20.    
  21.  
  22.             txtResult.Text = invalidChars;
  23.            
  24.             // Remove error alert
  25.             if (invalidChars.Length == 0)
  26.                 this.errYear.Clear();
  27.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement