开发者

How-To set Height of a Textbox?

开发者 https://www.devze.com 2023-03-20 10:29 出处:网络
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

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 highl开发者_Go百科y appreciated.


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.


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.


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.


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)


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消