How to set the desired height in TextBox?

    4 answers 4

    The height of a single-line TextBox depends on the font size and is calculated automatically. The source has a comment on the fact that you can turn off AutoSize and change the height, but according to the source it is a hidden public property inherited from TextBoxBase , which is not displayed either in the autocomplete or in the property editor. If you force it into * .Designer.cs (), then it works only for the time of editing the form, after launching the designer will delete your entry from your file, everything will return to its original state, although in the editing mode of the form, the TextBox will still pretend to be "compliant boy "resize, but only during editing.

    Workarounds:

    In the visual editor:

    1. You can try to choose the font of the desired size, but to set the size with pixel accuracy will not work.

    2. If you want to quickly and clearly - you can put the TextBox on the Panel . Set Border = None for TextBox and Border = Fixed3d for Panel . And also replace the background color of the Panel from the default Control to Window . It turns out such a composite control that looks like a TextBox . This option will also help if you need to set the text entry position to a position other than the upper left corner.

    In the code:

    1. You can customize TextBox parameters, including the hidden AutoSize property completely in the code, of course this option eliminates the possibility of a preview in the form editor.

    2. You can create a class inherited from TextBox and either override the AutoSize property and make it visible (it is virtual and hidden by attributes, see here ), or define a default constructor (without parameters) and simply set it to false in the designer and resize as you like even form editor.

    PS: In the forms there are still many such not obvious surprises, some are solved, some are not.

    • Thank you very much. Used 4 options. The second option is interesting by setting the position of the beginning of the text entry, but still stopped at the last version. - Alexander Puzanov

    You need to set the AutoSize property of the TextBox to false. You can inherit from the TextBox class.

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

    Or in the designer add code

     textBox.AutoSize = false; textBox.Size = new System.Drawing.Size(100, 50); 

    You can also try to change the size after setting the property Multiline = true

    • multiLine as an option is not suitable. - Alexander Puzanov
    • @ Alexander Puzanov Vmysle not suitable for your task? And the above options? - user2455111
    • about the designer wrote above, there is not so simple - rdorn

    From rezmetka

     <TextBox Height="45" /> 

    or from code

     var textBox = new TextBox { Height = 45 }; 
    • 3
      there is no markup in winforms , apparently mixed up with wpf - user2455111
    • Yes, I did not notice the tag. - risty

    As far as I remember, the element TextBox height depends on how many lines it fits, in other words, it has a Multiline property, you need to set it to true so that your TextBox is several lines high. From the code it becomes not difficult.

    First option:

     textBox1.Multiline = true; textBox1.Height = 32; 

    And after that you can change the height already.


    However, if you want to leave one line, while the text was more - you need to change the font size Font in the text box of your TextBox .

    The second option:

     textBox1.Font = new Font(textBox1.Font.FontFamily, 32); 

    That is, it all depends on what you want to see, if several lines are the same size as before - you are on the first path, if the text is larger - change the font size. It can be changed either by program code or through component properties in the visual interface of the development environment.