自动调整文本框控件垂直 [英] Autoresize textbox control vertically

查看:168
本文介绍了自动调整文本框控件垂直的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#的形式,我有一个面板固定四面,并在里面,一个文本框,挂靠顶部/左/右。

In a C# form, I have a panel anchored all sides, and inside, a textbox, anchored top/left/right.

当文字被装载到文本框,我希望它自动扩大自身垂直,这样我不需要滚动文本框(滚动顶多面板,如果有不适合的面板更多的文字) 。
有没有办法用一个文本框来做到这一点? (我不受限的,所以如果有适合的描述另一个控制使用这种控制,可随时提吧)

When text gets loaded into the textbox, i want it to auto expand itself vertically so that I don't need to scroll the textbox (scroll the panel at most, if there is more text that doesn't fit the panel). is there any way to do this with a textbox? (i'm not constrained to use this control so if there's another control that fits the description, feel free to mention it)

推荐答案

我会认为这是一个多行文本框,你会允许它垂直增长。这code效果很好:

I'll assume this is a multi-line text box and that you'll allow it to grow vertically. This code worked well:

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

您应该当文本框为空,如设置属性的minimumSize做一些合理的。

You ought to do something reasonable when the text box is empty, like setting the MinimumSize property.

这篇关于自动调整文本框控件垂直的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆