如何根据另一个文本框内容调整/拉伸文本框的大小?

时间:2019-04-05 11:48:20

标签: c# winforms textbox resize

我想根据其他文本框内容调整文本框的大小。

这是我试图做的。

private void button1_Click(object sender, EventArgs e)
{
    receive.AutoSize = true;
    receive.Text = send.Text;
}

我希望文本框根据另一个文本框内容自动拉伸或在X和Y上调整大小。

2 个答案:

答案 0 :(得分:0)

好的,所以我发现了类似的东西。

    private void button1_Click(object sender, EventArgs e)
    {
        receive.Text = send.Text;
        Size sz = new Size(receive.ClientSize.Width, int.MaxValue);
        TextFormatFlags flags = TextFormatFlags.WordBreak;
        int padding = 3;
        int borders = receive.Height - receive.ClientSize.Height;
        sz = TextRenderer.MeasureText(receive.Text, receive.Font, sz, flags);
        int h = sz.Height + borders + padding;
        if (receive.Top + h > this.ClientSize.Height - 10)
        {
            h = this.ClientSize.Height - 10 - receive.Top;
        }
        receive.Height = h;
    }

但是我需要设置最大高度和宽度以及最小值。我该怎么办?

答案 1 :(得分:0)

 private void sender_TextChanged/*or button1_Click*/(object sender, EventArgs e)
 {
      Graphics graph = CreateGraphics();

      SizeF measuredStringSize = graph.MeasureString(tb_sender.Text, tb_sender.Font);

      tb_receiver.Width = (int)measuredStringSize.Width;
      tb_receiver.Text = tb_sender.Text;
 }