如果文本框为空,如何用文本填充?我正在使用 VB.NET.
How do I fill a textbox with text if it is empty? I am using VB.NET.
您似乎在描述一个 提示横幅,它是显示在空文本框中的提示文本.从 Windows XP 开始,操作系统本身就支持此功能.这样实现的效果比在 TextChanged
事件中自己设置默认文本要优雅得多.它看起来像这样:
It looks like you're describing a cue banner, which is prompt text that is displayed in an empty textbox. As of Windows XP, this functionality is natively supported by the operating system. The effect achieved doing it this way is much more elegant than setting the default text yourself in the TextChanged
event. It looks like this:
通过向文本框控件发送 EM_SETCUEBANNER
消息.要在 .NET 项目中使用它,您必须使用 P/Invoke.
Setting this up is accomplished at the level of the Windows API by sending the textbox control an EM_SETCUEBANNER
message. To use this from a .NET project, you will have to use P/Invoke.
幸运的是,大部分工作已经为您完成.这个示例项目是一种快速、轻松的方式来将提示横幅支持添加到现有项目.这里是另一个示例,对过程进行了更完整的说明.
Fortunately, most of the work has already been done for you. This sample project is a quick and painless way to add cue banner support to an existing project. Here's another sample, with a more complete explanation of the process.
如果您不希望您的应用程序依赖于外部 DLL,您可以将必要的代码直接添加到您的项目中.最简单的方法是继承现有的 TextBox
控件,并在其中添加支持提示横幅的代码.有关您需要的代码,请参阅此答案.如果您在将其转换为 VB.NET 时遇到问题,请尝试 此工具.
If you don't want your application to depend on an external DLL, you can add the necessary code directly to your project. The simplest way is to subclass the existing TextBox
control, and add the code to support cue banners there. See this answer for the code you'll need. If you have trouble converting it to VB.NET, try this tool.
这篇关于如何用默认文本填充空文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!