how to vertically autosize a mutliline text box

  • Thread starter Thread starter Peted
  • Start date Start date
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
 
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
 
Darn it, sorry - forgot to add, that should be put in the text-box's
on-change event

Kind regards,
Eliott
 
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
 
Back
Top