我做的是WinForm C/S程序
我的窗体上有个ComboBox控件 我是通过后台代码绑定的数据 
现在我想实现这样一个效果 就是 如果我随便输入一个值 然后跟ComboBox里面的Items的值 进行匹配 然后又相同的 就使ComboBox里面的这个值 为默认选中项 我试了好多种方法 但是都没实现 希望那位达人 能够帮忙解决下这个问题 很急的我

解决方案 »

  1.   

       private void button1_Click(object sender, EventArgs e)
             {
                 for (int i = 0; i < comboBox1.Items.Count; i++)
                 {
                     if (comboBox1.Items[i].ToString() == textBox1.Text)
                     {
                         comboBox1.SelectedIndex = i;
                         return;
                     }
                 }
             }
      

  2.   

    其实我是在DataGridView中的CellClick事件里做的单击事件private void DgdvUser_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                this.lbl_ID.Text=this.DgdvUser.Rows[e.RowIndex].Cells[0].Value.ToString();
                string Group = this.DgdvUser.Rows[e.RowIndex].Cells[1].Value.ToString();
                for (int i = 0; i < Cbo_Group.Items.Count; i++)
                {
                    if (Cbo_Group.Items[i].ToString() == Group)
                    {
                        this.Cbo_Group.SelectedIndex = i;
                        string kl=Group;
                    }
                }
            }你看我写的 这是我测试的时候写的
      

  3.   

    private void DgdvUser_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                this.lbl_ID.Text=this.DgdvUser.Rows[e.RowIndex].Cells[0].Value.ToString();
                string Group = this.DgdvUser.Rows[e.RowIndex].Cells[1].Value.ToString();
                for (int i = 0; i < Cbo_Group.Items.Count; i++)
                {
                    if (Cbo_Group.Items[i].ToString() == Group)
                    {
                        this.Cbo_Group.SelectedIndex = i;
                        string kl=Group;
                        break;  // 加上这行代码,如果还不行,请使用MessageBox.Show(Group); 查看Group的值
                    }
                }
            }
      

  4.   

    我试了 不行 在FOR循环期间我的Group变量一值是有值的
      

  5.   

    那问题就是出现在if (Cbo_Group.Items[i].ToString() == Group)这行代码了??
    使用MessageBox.Show(comboBox1.Items[i].ToString().Length.ToString() + "   " + Group.Length.ToString()) 
    查看他们的长度是否一样
      

  6.   

    我是让我输入的内容跟我ComboBox集合里面的值对比啦 又相同的就直接吧集合里面那个值给设置层默认选中项
      

  7.   


    Cbo_Group的每一项都比较过了吗?长度不一样?
      

  8.   

    出什么错误也不说一下
    用SelectedValue不行吗
      

  9.   

    我自己用的代码,不知道是不是你想要的。using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;namespace CStest
    {
        class ListSort
        {
            public Dictionary<string, string> SortByID(AreaList[] arealist, string strFind)
            {
                Dictionary<string, string> Dic = new Dictionary<string, string>();            ArrayList al = new ArrayList();
                for (int i = 0; i < arealist.Length; i++)
                {
                    al.Add(arealist[i].ID);
                }
                al.Sort();
                foreach (object s in al)
                {
                    for (int i = 0; i < arealist.Length; i++)
                    {
                        if (s.ToString().Contains(strFind) && s.ToString().CompareTo(strFind) >= 0 && s.ToString() == arealist[i].ID)
                        {
                            Dic.Add(arealist[i].ID, arealist[i].Name);
                            break;
                        }
                    }
                }
                return Dic;
            }        public Dictionary<string, string> SortByName(AreaList[] arealist, string strFind)
            {
                Dictionary<string, string> Dic = new Dictionary<string, string>();
                string[] tempList = new string[arealist.Length];
                ArrayList al = new ArrayList();
                for (int i = 0; i < arealist.Length; i++)
                {
                    al.Add(arealist[i].Name);
                }
                al.Sort();
                foreach (object s in al)
                {
                    for (int i = 0; i < arealist.Length; i++)
                    {
                        if (s.ToString().Contains(strFind) && s.ToString().CompareTo(strFind) >= 0 && arealist[i].Name == s.ToString())
                        {
                            Dic.Add(arealist[i].Name, arealist[i].ID);
                            break;
                        }
                    }
                }
                return Dic;
            }
        }    public struct AreaList
        {
            public string ID;
            public string Name;
        }    public unsafe struct AreaListTemp
        {
            public int ID;
            public char* Name;
        }    unsafe class ListSortTemp
        {
            
        }
    }        private void FillListView(AreaList[] arealist)
            {
                listBox1.Items.Clear();
                Regex r = new Regex("[^\x00-\xff]");
                if (r.IsMatch(Form1.KeyWords))
                {
                    Dictionary<string, string> Dic = ls.SortByName(arealist, Form1.KeyWords);
                    if (Dic.Count < 1)
                    {
                        Hide();
                        return;
                    }
                    else
                    {
                        NameList = new string[Dic.Count];
                        Show();
                    }
                    int i = 0;
                    foreach (KeyValuePair<string, string> kv in Dic)
                    {
                        NameList[i] = kv.Key;
                        listBox1.Items.Add(kv.Key + "(" + kv.Value + ")");
                        i++;
                    }
                    listBox1.SelectedIndex = 0;
                }
                else
                {
                    Dictionary<string, string> Dic = ls.SortByID(arealist, Form1.KeyWords);
                    if (Dic.Count < 1)
                    {
                        Hide();
                        return;
                    }
                    else
                    {
                        NameList = new string[Dic.Count];
                        Show();
                    }
                    int i = 0;
                    foreach (KeyValuePair<string, string> kv in Dic)
                    {
                        NameList[i] = kv.Value;
                        listBox1.Items.Add(kv.Key + "(" + kv.Value + ")");
                        i++;
                        
                    }
                    listBox1.SelectedIndex = 0;
                }
                this.Invalidate();
                //if (listBox1.Items.Count < 1)
                //{
                //    IsShow = false;
                //}
            }现在就是有一个问题,数据上1000就感觉跑不动了,而c++做的类似的控件数量可以达到1000W还感觉不到慢,
      

  10.   

    我这个只是把ComBox换成了ListBox,在一个文件本框里输入内容,然后在ListBox里列出相似的选项。应该和你说的功能差不多吧
      

  11.   

    错了  你理解错了我的意思
    我要的效果是这样的:我的ComboBox通过后台绑定数据  然后我通过其他地方获得了一个值 然后吧这个值跟我ComboBox里面的集合进行匹配如果存在相同的值 那就把ComboBox里的这个值给设置成选中状态 按我原来的想法是 循环遍历ComboBox里面的Items来进行匹配值但是我直接试了好多种方法 就是没实现这个效果 
      

  12.   

    呵呵 你这样肯定不行的已经被我同事给解决了这个问题 答案是:
    foreach (object bj in comboBox1.Items)
                {
                   DataRowView dv =(DataRowView) bj;
                   comboBox1.SelectedItem = dv;
                    string uid=dv.Row["userid"].ToString();
                   if ( uid== aa)
                   {
                       comboBox1.SelectedItem = bj;
                       break;
                   }
                }