如何在窗口应用程序的文本框中给出占位符 [英] how to give placeholder in textbox in window application

查看:81
本文介绍了如何在窗口应用程序的文本框中给出占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows窗体中有一个文本框

现在我想要一个文本框上的默认文本,例如

我们在html

i have a textbox in windows form
now i want a default text on textbox like
we have place holder in html

推荐答案

如果您想准备好解决方案,请查看:



带有水印的自定义文本框 [ ^ ]
If you want ready to go solution, check this out:

Custom TextBox with Watermark[^]


Refer- 将占位符文本添加到文本框 [ ^ ]。

Refer- Adding placeholder text to textbox[^].
Quote:

这不会是这样的:

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

myTxtbx.OnFocus += OnFocus.EventHandle(RemoveText);
myTxtbx.LoseFocus += LoseFocus.EventHandle(AddText);

public RemoveText(object sender, EventArgs e)
{
     myTxtbx.Text = "";
}

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

这只是伪问题,但概念就在那里。

Thats just psuedocode but the concept is there.


private void Form_load(object sender, EventArgs e)
{
    textBox.Text = "Place Holder text..."
}

private void textBox_Enter(object sender, EventArgs e)
{
    if(textBox.Text == "Place Holder text...")
    {
        textBox.Text = ""
    }
}

private void textBox_Leave(object sender, EventArgs e)
{
    if(textBox.Text == "")
    {
        textBox.Text = "Place Holder text..."
    {
}





这会在表单加载时将文本放在文本框中。当焦点放在文本框上时,文本框的当前文本(焦点前)是placeHolder的文本,它会清除文本。

如果焦点离开文本框而没有插入文本文本更改回placeHolder文本。

在文本框上获得焦点时检查placeHolder文本是为了确保不删除以前的用户输入文本。



这应该可以让你知道如何做到这一点



如果这不是你的想法

看看这个

带有占位符的TextBox [ ^ ]

它可能更符合你要找的东西



This places text in the textbox on form load. when focus is placed on the textbox and the current text of the textbox(before focus) is the text of the placeHolder, it clears the text.
If focus leaves the textbox and no text is inserted the text changes back to the placeHolder text.
The check on the placeHolder text when focus is gained on the textbox is to ensure that previously user input text is not removed.

This should give you some idea of how you might want to do it

If this is not what you had in mind
Take a look at this
TextBox with Placeholder[^]
it might be more to what you are looking for


这篇关于如何在窗口应用程序的文本框中给出占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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