tags:

views:

134

answers:

3

I am making a program which dynamically adds objects, such as a button or a checkbox to a form.

For each instance, a handler is added so a certain function is called for the Click event of each object.

Now, when that handler is called, how can I manipulate the object that fired off the Click event? The Sender object is useless here as I cannot change the location, text, parent, nothing at all.

Since the objects are being created dynamically, their instance name is unfortunately always going to be the same, therefore I cannot simply do things like button1.Text = "Button 1".

I really do not want to create a new subroutine for every type since the actions that would be performed are the same...so how can I manipulate these objects?

There are, at last count, 27 different object types which are being manipulated, and which I want to be manipulated by a single sub.

Thanks for the help!

+1  A: 

If you know the type of input that called the handler, then you can use typecasting to solve your issue:

Sub General_OnClick(ByVal sender As Object, ByVal e As EventArgs)
    Dim b As Button = sender
    b.Text = "Hello World!"
End Sub

If you don't, which you don't seem you, you might try casting to Control instead, this may give you enough control, depending on what you need to do. If not, you can always do something like:

Sub General_OnClick(ByVal sender As Object, ByVal e As EventArgs)
    If TypeOf sender Is Button Then
        Dim b As Button = sender
        b.Text = "Hello World!"
    Else If TypeOf sender Is TextBox Then
        Dim tb As TextBox = sender
        tb.Text = "Goodbye cruel world!"
    End If
End Sub

EDIT: Updated to translate into VB.Net

Matthew Scharley
Its VB.net, not c#
Cyclone
Whoops, didn't refresh for a while and didn't see your edit
Cyclone
Thankyou very much Daniel. @Cyclone: It's been years since I used VB.NET, and I'm glad for the fact. The fact of the matter is though that this is the best way of going about it, and noone had answered it this way in VB yet, so I figured an answer in C# was better than no answer at all.
Matthew Scharley
http://www.developerfusion.com/tools/convert/csharp-to-vb/ ;) I personally love VB, so I plan on sticking with it for a while though.
Cyclone
+3  A: 

Cast the sender to Control & you can do what you want (all your objects are Controls right?)

najmeddine
I was trying to cast directly to the type of the object, that didnt work lol. Thanks, I didn't even realize there was a Control type at all lol....
Cyclone
+2  A: 

It sounds like the sender is what you want, as this will be the object that fired off the Click event. You just need to figure out a way to cast it to the required type.

If you are just manipulating location, text and parent, then casting to Control will be enough:

Dim c As Control = CType(sender, Control)

Otherwise you will need to cast to the specific type which means you will need different routines per type.

Another option is to turn on late binding, which I believe in VB is Option Strict Off. Then you can refer to control properties even without the casting -- .NET will look for the property at runtime (and, be warned, will throw an exception if the property's not there).

itowlson
I'd use TryCast() rather than CType, but +1 for casting to control
Joel Coehoorn