【问题标题】:if user start typing in textbox value change to input else if user didn't type any thing value change to an specific string in C#如果用户开始在文本框中输入值更改为输入,否则如果用户没有输入任何内容值更改为 C# 中的特定字符串
【发布时间】:2015-04-22 20:57:06
【问题描述】:

我有一个列表视图并创建了一个文本框来搜索该列表中的项目! 搜索一切正常! 但问题是我希望我的搜索框是这样的: 起初 Searchbox.Text="Search ..." 如果用户开始在该搜索框中输入,则更改为该关键字! else(if) searchbox 有空 Searchbox.Text 再次更改为“Search ...”! 也许它有点复杂!但我试了1-2个小时!来不及了! 我用过定时器、checkboxchange 事件、布尔值、...!但做不到! :( 请帮忙! *我的搜索框名称是 textbox1。 我测试的一些代码:

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        string str = textBox1.Text;

        /*
        if (!keywordentered_bool)
        {
            textBox1.Text = "";
        }
         */

        if (str != "")
        {


           //Doing Search operations!
                search_bool = true;
            }
            else
            {//Doing Search operations!

                search_bool = true;

              //  keywordentered_checkbox.Checked = true;

                Searchtextbox_Timer.Interval = 100;
                Searchtextbox_Timer.Enabled = true;
                Searchtextbox_Timer.Tick += Searchtextbox_Timer_Tick; 
                //textBox2.Visible = false;

            }
        }
        else 
        {
            if (search_bool)
            {
                listView1.Items.Clear();
                label1.Visible = false;
                listView1.Items.AddRange(Originalplaylist_list.ToArray());
                if (!search_bool)
                {
                    listView1.Items[MusicLogindex_list[MusicLogindex_list.Count - 1]].ForeColor = Color.Cyan;
                }
                else if (search_bool)
                {//Doing Search operations

            search_bool = false;

            Searchtextbox_Timer.Interval = 100;
            Searchtextbox_Timer.Enabled = true;
            Searchtextbox_Timer.Tick += Searchtextbox_Timer_Tick; 
            //textBox2.Visible = true;
           // keywordentered_checkbox.Checked = false;

        }
    }
void Searchtextbox_Timer_Tick(object sender, EventArgs e)
    {
        if (!search_bool)
        {
            textBox2.Visible = true;
            textBox2.Location = textBox1.Location;
            //textBox1.Text = "Search ...";
            //textBox1.ForeColor = Color.Gray;

            //textBox1.Font = new Font(textBox1.Font, FontStyle.Italic);
        }
        else 
        {
            textBox2.Visible = false;
         //   textBox1.Text = "";
         //   textBox1.ForeColor = Color.Black;

         // textBox1.Font = new Font(textBox1.Font, FontStyle.Regular);
        }
        Searchtextbox_Timer.Enabled = false;
        //throw new NotImplementedException();
    }

【问题讨论】:

  • Winforms 还是 Asp.net 网络表单?
  • @FrebinFrancis 我认为您引用的链接是我正在寻找的,但不幸的是演示下载链接不起作用! ://
  • @ACE 你用源码链接试试?
  • 我下载了,但还没有测试,因为它只是一个类!

标签: c# search textbox user-input


【解决方案1】:

这只是伪代码,但概念已经存在,您需要实现文本更改事件以进行搜索,您可以在事件处理程序中进行额外的更改。

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(string.IsNullorEmpty(myTxtbx.Text))
        myTxtbx.Text = "Enter text here...";
}

这是一个动态文本框控件的示例,您可以将这些事件添加到您的控件中并使用相同的代码使其工作。

或者你可以使用这个插件

Visual Studio gallery plugin

您可以为此目的使用的另一个插件

Textbox with placeholder

希望这会有所帮助。

【讨论】:

  • 首先感谢您的帮助!然后我试一试,但我认为这不是我想要的!
  • 您的实际需求是什么。你只是想显示一个占位符之类的东西不是吗?
  • 有点糟糕,我不知道占位符到底是什么!但是当我看到那个链接中的图片时,我想这就是我想要的!
  • @ACE 我用插件链接更新了我的答案,您只需安装该文件,您将在工具箱上获得额外的文本框控件。
【解决方案2】:

感谢@Frebin Francis,我的问题解决了! 我从这个链接TextBox With Placeholder下载了源代码 并将其添加到我的项目中!然后将其添加到我的表单中,是的! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多