get object's name
-
In VB2005, most of the events pass the sender object as a parameter through the vent itself:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Is there any way to get the object's name (not the type) of the sender? .ToString returns its type and I'm looking for a way to get "Button1". I want to handle the xxxx_Click event for several buttons in the same event and without adding a new parameter with an id or something like that. Thanks in advance, Marc Soleda... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits
-
In VB2005, most of the events pass the sender object as a parameter through the vent itself:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Is there any way to get the object's name (not the type) of the sender? .ToString returns its type and I'm looking for a way to get "Button1". I want to handle the xxxx_Click event for several buttons in the same event and without adding a new parameter with an id or something like that. Thanks in advance, Marc Soleda... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits
you can use : if ctype(sender,button).name="Button1" then 'do a task end if
-
you can use : if ctype(sender,button).name="Button1" then 'do a task end if
Or better yet, cast to Control, which has that property and is more 'guarenteed'.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
-
you can use : if ctype(sender,button).name="Button1" then 'do a task end if
Ok, that's right for a control but, if it isn't, and it's an object instanciated from a class library that after doing this cast doesn't have the Name property, how could I know it? Thanks, Marc Soleda
... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits
-
Ok, that's right for a control but, if it isn't, and it's an object instanciated from a class library that after doing this cast doesn't have the Name property, how could I know it? Thanks, Marc Soleda
... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits
I think you know that the answer to your question is NO, not by the way already mentioned; however, what I understood you are saying is, in general, all objects will have some name, how can you get access to that name? If that is the question, then I think that the only way is to check for the object type and access its name by the method appropriate to that object type. for e.g., maybe you need to use reflection to get the class name OR you can get the type name which should be the class name - I am not sure. Just my thoughts.
Shreekar
-
Ok, that's right for a control but, if it isn't, and it's an object instanciated from a class library that after doing this cast doesn't have the Name property, how could I know it? Thanks, Marc Soleda
... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits
Try If sender.name="A Name" Then 'Do A Task End If Catch ex As Exception MsgBox("The object dosn't support name property") End Try
-
I think you know that the answer to your question is NO, not by the way already mentioned; however, what I understood you are saying is, in general, all objects will have some name, how can you get access to that name? If that is the question, then I think that the only way is to check for the object type and access its name by the method appropriate to that object type. for e.g., maybe you need to use reflection to get the class name OR you can get the type name which should be the class name - I am not sure. Just my thoughts.
Shreekar
Using reflection I just can get the class name/type. What I'm looking for is its name that, even it's not a control, I have instanciated an object as a class type and with a name. Isn't there any way to retrieve it?
... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits
-
In VB2005, most of the events pass the sender object as a parameter through the vent itself:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Is there any way to get the object's name (not the type) of the sender? .ToString returns its type and I'm looking for a way to get "Button1". I want to handle the xxxx_Click event for several buttons in the same event and without adding a new parameter with an id or something like that. Thanks in advance, Marc Soleda... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits
You could also try:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Ctrl As Control For Each Ctrl In Me.Controls If Ctrl.GetType Is sender.GetType Then MsgBox(Ctrl.Name.ToString) Next End Sub
As a small concern, I would like to ask you this, are you sure that you are not trying to take efficient coding a bit too far? JohanMy advice is free, and you may get what you paid for.
-
You could also try:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Ctrl As Control For Each Ctrl In Me.Controls If Ctrl.GetType Is sender.GetType Then MsgBox(Ctrl.Name.ToString) Next End Sub
As a small concern, I would like to ask you this, are you sure that you are not trying to take efficient coding a bit too far? JohanMy advice is free, and you may get what you paid for.
Johan Hakkesteegt wrote:
As a small concern, I would like to ask you this, are you sure that you are not trying to take efficient coding a bit too far?
In this context maybe, but I want to export this to a class library made by me and it would help a lot if we could handle some events in this way. Thanks, Marc Soleda
... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits
-
Using reflection I just can get the class name/type. What I'm looking for is its name that, even it's not a control, I have instanciated an object as a class type and with a name. Isn't there any way to retrieve it?
... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits
Marc Soleda wrote:
Using reflection I just can get the class name/type
No, using reflection you can find properties and you can get their values.
Marc Soleda wrote:
I have instanciated an object as a class type and with a name.
If you mean the variable name, you can't get it. If you mean a property on the control, you can get it with reflection, but if you don't know what the variable name is, then you're going to have a hard time. An event handler is always going to have a control come in, I assume you mean the question more generally. It's unusual for your code to take an object and for you to have *no* idea what the object will be.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
-
Ok, that's right for a control but, if it isn't, and it's an object instanciated from a class library that after doing this cast doesn't have the Name property, how could I know it? Thanks, Marc Soleda
... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits
Marc Soleda wrote:
Ok, that's right for a control but, if it isn't, and it's an object instanciated from a class library that after doing this cast doesn't have the Name property, how could I know it?
You can't, as it doesn't have a name. What you are looking for is the name of the variable the holds the reference to the object. The problem is that there can be more than one variable referencing the object, or if there is a single variable, that variable might not have a name either, e.g. if it's part of an array.
--- Year happy = new Year(2007);
-
Marc Soleda wrote:
Using reflection I just can get the class name/type
No, using reflection you can find properties and you can get their values.
Marc Soleda wrote:
I have instanciated an object as a class type and with a name.
If you mean the variable name, you can't get it. If you mean a property on the control, you can get it with reflection, but if you don't know what the variable name is, then you're going to have a hard time. An event handler is always going to have a control come in, I assume you mean the question more generally. It's unusual for your code to take an object and for you to have *no* idea what the object will be.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
Christian Graus wrote:
An event handler is always going to have a control come in...
Not always if it's an object's class - not a control - that raises an event when a something happens: I file has been received, ...
Christian Graus wrote:
It's unusual for your code to take an object and for you to have *no* idea what the object will be.
Unusual maybe but If I handle an event from several objects in only one eventhandler, then I need to differenciate between them to know which one has been. At last, I've solved by using Object.ReferenceEquals that determines if two objects are the same instance. Thanks, Marc Soleda
... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits
-
Marc Soleda wrote:
Ok, that's right for a control but, if it isn't, and it's an object instanciated from a class library that after doing this cast doesn't have the Name property, how could I know it?
You can't, as it doesn't have a name. What you are looking for is the name of the variable the holds the reference to the object. The problem is that there can be more than one variable referencing the object, or if there is a single variable, that variable might not have a name either, e.g. if it's part of an array.
--- Year happy = new Year(2007);
Guffa wrote:
You can't, as it doesn't have a name.
That's the key! It doesn't have a name but I can compare two objects to know if they are the same instance that, for my purposes, is what I need: Object.ReferenceEquals Thanks, Marc Soleda
... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits