如何在单击文本框中删除文本并在删除文本框内的光标后取回文本?

时间:2018-02-17 18:33:26

标签: vb.net winforms

我的代码如下,当我单击我的搜索文本框时,文本将被清除,前景变为黑色。

Private Sub txbSearch_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles txbSearch.MouseClick
        txbSearch.Text = ""
        txbSearch.ForeColor = Color.Black
    End Sub

我的问题是,当我使用搜索框完成后,我希望将文本设置回"搜索"和forecolor设置为灰色。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

我想我知道你要求的是什么,我希望这就是你所需要的:

Private Sub TextBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseClick
    If TextBox1.Text = "Search" Then
        TextBox1.ForeColor = Color.Black
        TextBox1.Text = ""
    End If
End Sub

Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
    If TextBox1.Text = "" Then
        TextBox1.Text = "Search"
        TextBox1.ForeColor = Color.Gray
    End If
End Sub

但请尝试下次更好地解释,其他人也要了解并从中获益。快乐编码:)