用户在文本框输入数据,文本框下面自动提示

来源:互联网 发布:淘宝淘口令设置 编辑:程序博客网 时间:2024/06/06 22:57

1 系统预测的数据,我是放在listbox当中。

2 listbox 的高度随着数据变化

3 当匹配个数小于2时,自动隐藏。

效果如下。。。







具体代码如下:








using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;


namespace 输入提示
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cha_Click(object sender, EventArgs e)
        {

        }
        List<string> test = new List<string>();
      
       
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            listBox1.Visible = true;
            listBox1.Items.Clear();
            String str=textBox1.Text.Trim();
            for (int i = 0; i < test.Count; i++) {
                if ( test[i].ToString().Contains(str)) {
                    listBox1.Items.Add(test[i].ToString());
               
                }
            }
            if (listBox1.Items.Count < 2)
            {
                listBox1.Hide();
            }
            else {
                listBox1.Height = listBox1.Items.Count * 14;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            test.Add("1");
            test.Add("12");
            test.Add("123");
            test.Add("123");
            test.Add("1234");
            test.Add("122");
            test.Add("12345");
            listBox1.Hide();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox1.Hide();
            textBox1.Text =listBox1.SelectedItem.ToString();
        }
    }
}








0 0
原创粉丝点击