1

I'm trying to get a textbox to change its height dynamically while maintainign a fixed width. Cuttently when the text is too long and goes too a second line the textbox does not resize.

The textbox is sized after all text has been added.

Below is what I'm currently using.

txtwfSupportNotes[i].Multiline = true;
txtwfSupportNotes[i].Text += ds.Tables[0].Rows[i]["Notes"].ToString() 
+ "\r\n\r\n";
Size txtSize = TextRenderer.MeasureText(txtwfSupportNotes[i]
.Text, txtwfSupportNotes[i].Font);
txtwfSupportNotes[i].Width = 355;
txtwfSupportNotes[i].Height = txtSize.Height+5;
txtwfSupportNotes[i].BorderStyle = BorderStyle.FixedSingle;

EDIT: Got a fix, see below.

Size txtSize = TextRenderer.MeasureText(txtwfSupportNotes[i].Text, txtwfSupportNotes[i].Font, txtwfSupportNotes[i].ClientRectangle.Size, TextFormatFlags.WordBreak);
txtwfSupportNotes[i].Height = txtSize.Height+5;
Joeeigel
  • 43
  • 6
  • are you calling the above in the `TextChanged` event? Looks fine to me. – James Jun 19 '13 at 09:04
  • The text is only ever set once, and is set before any sizing is done, I'll edit the original post to show an example. – Joeeigel Jun 19 '13 at 09:08
  • I take it you are updating this as part of a loop? You shouldn't have to set the width every time if it's supposed to be fixed. – James Jun 19 '13 at 09:12

4 Answers4

0

I am assuming this is a wpf or winforms application, if that is the case, don't set the height, (but use wrapping,) then your textbox should resize dynamically.

Master117
  • 660
  • 6
  • 21
0

Why don't you use the FontChanged Event of the TextBox? Do it like this:

private void txtwfSupportNotes_FontChanged(object sender, EventArgs e)
    {
        Size txtSize = TextRenderer.MeasureText(txtwfSupportNotes.Text, txtwfSupportNotes.Font);
        txtwfSupportNotes.Height = txtSize.Height + 5;
    }
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
0

If you use MinHeight property, I think it should dynamically rezise your textbox :

txtwfSupportNotes[i].MinHeight = txtSize.Height+5;
Joffrey Kern
  • 6,449
  • 3
  • 25
  • 25
0

Check that the "Locked" property = false on the text box control in the designer?

Aaron
  • 86
  • 1
  • 5