Auto Expand Textbox

  • Thread starter Thread starter Thom Little
  • Start date Start date
T

Thom Little

Is there a textbox control to which I can pass a string of an arbitrary
length and it will automatically expand to allow all characters to be
displayed?

I need to have a fixed width and allow the control to add add"lines" with
the wrapped text if needed.
 
Thom said:
Is there a textbox control to which I can pass a string of an arbitrary
length and it will automatically expand to allow all characters to be
displayed?

I need to have a fixed width and allow the control to add add"lines" with
the wrapped text if needed.

If you set the textbox MultiLine property to true, then adding lines to
the Lines property should do what you wish.
 
Is there a textbox control to which I can pass a string of an arbitrary
length and it will automatically expand to allow all characters to be
displayed?

I need to have a fixed width and allow the control to add add"lines" with
the wrapped text if needed.

If you get the Graphics property of the Text Box you can measure your
string and calculate how high the Text Box needs to be, something like:

Graphics gfx = txtPath.CreateGraphics();
string strText = "";
SizeF sf = gfx.MeasureString(strText, txtPath.Font);
int intHeight = (int)(sf.Width / txtPath.Width);
txtPath.Height = intHeight;
gfx.Dispose();

strText would contain the string you want to put in the Text Box - and you
need to check my maths!
 
If you get the Graphics property of the Text Box you can measure your
string and calculate how high the Text Box needs to be, something like:

Graphics gfx = txtPath.CreateGraphics();
string strText = "";
SizeF sf = gfx.MeasureString(strText, txtPath.Font);
int intHeight = (int)(sf.Width / txtPath.Width);
txtPath.Height = intHeight;
gfx.Dispose();

strText would contain the string you want to put in the Text Box - and you
need to check my maths!

Sorry, the height calculation should be:
txtPath.Height = (int)(intHeight * sf.Height);
 
Back
Top