将占位符文本添加到文本框 [英] Adding placeholder text to textbox

查看:32
本文介绍了将占位符文本添加到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将占位符文本添加到文本框的方法,就像使用 html5 中的文本框一样.

I am looking for a way to add placeholder text to a textbox like you can with a textbox in html5.

即如果文本框没有文本,则添加文本Enter some text here,当用户点击它时,占位符文本消失并允许用户输入自己的文本,如果文本框失去焦点并且仍然没有文本然后占位符被添加回文本框.

I.e. if the textbox has no text, then it adds the text Enter some text here, when the user clicks on it the placeholder text disappears and allows the user to enter their own text, and if the textbox loses focus and there is still no text then the placeholder is added back to the textbox.

推荐答案

不就是这样吗:

Textbox myTxtbx = new Textbox();
myTxtbx.Text = "Enter text here...";

myTxtbx.GotFocus += GotFocus.EventHandle(RemoveText);
myTxtbx.LostFocus += LostFocus.EventHandle(AddText);

public void RemoveText(object sender, EventArgs e)
{
    if (myTxtbx.Text == "Enter text here...") 
    {
     myTxtbx.Text = "";
    }
}

public void AddText(object sender, EventArgs e)
{
    if (string.IsNullOrWhiteSpace(myTxtbx.Text))
        myTxtbx.Text = "Enter text here...";
}

那只是伪代码,但概念就在那里.

Thats just pseudocode but the concept is there.

这篇关于将占位符文本添加到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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