Change the Textbox height?

后端 未结 21 2197
心在旅途
心在旅途 2020-12-09 07:24

How do I change the height of a textbox ?

Neither of the below work:

this.TextBox1.Size = new System.Drawing.Size(173, 100);
         


        
相关标签:
21条回答
  • 2020-12-09 08:00

    You can set the MinimumSize and/or the MaximumSize properties of the textbox. This does not affect the size immediately, but when you resize the textbox in the forms designer, the size will automatically be adjusted to satisfy the minimum/maximum size constraints. This works even when Multiline is set to false and does not depend on the font size.

    0 讨论(0)
  • 2020-12-09 08:02

    Steps:

    1. Set the textboxes to multiline
    2. Change the height
    3. Change the font size. (so it fit into the big textboxes)
    4. Set the textboxes back to non-multiline
    0 讨论(0)
  • 2020-12-09 08:02

    the Simplest Way to do that

    1.. right click on the textbox

    2.. go to property

    set Multiline =True

    now done it will Automatically Change the Size of TextBox

    and you can also Customize that

    0 讨论(0)
  • 2020-12-09 08:03

    I know this is a kind of old post, but I found myself in this same issue, and by investigating a bit I found out that the Height of a WinForms TextBox is actually calculated depending on the size of the font it contains, it's just not quite equal to it.

    This guy explains how the calculation is done, and how you can set it on your TextBox to get the desired Height.

    Cheers!

    0 讨论(0)
  • 2020-12-09 08:07

    The following code added in your constructor after calling InitializeComponent() will make it possible to programmatically set your text box to the correct height without a) changing the Multiline property, b) having to hardcode a height, or c) mucking with the Designer-generated code. It still isn't necessarily as clean or nice as doing it in a custom control, but it's fairly simple and robust:

    if (txtbox.BorderStyle == BorderStyle.None)
    {
        txtbox.BorderStyle = BorderStyle.FixedSingle;
        var heightWithBorder = txtbox.ClientRectangle.Height;
        txtbox.BorderStyle = BorderStyle.None;
        txtbox.AutoSize = false;
        txtbox.Height = heightWithBorder;
    }
    

    I decided to make it cleaner and easier to use by putting it in a static class and make it an extension method on TextBox:

    public static class TextBoxExtensions
    {
        public static void CorrectHeight(this TextBox txtbox)
        {
            if (txtbox.BorderStyle == BorderStyle.None)
            {
                txtbox.BorderStyle = BorderStyle.FixedSingle;
                var heightWithBorder = txtbox.ClientRectangle.Height;
                txtbox.BorderStyle = BorderStyle.None;
                txtbox.AutoSize = false;
                txtbox.Height = heightWithBorder;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-09 08:10

    You can put it inside a panel that has the same back color with your desired height. This way has this advantage that the text box can center horizontally, which is not provided by other solutions.

    You can make it even more natural by using the following methods

        private void textBox1_Enter(object sender, EventArgs e)
        {
    
            panelTextBox.BorderStyle = BorderStyle.FixedSingle;
        }
    
        private void textBox1_Leave(object sender, EventArgs e)
        {
            panelTextBox.BorderStyle = BorderStyle.None;
        }
    
    0 讨论(0)
提交回复
热议问题