一个Textbox控件 一个按钮 listbox已连接ACCess数据库 如何操作点击botton ,利用循环判断texebox.text文本与listbox里面的某个值相等,得到listbox中该值的SelectedIndex=k,触发listBox2_SelectedIndexChanged事件,使得listbox选中SelectedIndex=k的值???大侠帮忙啦

解决方案 »

  1.   

    不需要触发listBox2_SelectedIndexChanged事件啊。。 也不用循环。
    if(listBox1.Items.Contains(textBox1.Text))
    {
    listBox1.Text=textBox1.Text;
    }
      

  2.   

    或者
    if(listBox1.Items.IndexOf(textBox1.Text)!=-1)
    {
    listBox1.SetSelected(listBox1.Items.IndexOf(textBox1.Text),true);
    }
      

  3.   

    循环的话:
    for(int i=0;i<listBox1.Items.Count;i++)
    {
    if(listBox1.Items[i].ToString().Equals(textBox1.Text))
    {
    listBox1.Text=textBox1.Text;
    }
    }
      

  4.   

    想触发事件的话:
    private void button2_Click(object sender, System.EventArgs e)
    {
    if(listBox1.Items.Contains(textBox1.Text))
    {
    listBox1.Text=textBox1.Text;
    }
    listBox1_SelectedIndexChanged(sender,e);
    }//这个是双击listBox自动生成的事。
    private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    this.textBox1.Text="is changed";
    }
      

  5.   

     private void button1_Click(object sender, EventArgs e)
            {
                if(this.textBox1.Text!="" && listBox1.Items.Count>0)
                {
                    for (int i = 0; i < listBox1.Items.Count;i++ )
                    {
                        if (textBox1.Text == listBox1.Items[i].ToString())
                        {
                            this.listBox1.SelectedIndex = i;
                            break;
                        }
                    }
                }            
            }