473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Control Property

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.
Nov 15 '05 #1
5 1436
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

"Nicolas" <nl*****@hotmail.com> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
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.

Nov 15 '05 #2
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.
"Santiago" <replytonewsgroup> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

"Nicolas" <nl*****@hotmail.com> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
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.


Nov 15 '05 #3
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.

"Santiago" <replytonewsgroup> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

"Nicolas" <nl*****@hotmail.com> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
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.


Nov 15 '05 #4
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/presentatio...ynamicCode.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

"Nicolas" <nl*****@hotmail.com> wrote in message
news:eJ**************@TK2MSFTNGP11.phx.gbl...
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.

"Santiago" <replytonewsgroup> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

"Nicolas" <nl*****@hotmail.com> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
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.



Nov 15 '05 #5
thank for your help.
I'm already using reflection and I will try to dig in more as per your
comments.
Thanks
"Kenneth Baltrinic" <ke*****@baltrinic.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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/presentatio...ynamicCode.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

"Nicolas" <nl*****@hotmail.com> wrote in message
news:eJ**************@TK2MSFTNGP11.phx.gbl...
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.

"Santiago" <replytonewsgroup> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

"Nicolas" <nl*****@hotmail.com> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
> 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.
>
>



Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
0
by: news.microsoft.com | last post by:
I have a user control with four option buttons depending which option button is pressed I store a score into session object. On the load event of a control the session object is set to 0. There is...
1
by: ANDRES BECERRA | last post by:
Herfried K. Wagner was kind enough to point me to the PropertyGrid control http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformspropertygridclasstopic.asp I have found a few...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
1
by: John Keenan | last post by:
I have a user control with 2 buttons on it & 1 label.... as each button is pressed, they set a member variable within the class and sets the label test. I also have a get/set property for the...
5
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an...
5
by: Richard Brown | last post by:
Ok, I've been looking through the .NET SDK docs and stuff. I'm wondering if you can provide a control extender that does generic validation or functionality just by dropping it on the form. For...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
2
by: Mike | last post by:
Hi, I am strugling with a simple problem which I can't seem to resolve. I have an asp.net page which contains a server-control (flytreeview, which is a kind of a tree to be exact). The tree is...
3
by: shapper | last post by:
Hello, How can I send a control to a class as a property? I don't know which control it would be. It could be a panel, a label .... Thanks, Miguel
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.