|
Development | .NET Compact Framework
So you want to have a Control with a .Name property?
Written by W.G. Ryan
[author's bio]
[read 16423 times]
Edited by Derek
Page 1
Creating Controls with a Name
One common problem that
many
Compact Framework developers face is getting the
name of a given control. While this is a pretty simple thing
to do on the Full Framework (ie Control.Name), it's not
that simple on the Compact Framework.
Chris Tacke presented two brilliant solution
to this problem using
Reflection. Here's one of
them and here's the other. One problem that's presented itself with
that approach is that occassionally a "_" character appends
itself at the beginning of the control name when the code
is compiled into a code library. This is an absolutely trivial
issue but I've seen a few people have problems with this.
Personally, I'm partial to Chris' method, but I came
up with this just for the sake of illustration. Anyway,
there's another track you can take to handle this issue.
Depending on your perspective it can be either a good OOP
example or a second rate hack. Eitherway, it's as simple
as can be. Since the compact framework does support inheritance
you can simply subclass the control (or controls) and give
them a .Name property. Since all Controls have a .Name property
in the Full Framework, you'd expect that you'd have to override
the base class' implementation. However that's not the case
b/c it's not implemented in the CF. Anyway, all you need
to do is create a subclass and give it a .Name property
(or something like .Name so that it won't end up conflicting
with a later implementation).
Here's all there is
to it:
using System;
using System.Data;
using System.Windows.Forms;
namespace Devbuzz.ControlLib
{
///<summary>
///< Summary description for Class1.
///</summary>
publicclass NewTextBox: System.Windows.Forms.TextBox
{
privatestring _ControlName;
public string Name
{
get{ return _ControlName;}
set{ _ControlName = value;}
}
public NewTextBox(){}
public NewTextBox(string name)
{
this.Name = name;
}
}
}
Now this is just the
implementation that I decided to use. You could change
the Accessors however you wanted and you could even combine
Chris's Reflection example inside this class so that you
can reference .Name directly. With this implementation,
you simple add a reference to the project, use the correct
imports/using statement for brevity and then just declare,
instantiate and set property:
NewTextBox myTextBox
= new NewTextBox("controlname");
Back to .NET Compact Framework | [Article Index]
|