.net 如何使用默认文本填充空文本框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5178247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I fill an empty textbox with default text?
提问by Mac
How do I fill a textbox with text if it is empty? I am using VB.NET.
如果文本框为空,如何用文本填充文本框?我正在使用 VB.NET。
回答by Cody Gray
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 TextChangedevent. It looks like this:
看起来您正在描述提示横幅,它是显示在空文本框中的提示文本。从 Windows XP 开始,操作系统本身就支持此功能。以这种方式实现的效果比自己在TextChanged事件中设置默认文本要优雅得多。它看起来像这样:


Setting this up is accomplished at the level of the Windows API by sending the textbox control an EM_SETCUEBANNERmessage. To use this from a .NET project, you will have to use P/Invoke.
通过向文本框控件发送EM_SETCUEBANNER消息,在 Windows API 级别完成此设置。要在 .NET 项目中使用它,您必须使用 P/Invoke。
Fortunately, most of the work has already been done for you. This sample projectis 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 TextBoxcontrol, and add the code to support cue banners there. See this answerfor the code you'll need. If you have trouble converting it to VB.NET, try this tool.
如果您不希望您的应用程序依赖于外部 DLL,您可以将必要的代码直接添加到您的项目中。最简单的方法是对现有TextBox控件进行子类化,并在那里添加支持提示横幅的代码。请参阅此答案以获取您需要的代码。如果您在将其转换为 VB.NET 时遇到问题,请尝试使用此工具。
回答by madd0
You will probably want to handle the TextChangedevent and set some default text if the text box is empty when the event is fired.
TextChanged如果在触发事件时文本框为空,您可能希望处理该事件并设置一些默认文本。
I don't have a VB.NET example, but the following C# should be too hard to understand:
我没有 VB.NET 示例,但以下 C# 应该太难理解了:
public Form1()
{
this.InitializeComponent();
textBox1.Tag = "Default text";
textBox1.Text = (string)textBox1.Tag;
textBox1.TextChanged += new EventHandler(OnTextChanged);
}
void OnTextChanged(object sender, EventArgs e)
{
var textbox = (TextBox)sender;
if (string.IsNullOrEmpty(textbox.Text))
{
textbox.Text = (string)textbox.Tag;
}
}
And the event handler can be reused for several text boxes.
并且事件处理程序可以重复用于多个文本框。
EDIT:Here's pretty much the same in VB.NET
编辑:这在 VB.NET 中几乎相同
Sub New()
' This call is required by the designer.
InitializeComponent()
TextBox1.Tag = "Default text" ' This can be set with the designer
TextBox1.Text = CStr(TextBox1.Tag)
End Sub
Private Sub OnTextBoxTextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim textbox As TextBox = DirectCast(sender, TextBox)
If String.IsNullOrEmpty(textbox.Text) Then
textbox.Text = CStr(textbox.Tag)
textbox.SelectAll()
End If
End Sub
Of course, you can also achieve similar behavior using native Windows functionality, but a few lines of managed code will give you pretty much all you need even if you do not want to use Win32.
当然,您也可以使用本机 Windows 功能实现类似的行为,但是即使您不想使用 Win32,几行托管代码也几乎可以满足您的所有需求。
回答by madd0
default text handling in a text box
文本框中的默认文本处理
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "Default Text" ' initialize the text box
End Sub
clear the text when the cursor is in the text box
当光标在文本框中时清除文本
Private Sub TextBox1_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1_GotFocus
TextBox1.Text = "" ' clear the text box for typing
End Sub
If the text box remains empty after data change then the default text comes again
如果数据更改后文本框保持为空,则默认文本再次出现
Private Sub TextBox1_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1_LostFocus
TextBox1.Text = "" ' clear the text box for typing
End Sub
回答by DeleteMyAccount
I assume you meant to add text from the aspx page.
我假设您打算从 aspx 页面添加文本。
<asp:TextBox ID="TextBox1" runat="server" value="Default Value"></asp:TextBox
Even when .NET don't suggest this feature in the intellitext, I assume that it will allow you to put any attribute, even custom that you can manipulate data in it. But, it does the work. and the value attribute is sent to the browser, therefore, initiate in the text field.
即使 .NET 不建议在智能文本中使用此功能,我也假设它允许您放置任何属性,甚至是您可以在其中操作数据的自定义属性。但是,它确实有效。并且 value 属性被发送到浏览器,因此,在文本字段中启动。
For TextArea (TextMode="MultiLine"), you can put between the tags.
对于 TextArea (TextMode="MultiLine"),您可以放在标签之间。
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine">Text Here will be inside the TextArea</asp:TextBox>
It is like html textarea tag behave.
这就像 html textarea 标签的行为。
回答by Anuraj
Are you looking for something like this?
你在寻找这样的东西吗?
If Textbox.Text = string.Empty Then
TextBox.Text = "Default Text"
End If
回答by Wojtek Turowicz
I would create a class that inherits TextBox and do two things with it:
我会创建一个继承 TextBox 的类并用它做两件事:
- Add DefaultText string property
- Override the Text setter to always set this DefaultText if the new Text value is String.Empty
- 添加 DefaultText 字符串属性
- 如果新的 Text 值为 String.Empty,则覆盖 Text setter 以始终设置此 DefaultText

