上下按键寻找文本框中输入的匹配项

void txtDatumName_TextChanged(object sender, EventArgs e)

        {

           

            strDatumName = this.txtDatumName.Text.Trim();

            if (!string.IsNullOrEmpty(strDatumName))

            {

                foreach (ListViewItem item in this.datumCategoriesList.Items)

                {

                    if (item.SubItems[1].Text.Contains(strDatumName))

                    {

                        datumCategoriesList.Items[0].Selected = true;

                        item.Selected = true;

                        this.datumCategoriesList.EnsureVisible(item.Index);

                        break;

                    }

                    else

                    {

                        datumCategoriesList.SelectedItems.Clear();

                    }

                }

            }

            //文本框什么都不存在的时候就不会选中listview

            else

            {

                datumCategoriesList.SelectedItems.Clear();

            }

        }
this.txtDatumName.KeyDown+=txtDatumName_KeyDown;
//添加按向上向下箭头进行切换listview的事件

        private void txtDatumName_KeyDown(object sender, KeyEventArgs e)

        {

            //临时的listview对象

            ListViewItem itemCode = null;



            #region 文本框存在内容时才会执行

            if (e.KeyCode == Keys.Down)

            {

                if (itemCode != null)

                {

                    //循环去查找符合strDatumName内容要求的项,从临时的listview对象存储的这一项开始查找

                    for (int i = itemCode.Index; i < datumCategoriesList.Items.Count; i++)

                    {

                        //如果TXT文本框包含这一项

                        if (datumCategoriesList.Items[i].SubItems[1].Text.Contains(strDatumName))

                        {

                            //找距离当前一个最近的前一个项

                            if (datumCategoriesList.Items[i].Index > datumCategoriesList.SelectedItems[0].Index)

                            {

                                datumCategoriesList.Items[i].Selected = true;

                                this.datumCategoriesList.EnsureVisible(datumCategoriesList.Items[i].Index);

                                //当前选中行的值赋给一个临时的listview下次查找就从这个新的listview的位置开始查找

                                itemCode = datumCategoriesList.Items[datumCategoriesList.SelectedItems[0].Index];

                                break;

                            }

                        }

                        else

                        {

                            datumCategoriesList.Items[i].Selected = false;

                        }

                    }

                }

                else

                {

                    for (int i = 0; i < datumCategoriesList.Items.Count; i++)

                    {

                        if (datumCategoriesList.Items[i].SubItems[1].Text.Contains(strDatumName))

                        {

                            if (datumCategoriesList.Items[i].Index > datumCategoriesList.SelectedItems[0].Index)

                            {

                                datumCategoriesList.Items[i].Selected = true;

                                this.datumCategoriesList.EnsureVisible(datumCategoriesList.Items[i].Index);

                                //当前选中行的值赋给一个临时的listview下次查找就从这个新的listview的位置开始查找

                                itemCode = datumCategoriesList.Items[datumCategoriesList.SelectedItems[0].Index];

                                break;

                            }

                        }

                        else

                        {

                            datumCategoriesList.Items[i].Selected = false;

                        }

                    }

                }

            }

            else if (e.KeyCode == Keys.Up)

            {



                if (itemCode != null)

                {

                    for (int i = datumCategoriesList.Items.Count - 1; i >= itemCode.Index; i--)

                    {

                        //如果TXT文本框包含这一项

                        if (datumCategoriesList.Items[i].SubItems[1].Text.Contains(strDatumName))

                        {

                            //找距离当前一个最近的前一个项

                            if (datumCategoriesList.Items[i].Index < datumCategoriesList.SelectedItems[0].Index)

                            {

                                datumCategoriesList.Items[i].Selected = true;

                                this.datumCategoriesList.EnsureVisible(datumCategoriesList.Items[i].Index);

                                //当前选中行的值赋给一个临时的listview下次查找就从这个新的listview的位置开始查找

                                itemCode = datumCategoriesList.Items[datumCategoriesList.SelectedItems[0].Index];

                                break;

                            }

                        }

                        else

                        {

                            datumCategoriesList.Items[i].Selected = false;

                        }

                    }



                }

                else

                {

                    for (int i = datumCategoriesList.Items.Count - 1; i >= 0; i--)

                    {

                        //如果TXT文本框包含这一项

                        if (datumCategoriesList.Items[i].SubItems[1].Text.Contains(strDatumName))

                        {

                            //找距离当前一个最近的前一个项

                            if (datumCategoriesList.Items[i].Index < datumCategoriesList.SelectedItems[0].Index)

                            {

                                datumCategoriesList.Items[i].Selected = true;

                                this.datumCategoriesList.EnsureVisible(datumCategoriesList.Items[i].Index);

                                //当前选中行的值赋给一个临时的listview下次查找就从这个新的listview的位置开始查找

                                itemCode = datumCategoriesList.Items[datumCategoriesList.SelectedItems[0].Index];

                                break;

                            }

                        }

                        else

                        {

                            datumCategoriesList.Items[i].Selected = false;

                        }

                    }

                }

            }

            e.Handled = true; 

            #endregion

        }

 其中,datumCategoriesList.Items[i].SubItems[1].Text表示获取listview的选中行的值

你可能感兴趣的:(文本框)