Name and Tag properties for Controls

D

Dennis

It seems Microsoft left out the Name and Tag properties
for of the Control class in the Compact Framework. I am
finding it difficult to work around this because there
seems to be no way to identify controls when iterating
through the controls collection of a form. I could use
the Text property but since that is a display property it
might be necessarily not unique. Often times I would
need to change the Text property for form customization
purposes. It seems too difficult to create new special
versions of all common controls to include a Name or Tag
property. There is also the problem of design time
versions of the custom controls. I believe this omission
is short sighted. Does anyone have a reasonable
workaround?
 
A

Alex Feinman [MVP]

There are several workarounds. Basically what you've been doing before was
trying to identify a control by a text string attached to it. This is not
really a good approach if you think of. The only reason to use it was the
lack of other means. In .NET languages you can simply compare objects:
TextBox tbUserName = new TextBox();
......
foreach( Control c in this.Controls )
{
if ( c == tbUserName )
// Do something here
}

If you absolutely insist on having text strings (or object tags) associated
with the controls, you can use Hashtable:

Hashtable controlTags = new Hashtable();
controlTags[tbUserName] = "User name";

foreach( Control c in this.Controls )
{
string tag = controlTags[c];
if ( s == "User Name" )
// Do something here
}
 
D

Dennis

Alex, Thank you for your response.
You might not like my approach but I am customizing forms
for different locations (users) by using a data table
that contains the form name, control name and
customization information (visible, enabled, text
etc...).
This is why I needed the textual identifier. How would
you identify an object with a string that contains the
objects "Name".
Petzold does this in "Programming Microsoft Windows with
Visual Basic .NET" page 579. The porpose might be
internationalization, customization or run-time
modification of control properties with some kind of
generic function defined outside of the given form class
and passed a reference to that form.
Your HashTable method looks good. I will try that in
VB.net and see if that solution fits my porposes.
Once more Thank you for your time.
Dennis
-----Original Message-----
There are several workarounds. Basically what you've been doing before was
trying to identify a control by a text string attached to it. This is not
really a good approach if you think of. The only reason to use it was the
lack of other means. In .NET languages you can simply compare objects:
TextBox tbUserName = new TextBox();
......
foreach( Control c in this.Controls )
{
if ( c == tbUserName )
// Do something here
}

If you absolutely insist on having text strings (or object tags) associated
with the controls, you can use Hashtable:

Hashtable controlTags = new Hashtable();
controlTags[tbUserName] = "User name";

foreach( Control c in this.Controls )
{
string tag = controlTags[c];
if ( s == "User Name" )
// Do something here
}


Dennis said:
It seems Microsoft left out the Name and Tag properties
for of the Control class in the Compact Framework. I am
finding it difficult to work around this because there
seems to be no way to identify controls when iterating
through the controls collection of a form. I could use
the Text property but since that is a display property it
might be necessarily not unique. Often times I would
need to change the Text property for form customization
purposes. It seems too difficult to create new special
versions of all common controls to include a Name or Tag
property. There is also the problem of design time
versions of the custom controls. I believe this omission
is short sighted. Does anyone have a reasonable
workaround?


.
 

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