Re: dynamic textbox width

S

Scott M.

This is an ASP.NET application? I wouldn't do it the way you are
attempting. Instead, I would determine the size you would like it to be
(based on characters in the box) and then add a CSS attribute to the tag
output to set its width.

tbText.attributes.add("style", "width:" & desiredWith)

No need to mess with the System.Drawing namespace (or even have a reference
to it).


Greg said:
How can I find out what the width of a textbox is in pixels? I want to
resize the listbox based on the text in it. So far I have:
//resize listbox
Font newFont = new Font("Courier New", 10);
newBitmap = new Bitmap(1,1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
g = Graphics.FromImage(newBitmap);
SizeF stringSize = g.MeasureString(tbText.Text, newFont);
int nWidth = (int)stringSize.Width;
int nHeight = (int)stringSize.Height;
int textSize = nHeight * (nWidth /(int)tbText.Width);

The problem is that tbText.Width returns "95%" (since that's what i have
in the HTML), not hte number of pixels. If the window resizes, then the
textbox needs to resize, so I can't explicitely give the textbox a size in
pixels. Any ideas?
 
G

Guest

Scott,
(Yes, this is ASP.NET in C#)
I thought about doing that, but the problem is that if the user resizes their window, I would like to have the textbox width resize with it (by adding width=95% to the asp:textbox tag) so they don't have to scroll hroizontally. The text in the textbox may be very short or very long (multiline), so I want to resize the height of the textbox to enlarge or shorten based on the amount of text.
Either way though, I'd have to use the System.Drawing namespace to get the width of the text - unless you know of a better way(?). But that part is working fine. The problem is that it assumes the text is all on 1 line (even though in reality it is on many lines inside the textbox), so i have to get the width of the textbox so I can use that to find out how many lines of text are in the textbox.

thanks,
Greg
 
S

Scott M.

See Bruce's comments. I agree with him. I don't know what your specific
design specs are, but, in general, I find it better to determine a maximum
width for the web page (usually 770px) and build to that width. Most
clients can display this without having to scroll horizontally and if the
client has a resolution larger, they just get some extra white space on the
right edge (actually, I like to create a 1 row, 1 column, 770px table with
the borders turned off and the table centered on the page - this way, if
there is any extra space, it is divided between the left and right edges of
the screen and the page just looks like it has margins.)


Greg said:
Scott,
(Yes, this is ASP.NET in C#)
I thought about doing that, but the problem is that if the user resizes
their window, I would like to have the textbox width resize with it (by
adding width=95% to the asp:textbox tag) so they don't have to scroll
hroizontally. The text in the textbox may be very short or very long
(multiline), so I want to resize the height of the textbox to enlarge or
shorten based on the amount of text.
Either way though, I'd have to use the System.Drawing namespace to get the
width of the text - unless you know of a better way(?). But that part is
working fine. The problem is that it assumes the text is all on 1 line
(even though in reality it is on many lines inside the textbox), so i have
to get the width of the textbox so I can use that to find out how many lines
of text are in the textbox.
 

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

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top