How can I get the instance name of a component?

E

Eric

I have a custom component that can be dragged from the ToolBox onto a
Windows Form (which means I can't modify it's constructor to add a name
parameter). VS assigns it an instance name, but the developer can
modify it's name. The name shows up in the property window as "(name)",
I want to find a way to access that name programatically, so that
instance of the control wil be able to determine it's own name. I have
the source for the control and I can modify it, but I really don't want
to add a new "name" property and make the user enter the same name
again. The user already entered it once, and I want to find a way to
get that. Does anyone have any ideas?
 
E

Eric

There is no name property, which is why the property inspector shows
paraenthesis around it: "(name)". It seems that neither Component nor
IComponent define a Name property. If I examine this.ToString() it
shows me the name of the class, and not the name of the instance. I'm
guessing the actual instance name may be a container matter, and not
something that is stored with the component instance?
 
G

Guest

Hello,

This post doesn't appear to have a suggested solution. I would like to
accomplish the same.

Does anyone have any suggestions?
 
N

Nicholas Paldino [.NET/C# MVP]

HairlipDog58,

You aren't really going to be able to find that out. If by instance
name, you mean the name of the variable, since multiple variables can
reference the same object, you could end up with more than one.

What are you trying to accomplish by doing this?
 
G

Guest

Hi Nicholas,

Components can be sited on a form or created programmatically. When sited on
a form, in design mode, the component can be configured through the VS.NET
Properties Grid. In the property grid for the component, there is a property
called (Name), which is the component's programmatic name. This is the
property that I would like access to.

The designer automatically assigns a name to the component when it creates
it. This name shows up in the property grid as '(Name)'. Since '(Name)' is
not accessible programmatically (that I know of), I've implemented a property
called Label to identify the component at runtime.

When the component is created, I would like the Label property to default to
the same value as the (Name) property.



Nicholas Paldino said:
HairlipDog58,

You aren't really going to be able to find that out. If by instance
name, you mean the name of the variable, since multiple variables can
reference the same object, you could end up with more than one.

What are you trying to accomplish by doing this?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

HairlipDog58 said:
Hello,

This post doesn't appear to have a suggested solution. I would like to
accomplish the same.

Does anyone have any suggestions?
 
N

Nicholas Paldino [.NET/C# MVP]

That property is not a property of the actual component, but the name
that the property designer uses for the name in the class.

If you want this, you could enumerate through the fields of the class
that it is contained in (using Reflection), and then check the type to see
if it has a Label property. If it does, then you can set it.

You might want to change your Label property to Text, as it seems more
in line with established convention (or override the Text property if there
is one).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

HairlipDog58 said:
Hi Nicholas,

Components can be sited on a form or created programmatically. When sited
on
a form, in design mode, the component can be configured through the VS.NET
Properties Grid. In the property grid for the component, there is a
property
called (Name), which is the component's programmatic name. This is the
property that I would like access to.

The designer automatically assigns a name to the component when it creates
it. This name shows up in the property grid as '(Name)'. Since '(Name)' is
not accessible programmatically (that I know of), I've implemented a
property
called Label to identify the component at runtime.

When the component is created, I would like the Label property to default
to
the same value as the (Name) property.



Nicholas Paldino said:
HairlipDog58,

You aren't really going to be able to find that out. If by instance
name, you mean the name of the variable, since multiple variables can
reference the same object, you could end up with more than one.

What are you trying to accomplish by doing this?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

HairlipDog58 said:
Hello,

This post doesn't appear to have a suggested solution. I would like to
accomplish the same.

Does anyone have any suggestions?
 
G

Guest

Nicholas,

I already have access to the Label property, it is a property of my
component. When the component instance is created, I want to assign the Label
property a default value that is the same value as 'the name that the
property designer uses for the name in the class'.

Nicholas Paldino said:
That property is not a property of the actual component, but the name
that the property designer uses for the name in the class.

If you want this, you could enumerate through the fields of the class
that it is contained in (using Reflection), and then check the type to see
if it has a Label property. If it does, then you can set it.

You might want to change your Label property to Text, as it seems more
in line with established convention (or override the Text property if there
is one).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

HairlipDog58 said:
Hi Nicholas,

Components can be sited on a form or created programmatically. When sited
on
a form, in design mode, the component can be configured through the VS.NET
Properties Grid. In the property grid for the component, there is a
property
called (Name), which is the component's programmatic name. This is the
property that I would like access to.

The designer automatically assigns a name to the component when it creates
it. This name shows up in the property grid as '(Name)'. Since '(Name)' is
not accessible programmatically (that I know of), I've implemented a
property
called Label to identify the component at runtime.

When the component is created, I would like the Label property to default
to
the same value as the (Name) property.



Nicholas Paldino said:
HairlipDog58,

You aren't really going to be able to find that out. If by instance
name, you mean the name of the variable, since multiple variables can
reference the same object, you could end up with more than one.

What are you trying to accomplish by doing this?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello,

This post doesn't appear to have a suggested solution. I would like to
accomplish the same.

Does anyone have any suggestions?
 
E

Eric

I ended up storing the name in the Tag property. Not ideal, but it was
good enough for me.

I noticed that VS 2005 has fixed this matter. They now seem to be
storing real names with the control instances. At least they do for
many of them - I'm not sure if this applies to all of them. Look in the
code the form designer creates for you - that's where they set it now.

Eric
 
G

Guest

Not sure if we're talking about the same thing here.

I'm interested in getting the name assigned by the designer to the component
instance. How did you get the component's name to store in the Tag property?
 
E

Eric

The VS 2005 designer populates the Name property, so it should be easy
with that.

The Tag property just holds any object you want to associate with
controls. You can put anything there, and it follows the instance.

In VS 2003, you drop the control down (I was using menu items), and
then you see the name in the Property window. Scroll down to find the
Tag property in the Property window and type in the name there (or copy
and paste). Once you do this, the name will be available in the
instance by reading the Tag property. Since Tag holds an object you'll
have to cast it to a string to read it.
 
G

Guest

I'm pretty sure we're not talking about the same thing here.

The original posting was 'How can I get the instance name of a component?',
which is what I am interested in.

I think you are referring to the Control class, which does have an
accessible Name property, as oppposed to the Component class which does not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top