4

我在 Windows 窗体应用程序中创建了一个文本框,该文本框以在单行中输入文本的高度开始。但是,如果用户输入包含在控件中的文本,我希望文本框自动增加其高度。

目前,对于这个文本框,我将属性 multiline 和 wordwrap 设置为 true。我尝试使用 TextChanged 事件来确定文本何时被换行,但我找不到任何可以帮助我解决此问题的属性。Lines 属性不提供任何包装文本的帮助;仅适用于用户按回车键开始新行的文本。

每次文本环绕超过文本框的宽度时,如何让我的文本框扩展其高度?

4

6 回答 6

7

与其他人发布的想法相同,请将其放入您的 textChanged 事件中:

Dim s As SizeF = TextRenderer.MeasureText(txt.Text, txt.Font, txt.ClientRectangle.Size, TextFormatFlags.WordBreak)
txt.Height = CInt(s.Height)

您将需要某种最小高度,并且可能需要指定一些填充,但这确实有效。

于 2009-07-30T14:42:45.573 回答
2

如果您愿意改用 RichTextBox(根据我的经验,这是一种带有很多怪癖的脾气暴躁的控件),您可以使用 ContentsResized 事件,它为您提供新的所需大小:

private void HandleContentsResized(object sender, ContentsResizedEvenetArgs e)
{
    int newheight = e.NewRectangle.Height;
}
于 2009-07-30T14:31:22.410 回答
2

我只是为另一个项目的标签控件编写了这个。我从代码项目的某个地方得到了我认为的代码。将其更改为文本框应该与更改基础一样简单。

public class GrowLabel : Label
{
    private bool _growing;
    //public bool GrowFontSize { get; set; }

    public GrowLabel()
    {
        AutoSize = false;
        //GrowFontSize = false;
    }

    public override sealed bool AutoSize
    {
        get { return base.AutoSize; }
        set { base.AutoSize = value; }
    }

    private void ResizeLabel()
    {
        if (_growing) return;
        try
        {
            _growing = true;

            var sz = new Size(Width, Int32.MaxValue);
            sz = TextRenderer.MeasureText(Text, Font, sz, TextFormatFlags.WordBreak);
            Height = sz.Height;
        }
        finally
        {
            _growing = false;
        }
    }

    protected override void OnTextChanged(EventArgs e)
    {
        base.OnTextChanged(e);
        ResizeLabel();
    }

    protected override void OnFontChanged(EventArgs e)
    {
        base.OnFontChanged(e);
        ResizeLabel();
    }

    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        ResizeLabel();
    }
}
于 2009-07-30T14:33:55.837 回答
2

AdamSane 的帖子很有帮助,但文本框没有增长。我会做一些修改。我的模组如下:

class GrowTextBox : TextBox
{
    private double m_growIndex = 0.0;
    private Timer m_timer;

    public GrowTextBox()
    {
        AutoSize = false;
        this.Height = 20;

        // Without the timer, I got a lot of AccessViolationException in the System.Windows.Forms.dll.
        m_timer = new Timer();
        m_timer.Interval = 1;
        m_timer.Enabled = false;
        m_timer.Tick += new EventHandler(m_timer_Tick);

        this.KeyDown += new KeyEventHandler(GrowTextBox_KeyDown);
    }

    void GrowTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
        {
            this.SelectAll();
        }
    }

    void m_timer_Tick(object sender, EventArgs e)
    {
        var sz = new Size(Width, Int32.MaxValue);
        sz = TextRenderer.MeasureText(Text, Font, sz, TextFormatFlags.TextBoxControl);

        m_growIndex = (double)(sz.Width / (double)Width);

        if (m_growIndex > 0)
            Multiline = true;
        else
            Multiline = false;

        int tempHeight = (int)(20 * m_growIndex);

        if (tempHeight <= 20)
            Height = 20;
        else
            Height = tempHeight;

        m_timer.Enabled = false;
    }

    public override sealed bool AutoSize
    {
        get { return base.AutoSize; }
        set { base.AutoSize = value; }
    }


    protected override void OnTextChanged(EventArgs e)
    {
        base.OnTextChanged(e);
        m_timer.Enabled = true;
    }

    protected override void OnFontChanged(EventArgs e)
    {
        base.OnFontChanged(e);
        m_timer.Enabled = true;
    }

    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        m_timer.Enabled = true;
    }
}
于 2011-08-08T12:12:35.780 回答
1

我成功地使用下面的代码直到第 10 行,然后它得到 1 个字符,但这对我有用。不要问我 - 7 和 - 12 之类的随机数,它们与填充有关

    private void txbDescription_TextChanged(object sender, EventArgs e)
    {
        SizeF s = TextRenderer.MeasureText(txbDescription.Text, txbDescription.Font, txbDescription.ClientRectangle.Size, TextFormatFlags.TextBoxControl);

        int lines = (int)Math.Ceiling((decimal)Convert.ToInt32(s.Width - 7) / ((decimal)txbDescription.Width - 12));

        if (lines == 0)
        {
            txbDescription.Height = 20;
        }
        else
        {
            txbDescription.Height = 20 + (lines - 1) * 13;
        }
    }
于 2013-06-10T18:21:26.747 回答
0

不幸的是,我无法提供细节,但您可能需要进行自定义实现。

我会派生一个新的文本框类型——ExpandableTextBox——然后你需要手动实现它。

这似乎也与您正在寻找的内容相关:http: //social.msdn.microsoft.com/Forums/en-US/winforms/thread/11dfb280-b113-4ddf-ad59-788f78d2995a

于 2009-07-30T14:28:16.720 回答