UserControl name

G

Guest

I am creating a UserControl in VS.NET 2003 (VB.NET). I would like my control
to display its name at design time when it is dropped onto a form, similar to
how a TextBox displays "TextBox1".

I tried doing this inside the UserControl's constructor after
InitializeComponent() by reading Me.Name, but it only gets the classname
"MyUserControl" not "MyUserControl1" or "MyUserControl2", etc. At what point
(event, override, etc) should I read Me.Name or should I read a different
property? I looked around for another suitably-named override or event like
OnRename or Renamed or OnNameChanged etc...

Thanks for the help!
 
D

Dmytro Lapshyn [MVP]

Hi David,

As far as I remember, the Name property you see in the designer is not a
property of the control itself - it is somehow being added by the designer
hosting your control.

You can find more info in these MSDN Magazine articles:

http://msdn.microsoft.com/msdnmag/issues/03/05/Design-TimeControls/ (Part
2, search for "Design Time-only Properties")
http://msdn.microsoft.com/msdnmag/issues/03/04/Design-TimeControls/ (Part
1 of the article above)

http://msdn.microsoft.com/msdnmag/issues/06/03/DesignerHosting/default.aspx
(.NET 2.0 only)
http://msdn.microsoft.com/msdnmag/issues/04/12/CustomFormsDesigner/default.aspx
 
G

Guest

Thanks Dmytro. Just as a follow-up, here's how I ended up solving this
problem:

It seems that the "Text" property is the one that the Visual Studio .NET
designer saves this value to. For instance, when you drop "MyUserControl"
from the toolbox onto the form, the Text property is set to the value
"MyUserControl1" (etc...).

So, in my WindowsForms UserControl, I had to override the "Text" property,
which is set by Visual Studio .NET to the correct value of "MyUserControl1",
and as part of the "setter" for the property I display that on my
UserControl. So, even though my UserControl doesn't actually have anything
that corresponds to "Text" (I actually only had a "Value" property instead),
overriding the "Text" property allows me to easily display the control's
default name so it mimics TextBox-like behavior.

- David McClelland
 

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