日期:2014-05-18  浏览次数:20699 次

有关ComboBox的问题?
在winform上,我想用ComboBox来实现类似于注册某个网站时,当你输入邮箱时,下面会出现你可能输入的邮箱地址,就好比注册新浪微博时,输入邮箱的那种效果。本来用TextBox实现了,但组长让我用ComboBox来实现,想了好久也没个所以然,哪个高手帮帮忙···

------解决方案--------------------
这样设置comboBox1
this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems; //设置自动完成的源 
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //设置自动完成的的形式 

参考http://www.cnblogs.com/huangfr/archive/2011/09/07/2170387.html
------解决方案--------------------
http://blog.csdn.net/happy09li/article/details/7083287
------解决方案--------------------
你的代码修改如下:
C# code
List<string> l;
        ListBox list = new ListBox();
        Dictionary<int, string> dic = new Dictionary<int, string>();

        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            list.Items.Clear();
            this.Controls.Add(list);
            Point p = new Point();
            p = comboBox1.Location;
            list.Location = new Point(p.X, p.Y + 21);
            list.Size = new Size(134, 80);
            list.HorizontalScrollbar = true;
            list.Enabled = true;
            list.Items.Add("@qq.com");
            list.Items.Add("@163.com");
            list.Items.Add("@126.com");
            list.Items.Add("@yahoo.com");
            list.Items.Add("@sina.com");
            list.Items.Add("@msn.com");
            if (comboBox1.Text.Contains(".com"))
            {
                this.Controls.Remove(list);
                return;
            }
            if (comboBox1.Text == "")
            {
                this.Controls.Remove(list);
            }
            else
            {
                for (int i = 0; i < list.Items.Count; i++)
                {
                    string email = comboBox1.Text;
                    if (email.Contains("@"))
                    {
                        for (int n = 0; n < list.Items.Count; n++)
                        {
                            list.Items[n] = dic[n];
                        }
                        int sum = email.IndexOf("@", 0);
                        if (comboBox1.Controls.Count - 1 > sum)
                        {
                            if (comboBox1.Controls.Count - sum == 2)
                            {
                                ShowInfo(email, sum, 1);
                            }
                            else if (comboBox1.Controls.Count - sum == 3)
                            {
                                ShowInfo(email, sum, 2);
                            }
                            else
                            {
                                list.Items.Clear();
                                for (int k = 0; k < l.Count; k++)
                                {
                                    list.Items.Add(l[k]);
                                }
                            }
                        }
                        break;
                    }
                    list.Items[i] = email + list.Items[i];
                    if (dic.ContainsKey(i))
                    {
                        dic[i] = list.Items[i].ToString();
                    }
                    else
                    {
                        dic.Add(i, list.Items[i].ToString());
                    }
                }
            }
            list.MouseDoubleClick += new MouseEventHandler(list_MouseDoubleClick);

        }
        private void ShowInfo(string email, int sum, int i)
        {
            string s = email.Substring(sum + i, 1);
            l = new List<string>();
            for (int m = 0; m < list.Items.Count; m++)