Tvd
Tvd

Reputation: 4601

How-To set Height of a Textbox?

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.

Upvotes: 4

Views: 31530

Answers (6)

GuidoG
GuidoG

Reputation: 12059

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.

Upvotes: 0

Jainish Jariwala
Jainish Jariwala

Reputation: 5

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

Upvotes: -2

Sereban
Sereban

Reputation: 101

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.

Upvotes: 10

LarsTech
LarsTech

Reputation: 81675

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.

Upvotes: 7

Gustavo Mori
Gustavo Mori

Reputation: 8386

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.

Upvotes: 3

Eric J.
Eric J.

Reputation: 150208

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)

Upvotes: 2

Related Questions