how to vertically autosize a mutliline text box

P

Peted

Hi
using VS 2008 winforms

If i have a textbox on a winforms

During runtime, i want it to start with a vertical height of 2 lines,
and not use any scrollbars of any type.

As a user types text into the textbox, and either hits return to add a
new line or the textbox wraps text to a new line,

i want the textbox to grow in vertical height 1 line at a time as the
user eneters text

The textbox is located in a cell of a tablelayout panel.

Can any one suggest how i can make the textbox autosize vertically in
the manner i have described. A textbox does not appear to have an
autoresize property


thanks for any help


Peted
 
M

MRe

As a user types text into the textbox, and either hits return to add a
new line or the textbox wraps text to a new line,
i want the textbox to grow in vertical height 1 line at a time as the
user eneters text

Hi Peted,

I'm not sure what the best way to do this might be, but here is a
solution that almost works :p

int Border = 10;
using(Graphics g = Graphics.FromHwnd(textBox1.Handle))
textBox1.Height = (int)g.MeasureString
( textBox1.Text + " ",
textBox1.Font,
textBox1.ClientSize.Width
).Height + Border;

Hope this is of some use,
Kind regards,
Eliott
 
M

MRe

Darn it, sorry - forgot to add, that should be put in the text-box's
on-change event

Kind regards,
Eliott
 
P

Peter Duniho

Hi Peted,

I'm not sure what the best way to do this might be, but here is a
solution that almost works :p

int Border = 10;
using(Graphics g = Graphics.FromHwnd(textBox1.Handle))
textBox1.Height = (int)g.MeasureString
( textBox1.Text + " ",
textBox1.Font,
textBox1.ClientSize.Width
).Height + Border;

I haven't tried it, but probably if you switch the above code to use the
TextRenderer class, rather than the Graphics class, you'll get size
information that fits the TextBox behavior exactly (it uses TextRenderer
to draw the text).

You may have to fiddle with the text rendering options, to match the
settings of the text box. But the basic idea is sound.

Pete
 

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

Top