可扩展的WinForms文本框文本框、WinForms

2023-09-04 12:36:41 作者:扣扣大全

我已经创建了一个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.

目前,这个文本框,我的属性多行和换行设置为true。我已经使用TextChanged事件,以确定文本已换,但我无法找到任何财产,这将帮助我在尝试。该行酒店不提供任何帮助裹文字;只对文本用户已按回车键开始一个新行。

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.