Change the Textbox height?
Asked Answered
C

21

70

How do I change the height of a textbox ?

Neither of the below work:

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

or

this.TextBox1.Size.Height = 100;

I wanted to be able to change the single line text box height to fit a font size on it without using multi-line if possible.

Carboniferous answered 2/5, 2011 at 2:41 Comment(0)
C
85

There are two ways to do this :

  • Set the textbox's "multiline" property to true, in this case you don't want to do it so;
  • Set a bigger font size to the textbox

I believe it is the only ways to do it; the bigger font size should automatically fit with the textbox

Cocoa answered 2/5, 2011 at 2:45 Comment(1)
Why is this marked as the correct answer? Adam's answer of setting AutoSize = false and then set dimensions is the correct answer.Chaworth
E
89

Go into yourForm.Designer.cs Scroll down to your textbox. Example below is for textBox2 object. Add this

this.textBox2.AutoSize = false;

and set its size to whatever you want

this.textBox2.Size = new System.Drawing.Size(142, 27);

Will work like a charm - without setting multiline to true, but only until you change any option in designer itself (you will have to set these 2 lines again). I think, this method is still better than multilining. I had a textbox for nickname in my app and with multiline, people sometimes accidentially wrote their names twice, like Thomas\nThomas (you saw only one in actual textbox line). With this solution, text is simply hiding to the left after each char too long for width, so its much safer for users, to put inputs.

Eutectoid answered 26/6, 2013 at 17:32 Comment(3)
No, because A/ it doesn't work if the textbox is docked B/ As pointed out, changing it in the designer breaks the fixJaved
Changing the Designer file manually is generally not a good fix to anything. Almost always ends up getting clobbered later.Ashcan
I can confirm Adam's answer and Franck's comment, AutoSize is indeed hidden from intellisense. If you type it up in full, it will be recognized as valid code. Try it, and then push Ctrl and click on AutoSize when underlined, you'll see the public override method in TextBoxBase (from metadata).Sandon
C
85

There are two ways to do this :

  • Set the textbox's "multiline" property to true, in this case you don't want to do it so;
  • Set a bigger font size to the textbox

I believe it is the only ways to do it; the bigger font size should automatically fit with the textbox

Cocoa answered 2/5, 2011 at 2:45 Comment(1)
Why is this marked as the correct answer? Adam's answer of setting AutoSize = false and then set dimensions is the correct answer.Chaworth
I
29

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.

Ilan answered 28/2, 2013 at 15:32 Comment(4)
I've found a quirk with this method - when placed inside of a container such as a cell of a TableLayoutPanel and you give it a Left-Right anchor, it doesn't vertically center correctly. Its vertical position seems to be calculated as though it were at its normal size.Jamila
@p0lar_bear: You could place the textbox on a panel with the right size as a workaround, and place this panel in the TableLayoutPanel cell.Ilan
@OlivierJacot-Descombes: True. It's my opinion, though, that if you're going to do something potentially messy like that, it'd be more manageable to just make it a multiline TextBox and suppress the Enter key, as suggested elsewhere on this page (though I know it's not what the OP was looking for - it works for me).Jamila
Another workaround would be to tweek the Margin property of the textbox or the Padding property of the container control.Ilan
A
9

Just found a great little trick to setting a custom height to a textbox.

In the designer view, set the minimumSize to whatever you desire, and then completely remove the size setting. This will cause the designer to update with the new minimum settings!

Alteration answered 6/11, 2014 at 11:48 Comment(2)
Works momentarily in the Designer, but as soon as it saves, it goes back to the previous height.Ashcan
Darn then! It was probably a bug they fixed. Though, if you want a larger textbox just alter the text size.Alteration
C
8

set the minimum size property

tb_01.MinimumSize = new Size(500, 300);

This is working for me.

Copepod answered 31/10, 2012 at 3:55 Comment(0)
T
4
public partial class MyTextBox : TextBox
{
    [DefaultValue(false)]
    [Browsable(true)]
    public override bool AutoSize
    {
        get
        {
            return base.AutoSize;
        }
        set
        {
            base.AutoSize = value;
        }
    }

    public MyTextBox()
    {
        InitializeComponent();
        this.AutoSize = false;
    }
}
Throve answered 28/5, 2014 at 4:48 Comment(1)
For anyone looking for the answer, setting AutoSize to false is the key. You can do it in code, if you don't want to create a new control. You'd have to set the height in code though.Knacker
D
3

Try the following :)

        textBox1.Multiline = true;
        textBox1.Height = 100;
        textBox1.Width = 173;
Defant answered 4/5, 2011 at 6:3 Comment(0)
W
3

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
Whitewash answered 21/3, 2017 at 15:44 Comment(1)
This works great, even without changing the font size if you copy the size value to minimumSize before setting multiline back to false.Terpsichorean
J
1

May be it´s a little late. But you can do this.

txtFoo.Multiline = true;
txtFoo.MinimumSize = new Size(someWith,someHeight);

I solved it that way.

Johnathan answered 11/5, 2011 at 15:43 Comment(0)
A
1

AutoSize, Minimum, Maximum does not give flexibility. Use multiline and handle the enter key event and suppress the keypress. Works great.

textBox1.Multiline = true;

 private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
            }
        }
Abomb answered 10/4, 2014 at 20:37 Comment(1)
The text will still wrap automatically if the user enters too much text, so overriding enter isn't sufficient. Setting Multiline to true is definitely NOT the answer to this question no matter how many people post it as the correct solution. (If you actually want a multiline text box, it's a great solution.)Ashcan
B
1

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;
    }
Bandung answered 26/12, 2015 at 7:59 Comment(0)
P
1

The Simplest Way to do that

  1. Right click on the TextBox.
  2. Go to properties.
  3. Set Multiline = True.

Now you will be able to resize the TextBox vertically as you wish.

Peale answered 2/2, 2018 at 8:5 Comment(0)
E
0

for me, the best approach is remove border of the textbox, and place it inside a Panel, which can be customized as you like.

Emma answered 25/12, 2012 at 22:43 Comment(1)
But it doesn't solve the problem. It makes the border look right, but the text still gets clipped at larger font sizes.Ashcan
A
0

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;
        }
    }
}
Ashcan answered 2/2, 2015 at 16:28 Comment(0)
D
0

Some of you were close but changing designer code like that is annoying because you always have to go back and change it again.

The original OP was likely using an older version of .net because version 4 autosizes the textbox height to fit the font, but does not size comboboxes and textboxes the same which is a completely different problem but drew me here.

This is the problem I faced when placing textboxes next to comboboxes on a form. This is a bit irritating because who wants two controls side-by-side with different heights? Or different fonts to force heights? Step it up Microsoft, this should be simple!

I'm using .net framework 4 in VS2012 and the following was the simplest solution for me.

In the form load event (or anywhere as long as fired after InitializeComponent): textbox.AutoSize = false Then set the height to whatever you want. For me I wanted my text boxes and combo boxes to be the same height so textbox.height = combobox.height did the trick for me.

Notes:

1) The designer will not be affected so it will require you to start your project to see the end result, so there may be some trial and error.

2) Align the tops of your comboboxes and textboxes if you want them to be aligned properly after the resize because the textboxes will grow down.

Delirium answered 1/12, 2016 at 16:6 Comment(1)
Your idea had me interested until I realized the AutoSize property on TextBox isn't available on asp.net 4.6.2 forms. It's intended only for winforms.Bishop
G
0

This is what worked nicely for me since all I wanted to do was set the height of the textbox. The property is Read-Only and the property is in the Unit class so you can't just set it. So I just created a new Unit and the constructor lets me set the height, then set the textbox to that unit instead.

Unit height = txtTextBox.Height;
double oldHeight = height.Value;
double newHeight = height.Value + 20; //Added 20 pixels
Unit newHeightUnit = new Unit(newHeight);
txtTextBox.Height = newHeightUnit;
Govea answered 29/3, 2017 at 19:13 Comment(0)
P
0

You can make multiline : false and then just change the text size on the text box then the height will automatically increment

Plantar answered 14/7, 2021 at 19:49 Comment(0)
C
-1

you can also change you can also change MinimumSize

Cedrickceevah answered 9/4, 2019 at 6:28 Comment(0)
R
-1

So after having the same issue with not being able to adjust height in text box, Width adjustment is fine but height never adjusted with the above suggestions (at least for me), I was finally able to take make it happen. As mentioned above, the issue appeared to be centered around a default font size setting in my text box and the behavior of the text box auto sizing around it. The default font size was tiny. Hence why trying to force the height or even turn off autosizing failed to fix the issue for me.

Set the Font properties to the size of your liking and then height change will kick in around the FONT size, automatically. You can still manually set your text box width. Below is snippet I added that worked for me.

    $textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(60,300)
$textBox.Size = New-Object System.Drawing.Size(600,80)
$textBox.Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Regular)
$textBox.Form.Font = $textbox.Font

Please note the Height value in '$textBox.Size = New-Object System.Drawing.Size(600,80)' is being ignored and the FONT size is actually controlling the height of the text box by autosizing around that font size.

Regelation answered 14/4, 2019 at 3:34 Comment(0)
H
-1

All you have to do is enable the multiline in the properties window, put the size you want in that same window and then in your .cs after the InitializeComponent put txtexample.Multiline = false; and so the multiline is not enabled but the size of the txt is as you put it.

InitializeComponent();
txtEmail.Multiline = false;
txtPassword.Multiline = false;

enter image description here

enter image description here

enter image description here

Herbie answered 1/7, 2019 at 3:44 Comment(1)
The multiline is not being activated, it is only being used as a resource to change the height, but if we press enter in our textbox there will be no line break.Herbie
S
-3

I think this should work.

TextBox1.Height = 100;
Slipover answered 2/5, 2011 at 2:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.