6

i've changed a WinForms TextBox control to have no border.

When i do the bottom pixel row of text in the box is being cut off.

Top: BorderStyle.Fixed3D (default). Bottom: BorderStyle.None

enter image description here

You can see the last bit of text in the un-bordered text box is cut off:

enter image description here

How do i convince a TextBox (who's height cannot be changed), that it needs to be taller?

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • isn't there a autosize property that you could either turn on or off for that.? you should be able to set the width and height as well unless I am deeply mistaken. – MethodMan Dec 12 '11 at 21:49
  • You are, in fact, deeply mistaken. You are thinking of `AutoSize` (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.autosize.aspx). My first thought was that too; toggle it off and on so that .NET gets its head out of its assembly. But `TextBox` has no `AutoSize` property. Also any attempts to adjust the height of a TextBox (e.g. `Height`, `Bounds.Height`, `ClientRectangle`, `ClientSize`) ignore any height changes. The `TextBox` decides it's height for itself and that's that. – Ian Boyd Dec 12 '11 at 21:56
  • Try a loo here [another answer](https://stackoverflow.com/a/46564085/3998265) it is pretty similar – Fabrice T Oct 04 '17 at 12:08

4 Answers4

2

The AutoSize property is there, just inherit from TextBox and you can get to the property:

public class TextBoxEx : TextBox {

  public TextBoxEx() {
    base.AutoSize = false;
  }

}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
0

You can do this in your form:

    private void RefreshHeight(TextBox textbox)
    {
        textbox.Multiline = true;
        Size s = TextRenderer.MeasureText(textbox.Text, textbox.Font, Size.Empty, TextFormatFlags.TextBoxControl);
        textbox.MinimumSize = new Size(0, s.Height + 1);
        textbox.Multiline = false;
    }

Then you say RefreshHeight(textbox1);

Multiline change will force the textbox to "accept" the new size

Fabrice T
  • 648
  • 9
  • 23
0

You can also fix this in the Visual Studio Designer.

Change the height in the minimum size property for the text box. Change to multiline=true, then back to false. The text box will resize and stay fix.

No code changes needed.

JRE
  • 337
  • 3
  • 14
0

This seems to do the trick:

public Form2()
{
    InitializeComponent();
    textBox1.Multiline = true;
    textBox1.MinimumSize = new Size(0, 30);
    textBox1.Size = new Size(textBox1.Size.Width, 30);
    textBox1.Multiline = false;
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188