标签:dsc rtb 显示 void als cti == pre sele
功能: 逐个匹配出文本中所需的字符串,并高亮显示
public class SchNxter { #region #region par Color fntClr; Color bakClr; Color sltClr = Color.Pink; Color sltBakClr = Color.Azure; int mchIdx; int schLen; int txtLen = 0; string txt; string sch; string tmp; bool mched = false; char chri, chrj; List<int> mchIdxL = new List<int>(); int mchCnt = 0; RichTextBox rtb = null; bool endSch = false; #endregion #region /// <summary> /// /// </summary> /// <param name="sltClr">选中前景色</param> /// <param name="sltBakClr">选中背景色</param> public SchNxter(Color sltClr, Color sltBakClr) { this.sltClr = sltClr; this.sltBakClr = sltBakClr; } /// <summary> /// 逐个向下搜索 /// </summary> /// <param name="rtb"></param> /// <param name="sch">要查询的字符串</param> /// <param name="schEnd">是否已查询至结尾</param> public void SchNxt(RichTextBox rtb, string sch, out int mchingCnt, out bool schEnd) { schEnd = false; mchingCnt = 0; if (sch == "") return; if (sch != this.sch || rtb.TextLength != txtLen) { BakSltClr(rtb); mched = false; endSch = false; } else if (this.rtb.Name != rtb.Name) { BakSltClr(this.rtb); mched = false; endSch = false; } else if (rtb.Text != txt) { BakSltClr(rtb); mched = false; endSch = false; } else if (endSch) { if (this.sch == sch) { schEnd = true; return; } endSch = false; } if (!mched) IniClr(rtb, sch); SchNxt(rtb,out mchingCnt, out schEnd); } #endregion #region /// <summary> /// 初始化颜色,参数... /// </summary> /// <param name="rtb"></param> /// <param name="sch"></param> private void IniClr(RichTextBox rtb, string sch) { mchIdx = 0; fntClr = rtb.ForeColor; bakClr = rtb.BackColor; schLen = sch.Length; txt = rtb.Text; txtLen = txt.Length; rtb.SelectionBackColor = sltBakClr; rtb.SelectionColor = sltClr; //MessageBox.Show(mchIdx + "," + rtb.SelectionColor.Name + "," + rtb.SelectionBackColor.Name); this.sch = sch; this.rtb = rtb; } private void SchNxt(RichTextBox rtb, out int mchingCnt, out bool schEnd) { schEnd = false; mchingCnt = 0; tmp = ""; int j = 0; chrj = sch[j]; int i = mchIdx; //MessageBox.Show(mchIdx + "," + rtb.SelectionColor.Name + "," + rtb.SelectionBackColor.Name); for (; i < txtLen; i++) { chri = txt[i]; if (chrj == chri) { tmp += chri; j++; if (j == schLen) { i++; i -= j; rtb.Select(i, j); rtb.ScrollToCaret(); rtb.SelectionBackColor = sltBakClr; rtb.SelectionColor = sltClr; //_mchIdx = i; mchIdxL.Add(i); mchCnt++; mchingCnt=mchCnt; mchIdx = i + j; tmp = ""; j = 0; mched = true; return; } else chrj = sch[j]; } else { if (j > 0) { i++; i -= j; tmp = ""; j = 0; chrj = sch[j]; } } } mchIdx = i; mched = false; schEnd = true; endSch = true; } /// <summary> /// 还原之前选中的项字色 /// </summary> public void BakSltClr() { if (mchCnt < 1) return; BakSltClr(this.rtb); } /// <summary> /// 还原之前选中的项字色 /// </summary> /// <param name="rtb"></param> private void BakSltClr(RichTextBox rtb) { if (mchCnt < 1) return; for (int i = 0; i < mchCnt; i++) { mchIdx = mchIdxL[i]; rtb.Select(mchIdx, schLen); rtb.SelectionBackColor = bakClr; rtb.SelectionColor = fntClr; } rtb.Select(0, 0); mchIdxL.Clear(); mchCnt = 0; } #endregion #endregion }
交流 QQ : 2412366909@qq.com
手机号码:177-7499-4428
注:本人使用过程中暂未发现造成存取不正确的问题
标签:dsc rtb 显示 void als cti == pre sele
原文地址:https://www.cnblogs.com/grCod/p/14763872.html