欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

winform listbox与textbox组合提示框 模糊查询

程序员文章站 2022-04-15 15:56:25
private void listbox1_MouseClick(object sender, MouseEventArgs e) { textbox1.Visible = false; textbox1.Text = lsbSearchItem.SelectedItem.ToString(); t ......

    private void listbox1_MouseClick(object sender, MouseEventArgs e)
        {
            textbox1.Visible = false;
            textbox1.Text = lsbSearchItem.SelectedItem.ToString();
            textbox1.Focus();
            textbox1.SelectAll();
        }

        private void textbox1_KeyUp(object sender, KeyEventArgs e)
        {
                if (textbox1.Text == "")
                {
                    listbox1.Items.Clear();
                    listbox1.Visible = false;
                    return;
                }
                if (e.KeyCode == Keys.Up)
                {
                    if (listbox1.SelectedIndex > 0)
                        listbox1.SelectedIndex -= 1;
                }
                else
                    if (e.KeyCode == Keys.Down)
                    {
                        if (listbox1.SelectedIndex < listbox1.Items.Count - 1)
                            listbox1.SelectedIndex += 1;
                    }
                    else
                    {
                        if (e.KeyCode == Keys.Enter)
                        {
                            textbox1.Text = listbox1.SelectedItem.ToString();
                            listbox1.Visible = false;
                            textbox1.Focus();
                            textbox1.SelectAll();
                        }
                        else
                        {
                            listbox1.Items.Clear();
                            for (int i = 0; i < typeItem.Length; i++)
                            {
                                if (str[i].IndexOf(textbox1.Text.Trim()) > 0)
                                {
                                    listbox1.Items.Add(str[i]);
                                    listbox1.Visible = true;
                                    listbox1.SelectedIndex = 0;
                                }
                            }

                        }
            }