Access to the "Name" property of a non visual component
I'm in the process of building a non-visual component. It works quite well.
When my component is dropped on a form, I see it has a property "(Name)" which I haven't defined. This property is displayed between parenthesis in the IDE properties window while my own defined properties are displayed withow parenthesis.
My question is: How do I access the Name property by code within my component class ?
If I define a Name property, it is displayed as a second one, without parenthesis, in the the property window. If I remove the definition, the compiler complain about unknown symbol when I try to use it.
The (Name) property is required and is put in by default I believe, to make sure that the control has a unique name otherwise the compiler will throw an error saying that 2 components have the same name, which is invalid!
OK, I've found where this property is located.
Actually it is a property of the ISite interface which is available thru Component.Site property.
The Site property has a value only when the component is dropped on a form (owned by a container). So to use that property, you can write the code below:
[Browsable(false)]
public string Name {
get { if (Site == null)
return FName;
else
return Site.Name;
}
set { FName = value;
if (Site != null)
Site.Name = FName;
}
}
protected string FName;
This code define a non browsable property (that is which doesn't appears in the object inspector and is mapped to the Site.Name property when Site is assigned. If not, it is mapped to a protected string member.
This implementation perfectly fit my need: When a component is dropped on a form, it has a value for Site.Name which is available thru the Name property I created. When the component is created at runtime, it has a the property Name which is mapped to the adequate storage.
I could enhance a little bit the implementation by overriding set_Site in order to not loose the Name property value when the component is included within a container. But this is not necessary for any use I have.