4

For my single line Textbox, I set is Border = None. On doing this, the height turns very small. I can't programamtically set the height of the textbox. If I set any border, then again its fine, but I don't want any border. Even the text is not visible completely - so the font size is already bigger the the textbox height.

I tried creating a custom textbox, and set the Height of it, but it has no effect. How to handle this situation? Any help is highly appreciated.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
Tvd
  • 4,463
  • 18
  • 79
  • 125
  • At the default font size (Microsoft Sans Serif 8.25 pt) this does not happen. However, if you increase the font size, glyphs which go below the baseline by more than a couple pixels get cut off. – Tom Bogle Feb 02 '15 at 15:54

6 Answers6

10

There is a simple way not to create a new class. In Designer.cs file:

this.textBox1.AutoSize = false;
this.textBox1.Size = new System.Drawing.Size(228, 25);

And that's all.

Sereban
  • 101
  • 1
  • 2
  • That's fine, except you ideally don't really want to hard-code the corrected height, since you want it to respond appropriately to changes in the font height (in Designer). A custom control that actually works correctly in Designer would be the ideal, though a bit of a pain. – Tom Bogle Feb 02 '15 at 15:57
7

TextBox derives from Control, which has an AutoSize property, but the designers have hidden the property from the PropertyGrid and Intellisense, but you can still access it:

public class TextBoxWithHeight : TextBox {

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

Rebuild and use.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
3

TextBox controls automatically resize to fit the height of their Font, regardless of the BorderStyle you choose. That's part of the defaults used by Visual Studio.

By changing the Multiline, you can override the Height.

this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 
                                             26.25F, 
                                             System.Drawing.FontStyle.Regular, 
                                             System.Drawing.GraphicsUnit.Point,
                                             ((byte)(0)));
this.textBox1.Location = new System.Drawing.Point(373, 502);
// this is what makes the height 'stick'
this.textBox1.Multiline = true;
// the desired height
this.textBox1.Size = new System.Drawing.Size(100, 60);

Hope this helps.

Gustavo Mori
  • 8,319
  • 3
  • 38
  • 52
  • Thanks Gustavo. MAking multiline True & then setting it height is also a good option. – Tvd Jul 18 '11 at 07:54
2

I just created this case in an empty project and don't see the result you are describing.

When the BorderStyle is none, the display area of the Textbox auto-sizes to the font selected. If I then set Multiline = true, I can change the height portion of the Size property and the change sticks.

Perhaps another portion of your code is modifying the height? A resize event handler perhaps?

My suggestions:

  • Post the relevant portions of your code
  • Try to reproduce the issue in an empty WinForms project (as I just did)
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • I rtried to try it with an empty project and it did work as you said. Actually, the textbox is a custom Textbox, and text is painted at 4th y, that's what was creating the difference. Thanks – Tvd Jul 18 '11 at 07:53
  • 3
    Changing the Multiline property has side effects, such as disabling AutoComplete and wrapping the last word off the line when the text is too long. So setting Multiline to true is unacceptable in many circumstances. Moreover the problem the OP is having DOES happen in the standard TextBox control. To reproduce it: Create a new empty project, add a textbox and set its font to "Microsoft Sans Serif, 9.75pt". Set its text to "good day, joe" and set its BorderStyle to None. You will see that 'g', 'y', and 'j' are all truncated. – drwatsoncode May 24 '13 at 17:54
  • @ricovox: That may be in general, but specific to the question that was asked, this solution seems to be appropriate since the OP accepted it. – Eric J. May 24 '13 at 18:35
  • Upon some further inspection, this seems to be a bug in the way the TextBox control paints its text. If you take a look at the STANDARD TextBox control, with the font size set to 9.0, there are 4 rows of blank pixels above the text, but only one below. You'll notice that the bottom "hook" of the "g" is entirely chopped off. So the text is being painted too low. The best solution was given by Sereban: Set the TextBox's hidden AutoSize property to False. Then you can set the size to whatever you want, without modifying the Multiline property. – drwatsoncode May 24 '13 at 18:51
  • It does _appear_ to be an issue with positioning, but it may not be. Just as some glyphs can have descenders, others rise well above the baseline. I think MS may have actually got the auto-sizing algorithm wrong. At 11.25 pt, a text box with a single-pixel border gets a height of 24. Remove the border, and it goes all the way down to 17. That's a 7 pixel decerease for a 2 (or maybe 4) pixel change! – Tom Bogle Feb 02 '15 at 16:01
0

I find the best solution is to subclass the Textbox and expose the hidden AutoSize there:

public class TextBoxWithHeight : TextBox 
{
    public bool Auto_Size
    {
        get { return this.AutoSize; }
        set { this.AutoSize = value; }
    }
}

Now you can set the Autosize on or off using the object inspector in the visual designer or in code, whatever you prefer.

GuidoG
  • 11,359
  • 6
  • 44
  • 79
-2

Just select your textbox and go to properties then increase your font size.. DONE !!!