Advertisement
NAK

VB NET TextBox Validation

NAK
Apr 26th, 2013
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.56 KB | None | 0 0
  1. Public Class Form1
  2.     ' to get this example to work, you will need 5 TextBoxes and one buton (leave with default names) on a form
  3.     ' set the Enable property of the button to False, otherwise you wont see it working
  4.  
  5.     Public Sub New()
  6.  
  7.         ' This call is required by the designer.
  8.         InitializeComponent()
  9.  
  10.         ' set the Tag value for each textbox (this is the power of 2)
  11.         ' it is the mask that is added/subtracted to a total (mask) if there text is text in the textbox or not
  12.         TextBox1.Tag = 1
  13.         TextBox2.Tag = 2
  14.         TextBox3.Tag = 4
  15.         TextBox4.Tag = 8
  16.         TextBox5.Tag = 16
  17.         'TextBox6.Tag = 32
  18.         'TextBox7.Tag = 64
  19.         'etc
  20.  
  21.     End Sub
  22.  
  23.     Dim mask As Integer
  24.  
  25.     Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged, TextBox6.TextChanged
  26.         ' OK this uses bit masking to validate that there is text in the textbox...
  27.         ' it might seem like a lot of touble to go to but it works well.
  28.  
  29. ' OK so what we are doing in the next few lines of code is 'Anding' the value of 'mask'
  30. ' with the tag value of the current textbox
  31. ' this will tell us if we have previously verified this textbox and confirmed that it contained text
  32. ' now, (because the user has modified the text in this textbox) we have to verify that it still contains
  33. ' some text, if not then we subtract the tag value from  mask, if it does then we leave the value of mask  alone…
  34. ' example
  35. 'mask value of 20 and’ed with tag 4 = 4 (we have previously verified textbox 3)
  36. 'mask value of 20 and’ed with tag 16 = 16 (we have previously verified textbox text 5)
  37. 'mask value of 20 and’ed with tag 1 = 0 (we have NOT previously verified textbox text 1)
  38.  
  39.         If (mask And DirectCast(sender, System.Windows.Forms.TextBox).Tag) = DirectCast(sender, System.Windows.Forms.TextBox).Tag Then ' Mask already added
  40.             If DirectCast(sender, System.Windows.Forms.TextBox).Text = String.Empty Then ' if there is no text in this textbox
  41.                 mask -= DirectCast(sender, System.Windows.Forms.TextBox).Tag ' subtract the mask
  42.             End If
  43.         ElseIf DirectCast(sender, System.Windows.Forms.TextBox).Text <> String.Empty Then ' mask is not added
  44.             mask += DirectCast(sender, System.Windows.Forms.TextBox).Tag
  45.         End If
  46.  
  47.         Button1.Enabled = mask = 31 ' for 6 textboxes set this value to 63 for 7 textboxes set it to 127 and so on
  48.     End Sub
  49.  
  50. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement