可扩展的 WinForms 文本框 [英] Expandable WinForms TextBox

查看:19
本文介绍了可扩展的 WinForms 文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a textbox in a Windows Forms application that starts out at a height for entering text in a single line. But I would like the textbox to automatically increase its height if the user enters text that is wrapped within the control.

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

Currently, for this textbox, I have the properties multiline and wordwrap set to true. I've tried using the TextChanged event to determine when the text has been wrapped but I'm unable to find any property that will help me with this. The Lines property does not provide any help with wrapped text; only for text that the user has hit enter to begin a new line.

如何让我的文本框在每次文本换行超过文本框宽度时扩展其高度?

How can I get my textbox to expand its height each time the text wraps past the width of the textbox?

推荐答案

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

Same kind of idea as others have posted, put this in your textChanged event:

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

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

You will need some kind of minimum height, and possibly to specify some padding, but this does work.

这篇关于可扩展的 WinForms 文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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