Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Form1
- ' to get this example to work, you will need 5 TextBoxes and one buton (leave with default names) on a form
- ' set the Enable property of the button to False, otherwise you wont see it working
- Public Sub New()
- ' This call is required by the designer.
- InitializeComponent()
- ' set the Tag value for each textbox (this is the power of 2)
- ' it is the mask that is added/subtracted to a total (mask) if there text is text in the textbox or not
- TextBox1.Tag = 1
- TextBox2.Tag = 2
- TextBox3.Tag = 4
- TextBox4.Tag = 8
- TextBox5.Tag = 16
- 'TextBox6.Tag = 32
- 'TextBox7.Tag = 64
- 'etc
- End Sub
- Dim mask As Integer
- Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged, TextBox6.TextChanged
- ' OK this uses bit masking to validate that there is text in the textbox...
- ' it might seem like a lot of touble to go to but it works well.
- ' OK so what we are doing in the next few lines of code is 'Anding' the value of 'mask'
- ' with the tag value of the current textbox
- ' this will tell us if we have previously verified this textbox and confirmed that it contained text
- ' now, (because the user has modified the text in this textbox) we have to verify that it still contains
- ' some text, if not then we subtract the tag value from mask, if it does then we leave the value of mask alone…
- ' example
- 'mask value of 20 and’ed with tag 4 = 4 (we have previously verified textbox 3)
- 'mask value of 20 and’ed with tag 16 = 16 (we have previously verified textbox text 5)
- 'mask value of 20 and’ed with tag 1 = 0 (we have NOT previously verified textbox text 1)
- If (mask And DirectCast(sender, System.Windows.Forms.TextBox).Tag) = DirectCast(sender, System.Windows.Forms.TextBox).Tag Then ' Mask already added
- If DirectCast(sender, System.Windows.Forms.TextBox).Text = String.Empty Then ' if there is no text in this textbox
- mask -= DirectCast(sender, System.Windows.Forms.TextBox).Tag ' subtract the mask
- End If
- ElseIf DirectCast(sender, System.Windows.Forms.TextBox).Text <> String.Empty Then ' mask is not added
- mask += DirectCast(sender, System.Windows.Forms.TextBox).Tag
- End If
- Button1.Enabled = mask = 31 ' for 6 textboxes set this value to 63 for 7 textboxes set it to 127 and so on
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement