2020-12-04 自己开发小软件对工作,加了个搜索医嘱名称,再显示颜色添加,回车索引跳行搜索,自用代码,很多模块,如果需要其他模块可以私聊

  private void txtYZName_TextChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab == tabControl1.TabPages["YZPage"])
            {
                if (SearchYZ == false)
                {
                    #region 恢复原先没颜色的状态
                    if (txtYZName.Text.Equals(""))//&& (DataGridView2.Rows.Count == DGV_YZ_Count)
                    {
                        // 恢复之前行的背景颜色为默认的白色背景
                        for (int i = 0; i < beforeMatchedRowIndex.Count; i++)
                        {
                            DataGridView2.Rows[beforeMatchedRowIndex[i]].DefaultCellStyle.BackColor = System.Drawing.Color.White;
                        }
                        return;
                    }

                    // 恢复之前行的背景颜色为默认的白色背景
                    for (int i = 0; i < beforeMatchedRowIndex.Count; i++)
                    {
                        DataGridView2.Rows[beforeMatchedRowIndex[i]].DefaultCellStyle.BackColor = System.Drawing.Color.White;
                    }
                }

                #endregion

                #region  添加状态
                //如果是刷新了搜索,如果是空字符,就退出
                if (txtYZName.Text.Equals(""))
                {
                    return;
                }
                // Linq模糊查询
                IEnumerable<DataGridViewRow> enumerableList = DataGridView2.Rows.Cast<DataGridViewRow>();
                Matchedlist = (from item in enumerableList
                               where item.Cells["YZMC"].Value.ToString().IndexOf(txtYZName.Text) >= 0
                               select item).ToList();

                if (Matchedlist.Count > 0)
                {
                    beforeMatchedRowIndex.Clear();
                    for (int i = 0; i < Matchedlist.Count; i++)
                    {
                        // 查找匹配行高亮显示
                        int matchedRowIndex = Matchedlist[i].Index;

                        DataGridView2.Rows[matchedRowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;

                        this.beforeMatchedRowIndex.Add(matchedRowIndex);
                    }
                }

                k = 1;
                SearchYZ = false;
                #endregion
            }
            else
            {
                MessageBox.Show("请先定位到医嘱表,再输入医嘱名称");
                return;
            }
        }

        private void txtYZName_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.KeyCode == Keys.Enter)
            //{
            //    // 恢复之前行的背景颜色为默认的白色背景
            //    if (k <= Matchedlist.Count)
            //    {
            //        if((k-2) >=0)//判断第一次不消颜色
            //        {
            //           DataGridView2.Rows[Matchedlist[k-2]].Selected = false;
            //        }
            //        DataGridView2.Rows[Matchedlist[k-1]].Selected = true; //加颜色
            //        DataGridView2.CurrentCell = DataGridView2.Rows[Matchedlist[k - 1]].Cells["YZMC"];//设置成当前选框
            //        if(k== Matchedlist.Count)//到了最大值
            //        {
            //            k++;
            //            return;
            //        }
            //        k++;
            //    }

            //    if (k > beforeMatchedRowIndex.Count)
            //    {
            //        DataGridView2.Rows[beforeMatchedRowIndex[k - 2]].Selected = false;
            //        k = 1;
            //    }
            //}


            if (e.KeyCode == Keys.Enter)
            {
                if (SearchYZ == false)
                {
                    if (txtYZName.Text != "")
                    {
                        // 恢复之前行的背景颜色为默认的白色背景
                        if (k <= Matchedlist.Count && Matchedlist.Count > 0)
                        {
                            //if ((k - 2) >= 0)//判断第一次不消颜色
                            //{
                            //    DataGridView2.Rows[Matchedlist[k - 2]].Selected = false;
                            //}
                            //DataGridView2.Rows[Matchedlist[k - 1]].Selected = true; //加颜色
                            DataGridView2.CurrentCell = Matchedlist[k - 1].Cells["YZMC"];//设置成当前选框
                            if (k == Matchedlist.Count)//到了最大值
                            {
                                k++;
                                return;
                            }
                            k++;
                        }

                        if (k > Matchedlist.Count)
                        {
                            //DataGridView2.Rows[beforeMatchedRowIndex[k - 2]].Selected = false;
                            k = 1;
                        }
                    }
                }
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值