Tuesday, March 13, 2012

Setting the width of a TextBox in code

In the Page_Load event, the following does not work
TextBox1.Width = 50
VS.Net gives the message:
Type integer cannot be converted to System.Web.UI.WebControls.Unit
I have also tried 50px and "50px", all of which elicit the same message.
In the properties dialog within VS, it is indicated in the form 50px.

Reading help did not help me in figuring out how to sepcify a System.Web.UI.WebControls.Unit
Thank you.
You need to use the Unit Struct. This goes for all Web Controls:
TextBox1.Width = Unit.Pixels(50)
There are several other static/shared Methods available:
public methodstatic (Shared in Visual Basic)ParseOverloaded. Converts a string to aUnit.
public methodstatic (Shared in Visual Basic)PercentageCreates aUnit of typePercentage from the specified double precision floating point number.
public methodstatic (Shared in Visual Basic)PixelCreates aUnit of typePixel from the specified 32-bit signed integer.
public methodstatic (Shared in Visual Basic)PointCreates aUnit of typePoint from the specified 32-bit signed integer.


Thanks much. If anybody from Microsoft reads this, you really should imporve the help topic on WebControl.Width property at URL:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfSystemWebUIWebControlsWebControlClassWidthTopic.htm
This is pretty much "square 1" information, you can't proceed at all without it; but it is not at all explained or mentioned in that help topic.
Thanks much!
Tom

The URL has nothing to do with MS, it's my fault. This URL is the link to the MSDN help files on my development computer. Try this instead:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsunitclasstopic.asp?frame=true

0 comments:

Post a Comment