Control Property

  • Thread starter Thread starter Nicolas
  • Start date Start date
N

Nicolas

How can I simply do this.

string tAlign;
tAlign="Left";
TextBox textbox1 = new TextBox();

textbox1.TextAlign = tAlign;


I know that it shoul be like "textbox1.TextAlign =
HorizontalAlignment.Left;" but I want to replace this by a variable so I
dont have to go throught a lot of case statement and also using the same
technic to other property like BorderStyle, FlatStyle etc. in the Appearance
of a control

Thank for the help.
 
Nicolas,
You can cast an integer to the enum. Here's an example:
int tAlign;
tAlign = 1; //Trick here is to figure out what integer value corresponds
to what enum
..
..
..
textbox1.TextAlign = (HorizontalAlignement)tAlign.

- Santiago
 
The problem is that I got the value for tAlign from the registry where I
save the property before and it is saved as text for this sample.
 
The main problem is that How do I cast something like this without going
over thousands of case statement

string registryVal ;
string tAlign;
registryVal= got ValueFromRegistry(ControlName);
tAlign = got ValueFromRegistry(registryVal);

// (HorizontalAlignement)tAlign
// But I dont know if this will be HorizontalAlignement or ContentAlignment
or BorderStyle, FlatStyle etc...

control.GetType().GetProperty(registryVal.Trim()).SetValue(control,tAlign,
null);

Thanks.
 
What are you trying to do is establish a relationship between a string
"Left" and a constant Left whose label just happens to have the same
sequence of characters as your string. No such relationship exists at the
level of the CRL or the compiler. So traditionally speaking, you are out of
luck.

HOWEVER, .Net does provide you with at least three ways to cheat tradition
and get what you are looking for, though the effort may be more than you had
in mind. I would not recommend any of these methods as a best practice but
you could try following: (See my recommendations at the end.)

1) Use reflection to interrogate the enumeration and find the value of the
enumeration member whose name matches your string. For more information on
using reflection look up the System.Reflection namespace.

2) Use the CRLs ability to execute code contained in a string to evaluate
string.Format("HorizontalAlignment.{0}", tAlign) for its value. Here is an
article on how to do that:
http://www.west-wind.com/presentations/dynamicCode/DynamicCode.htm

3) Lastly, it just so happens that when you serialize an enumerated value to
XML using the XmlSerializer object, it does the work in 1) above for you.
For instance if you declared a variable of type HorizontalAlignment as
follows: HorizontalAlignment MyVar = Left and then serialized MyVar to xml
you would get something like "<MyVar>Left</MyVar>" I never tried to
serialize a single value before and my guess is the XML would be lightly
more complex that my off-the-cuff example but you should get the general
idea. Once you serialized such an XML string and see what the format is,
you should be able to construct one containing a any appropriate enumeration
member name and then deserialize it to get the members value.

So what do I recommend?

1) Store your value as a number and avoid the whole issue.
2) If you must store it in a human readable form, do one of the following:
a) Use a case statement to do your conversion.
b) Store your data in an XML file rather than the registry. Then you
can automatically have the advantage of 3) above. You never have to see the
string representation of your data. The XmlSerializer will take an
enumerated value from you and give you one back (upon serialization and
deserialization respectively). All you ever work with is the enumerated
data type.

--Ken
 
thank for your help.
I'm already using reflection and I will try to dig in more as per your
comments.
Thanks
 
Back
Top