可扩展的 WinForms 文本框

Expandable WinForms TextBox(可扩展的 WinForms 文本框)
本文介绍了可扩展的 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 文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)