Expandable WinForms TextBox(可扩展的 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 文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:可扩展的 WinForms 文本框
基础教程推荐
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
