Programming: Visual Basic.NET

Tricks, tips, techniques, and source code for using the (finally!) fully object-oriented version of Visual BASIC! It’s about time that we can finally do dynamic object creation and inheritance with VB! :)

Start New Discussion
8/4/2006 4:55:44 AM
Viewed: 802 times
No Summary
5/6/2006 11:04:05 PM
Viewed: 475 times
No Summary
11/2/2005 8:16:29 AM
Viewed: 1019 times
No Summary
10/21/2005 3:25:16 PM
Viewed: 843 times
No Summary
10/10/2005 3:23:17 PM
Viewed: 922 times
No Summary
9/23/2005 11:17:54 AM
Viewed: 771 times
No Summary
6/6/2005 10:53:07 AM
Viewed: 5952 times

This is a conversion to VB.NET from the message I posted a while back under the C# MessageBase.



Handling events from dynamically created buttons in VB.NET 1.0/1.1:

    In VB.NET, you use a command called AddHandler to define what code to run when the button is clicked. (Internally this involves things called 'delegates', but for VB.NET talk about delegates tends to confuse the issue, so going to steer away from the pure theory of things and just show you how to get things working. After you get things working, then you can look at all the theory.)
    
    Another common confusion is that listed in the properties for a button, there is a 'Click' event defined. DON'T USE IT. Not that you can't get it to work, but there's a lot better event to use, called 'Command' which works just like 'Click', but also allows sending additional information in the CommandArgument property. Without it, it's all but impossible to determine which button triggered the event.
    
    The Command event only exists for  the (you guessed it) Button, LinkButton, and ImageButton objects. This event is called Command. It fires the same as the Click event, but allows you to pass additional data via the Button.CommandArgument property. (The normal Click event does not support sending this additional data, thus why you should use the Command event instead of the Click event, unless you just need to see if a button was clicked and do not need any additional data about the click.)
    
    All you really need to do is create a sub that requires two objects sent to it when invoked. The first is the 'sender' object, which identifies which object is calling it (The button itself) and another object called 'CommandEventArgs'. So create a method such as:

Public Sub ButtonHandler(ByVal sender As Object, ByVal e As WebControls.CommandEventArgs)
    ' Do something...
End Sub


Now, the next thing to do is add the handler for the button to the button.command event. Say we have a button called newButton. We attach the ButtonHandler method to the Command event of newButton with the following code:

AddHandler newButton.Command, AddressOf ButtonHandler

Then, with any additional data you need to send to the ButtonHandler method, you add it to the newButton.CommandArgument property and it will be sent as part of the CommandEventArgs object that is passed via the delegate. CommandArgument only accepts a string value, so any numerical data to be sent needs to be converted to a string first. In the code example at the end of this article, I am sending both the X and Y position of the table separated by a colon. Since it's a single string, formatting of that string is up to you.
5/27/2005 11:03:13 AM
Viewed: 594 times
No Summary
5/14/2005 10:27:32 PM
Viewed: 642 times
No Summary
5/4/2005 2:22:13 PM
Viewed: 704 times
No Summary
3/9/2005 4:45:23 PM
Viewed: 848 times
No Summary
12/20/2004 4:28:50 PM
Viewed: 647 times
No Summary
12/20/2004 10:16:25 AM
Viewed: 12987 times
No Summary
12/10/2004 3:19:31 PM
Viewed: 923 times
No Summary
12/9/2004 10:53:29 PM
Viewed: 1165 times
No Summary