根据文本长度调整文本框和窗体大小 [英] Resize textbox and form size according to Text Length

查看:1117
本文介绍了根据文本长度调整文本框和窗体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能自动增加/减少TextBox和Windows窗体大小根据文本长度?

How can I automatically increase/decrease TextBox and Windows Form size according to text Length?

推荐答案

您可以尝试重写 OnTextChanged 事件,然后更改的宽度取决于文本的大小

You can try overriding the OnTextChanged event, then changing the Width depending on the size of the text.

protected override OnTextChanged(EventArgs e)
{
    using (Graphics g = CreateGraphics())
    {
        SizeF size = g.MeasureString(Text, Font);
        Width = (int)Math.Ceiling(size.Width);
    }
    base.OnTextChanged(e);
}

这篇关于根据文本长度调整文本框和窗体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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