How开发者_如何学C do I fill a textbox with text if it is empty? I am using VB.NET.
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:
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.
You will probably want to handle the TextChanged
event and set some default text if the text box is empty when the event is fired.
I don't have a VB.NET example, but the following C# should be too hard to understand:
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
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.
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
Are you looking for something like this?
If Textbox.Text = string.Empty Then
TextBox.Text = "Default Text"
End If
I would create a class that inherits TextBox and do two things with it:
- Add DefaultText string property
- Override the Text setter to always set this DefaultText if the new Text value is String.Empty
I assume you meant to add text from the aspx page.
<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.
For TextArea (TextMode="MultiLine"), you can put between the tags.
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine">Text Here will be inside the TextArea</asp:TextBox>
It is like html textarea tag behave.
精彩评论