Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

.net - How do I fill an empty textbox with default text?

How do I fill a textbox with text if it is empty? I am using VB.NET.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

     sample of textbox with cue banner text

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...