单击它时清除文本框中的文本 [英] Clear text in Text box on clicking it

查看:39
本文介绍了单击它时清除文本框中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望文本框上已经有产品名称的文本在我点击时自动消失,我可以在其中输入我想要的文本框.

I want my TextBox's text which has already product name on it to automatically vanishes when I click on it, and I can enter the text box I want in it.

  • 产品名称必须始终存在,而 TextBox 中没有文本
  • 我也希望,如果我第二次点击 TextBox,我不会丢失我已经输入的内容.

请让我知道如何在不丢失我手动输入的数据的情况下执行此操作,并且只要我自己在文本框中没有输入任何内容,我就可以获得默认文本.

Please let me know how can I do it without loosing the data I've entered manually in it and I can get the default text whenever there is nothing entered by myself in the text box.

推荐答案

为什么要使用一些额外的软件而不是用自己的头脑来编码?这是实现此任务的简单代码

why use some extra software and not to use your own mind to code? here is the simple code to achieve this task

首先使用这个:

public bool txSearch = false;

然后在您的文本点击事件代码上:

then on your text click event code:

private void txtSearch_Click(object sender, EventArgs e)
{
    txSearch = true;

    if (txtSearch.Text == "Product Name")
    {
        if (txSearch == true)
        {
            txtSearch.Text = "";
        }                   
    }
}

当您点击文本时,这将清除您的字段文本框,现在当您的文本框中没有任何内容时写回产品名称并且您将其保留在文本框离开事件中执行此代码:

this will clear your field text box when you click on the text, now to write back the product name when there is nothing in your textbox and you are leaving it do this code on textbox leaving event:

private void txtSearch_Leave(object sender, EventArgs e)
{
    if (txtSearch.Text == "")  // here you can also use txtSearch.Text != "Poduct Name", but it could affect your search code possibly 
    {
        txtSearch.Text = "Product Name";  
    } 
}

这篇关于单击它时清除文本框中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆