Change the Textbox height?

2019-02-05 10:54发布

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.

18条回答
再贱就再见
2楼-- · 2019-02-05 11:25

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;
            }
        }
查看更多
等我变得足够好
3楼-- · 2019-02-05 11:29

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
查看更多
贼婆χ
4楼-- · 2019-02-05 11:29

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!

查看更多
Anthone
5楼-- · 2019-02-05 11:30

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

查看更多
萌系小妹纸
6楼-- · 2019-02-05 11:36

Try the following :)

        textBox1.Multiline = true;
        textBox1.Height = 100;
        textBox1.Width = 173;
查看更多
ら.Afraid
7楼-- · 2019-02-05 11:37

I think this should work.

TextBox1.Height = 100;
查看更多
登录 后发表回答