Getting the object name via reflection 
Author Message
 Getting the object name via reflection

Hi All,

Does anybody can show me the right direction, how to get the object name, if
the object does not have the name property?

In my case, I want to get the name of the menuitem object. Is it possible at
all?

Thanks in advance

Gabor



Sun, 20 Nov 2005 03:52:34 GMT  
 Getting the object name via reflection
Let's see your code.


Quote:
> Hi All,

> Does anybody can show me the right direction, how to get the object name,
if
> the object does not have the name property?

> In my case, I want to get the name of the menuitem object. Is it possible
at
> all?

> Thanks in advance

> Gabor



Sun, 20 Nov 2005 04:33:45 GMT  
 Getting the object name via reflection
Gabor,
Are you asking if I have the following menu item:

    Friend MenuItem1 As MenuItem

How do I return 'MenuItem1'?

Short answer:
    You cannot.

Long answer:
    You cannot, what are you attempting to achieve?

If you have a single menu handler and you want to know which item was
selected you can use the 'Is' operator:

    Private Sub Menu_Click(ByVal sender As Object, ByVal e As EventArgs)
        If sender Is MenuItem1 Then
            ' Do something for MenuItem1
        End If
    End Sub

Of course if you want to your menu items to have names, you can always
derive a new class from MenuItem and add a name property:

    Public Class NamedMenuItem
        Inherits MenuItem

        Private m_name As String

        Public Property Name() As String
            Get
                Return m_name
            End Get
            Set(ByVal Value As String)
                m_name = Value
            End Set
        End Property

    End Class

Hope this helps
Jay


Quote:
> Hi All,

> Does anybody can show me the right direction, how to get the object name,
if
> the object does not have the name property?

> In my case, I want to get the name of the menuitem object. Is it possible
at
> all?

> Thanks in advance

> Gabor



Sun, 20 Nov 2005 05:59:14 GMT  
 Getting the object name via reflection
Thanks Jay,

I'm using the menu editor component, and want to obtain the international
name from the resource file on the basis of the menu "name".

I workarounded the problem: I send an array of menuitems to the my
intarnationalization component, that read the menu tree, get the appropriate
name from the resource file, that match the menuitem name from the array.
Not the perfect solution.

It would be best, to inherit the menu editor component and add a "Tag"
property like the label control, to hold the menu name, that never changes
despite the menuitem.text changes. I want to save a lot of code, and
therefore I'm using the menueditor component.

I think to complet menu editor component with Tag property is not such
easy - isn't it?

Thanks

Gabor



Quote:
> Gabor,
> Are you asking if I have the following menu item:

>     Friend MenuItem1 As MenuItem

> How do I return 'MenuItem1'?

> Short answer:
>     You cannot.

> Long answer:
>     You cannot, what are you attempting to achieve?

> If you have a single menu handler and you want to know which item was
> selected you can use the 'Is' operator:

>     Private Sub Menu_Click(ByVal sender As Object, ByVal e As EventArgs)
>         If sender Is MenuItem1 Then
>             ' Do something for MenuItem1
>         End If
>     End Sub

> Of course if you want to your menu items to have names, you can always
> derive a new class from MenuItem and add a name property:

>     Public Class NamedMenuItem
>         Inherits MenuItem

>         Private m_name As String

>         Public Property Name() As String
>             Get
>                 Return m_name
>             End Get
>             Set(ByVal Value As String)
>                 m_name = Value
>             End Set
>         End Property

>     End Class

> Hope this helps
> Jay



> > Hi All,

> > Does anybody can show me the right direction, how to get the object
name,
> if
> > the object does not have the name property?

> > In my case, I want to get the name of the menuitem object. Is it
possible
> at
> > all?

> > Thanks in advance

> > Gabor



Sun, 20 Nov 2005 12:54:54 GMT  
 Getting the object name via reflection
Gabor,
Adding the new property to a MenuItem is not that difficult. Just inherit
from MenuItem and add your properties. NOTE: They need to be properties.

The problem I see is getting the Menu Editor component to recognize the new
MenuItems.

If you use the menu editor to create your menu structure, you can then
carefully change the generated code from System.Windows.Forms.MenuItem to
YourProject.YourMenuItem, as long as you do not create new items, you can
then use the properties window to set your custom properties. If you create
new menu items, you need to again change them from
System.Windows.Forms.MenuItem to YourProject.YourMenuItem

For other controls (ToolBar for example) you can shadow the Buttons (or
other child collection) property to replace the collection editor with one
of your own.

Hope this helps
Jay


Quote:
> Thanks Jay,

> I'm using the menu editor component, and want to obtain the international
> name from the resource file on the basis of the menu "name".

> I workarounded the problem: I send an array of menuitems to the my
> intarnationalization component, that read the menu tree, get the
appropriate
> name from the resource file, that match the menuitem name from the array.
> Not the perfect solution.

> It would be best, to inherit the menu editor component and add a "Tag"
> property like the label control, to hold the menu name, that never changes
> despite the menuitem.text changes. I want to save a lot of code, and
> therefore I'm using the menueditor component.

> I think to complet menu editor component with Tag property is not such
> easy - isn't it?

> Thanks

> Gabor


message

> > Gabor,
> > Are you asking if I have the following menu item:

> >     Friend MenuItem1 As MenuItem

> > How do I return 'MenuItem1'?

> > Short answer:
> >     You cannot.

> > Long answer:
> >     You cannot, what are you attempting to achieve?

> > If you have a single menu handler and you want to know which item was
> > selected you can use the 'Is' operator:

> >     Private Sub Menu_Click(ByVal sender As Object, ByVal e As EventArgs)
> >         If sender Is MenuItem1 Then
> >             ' Do something for MenuItem1
> >         End If
> >     End Sub

> > Of course if you want to your menu items to have names, you can always
> > derive a new class from MenuItem and add a name property:

> >     Public Class NamedMenuItem
> >         Inherits MenuItem

> >         Private m_name As String

> >         Public Property Name() As String
> >             Get
> >                 Return m_name
> >             End Get
> >             Set(ByVal Value As String)
> >                 m_name = Value
> >             End Set
> >         End Property

> >     End Class

> > Hope this helps
> > Jay



> > > Hi All,

> > > Does anybody can show me the right direction, how to get the object
> name,
> > if
> > > the object does not have the name property?

> > > In my case, I want to get the name of the menuitem object. Is it
> possible
> > at
> > > all?

> > > Thanks in advance

> > > Gabor



Sun, 20 Nov 2005 22:08:26 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Getting tables name from SQL Server via ADO

2. Trying to Dunamically Load Assembly via Reflection

3. getting document URL from XMLHTTP object via VB Script

4. HELP: Getting table data from EXCEL via Exel OBJECT and then dumping result in Flexgrid

5. Reflection: Getting fields in structure

6. Reflection issue: Getting instances of a Type?

7. Help changing an object's name via a variable's value

8. VB2008: Getting the System.Reflection.PropertyInfo from within a class property

9. (Q) Getting current Sub/Function name (or Object Browser functionality)

10. Getting the name of an object

11. Getting an objects class name

12. Getting the name of an object in a array

 

 
Powered by phpBB® Forum Software