|
-
Jun 27th, 2006, 01:09 AM
#1
Thread Starter
Hyperactive Member
VB.NET - One routine to handle them all (events)
Ever written about 30 different event handlers that all do roughly the same thing? Not only is it a pain to create it, but it leaves large chunks of asynchronous code where a change in one needs to be copied, and errors are more likely to present themselves as a result.
This code was written in VB.NET 2005 Standard Edition, so no guarentees anywhere else (though I believe it should work anyway in VB.NET).
For this example I am going to parse the input of textboxes 1-10 to make sure that they are all numeric, less than 100, and greater than 0 (such as a percent).
VB Code:
Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged, _
TextBox6.TextChanged, TextBox7.TextChanged, TextBox8.TextChanged, TextBox9.TextChanged, TextBox10.TextChanged
' We can refer to the sender object as a textbox, because we know that the only controls that raise this event are textboxes.
Dim curTxtBox As TextBox = sender
' We could not access sender.Text using Intellisense, but we can through curTxtBox - this can be very useful.
Dim curText As String = curTxtBox.Text
If Not (IsNumeric(curText) And Val(curText) < 100 And Val(curText) > 0) Then
' The complicated line in the middle is a hack to get the number of the textbox
' based on the fact that all the textbox's names start with "TextBox".
MsgBox("This is not a valid percentage in textbox" & _
curTxtBox.Name.Substring("TextBox".Length, curTxtBox.Name.Length - "TextBox".Length) & _
"." & vbNewLine & "Please enter a number between 0 and 100.")
curTxtBox.Focus()
' Since the text is not valid, lets select it all so the user can type away over the top of it.
' Again, this method would not appear using Intellisense, but it will now.
curTxtBox.SelectAll()
Exit Sub
End If
End Sub
That has a few hacks thrown in for good measure, but the important part is the definition of the subroutine.
Note that the name applied is completely arbitrary. It took me a little while to figure out that event handlers are just routines with additional functionality, so I thought I'd pass that on. That this means is you could easy replace my routine name with this:
VB Code:
Private Sub WingDerWongNumber(...)
...
The important part is that you add multiple handlers to the same function.
Also note that I have used _ to break the code into multiple lines at some points. I find this particularly useful when I have a long messagebox or definition to write, and I usually try to group things using this technique.
Hopefully many of you will find this useful at some point. 
Post writing: I decided to compile it to check, and I found 2 typos: a missing 2 and a missing ". Fixed it now, but shows that even when you're paying attention you can make mistakes.
Last edited by kleinma; Jun 27th, 2006 at 03:07 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|