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

2016-03-30 11:19:59来源:cnblogs.com作者:wyx940人点击

    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;
                                }
                            }

                        }
            }

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台