110,536
社区成员
发帖
与我相关
我的任务
分享
#region 全局变量
/// <summary> 记录原始光标位置 </summary>
static int s = -1;
/// <summary> 字符长度添量 </summary>
static int Index = 0;
/// <summary> 过度字符串变量 </summary>
static string str = string.Empty;
/// <summary> 判断字符串是否更改 </summary>
static string strE = string.Empty;
/// <summary> 计算数量 </summary>
static int count = 0;
/// <summary> 实例化富文本 </summary>
static RichTextBox r = new RichTextBox();
#endregion
//窗口加载
private void Form1_Load(object sender, EventArgs e)
{
txtInput.ReadOnly = true;
string filePath = "myfile.txt";
FetchFile(filePath);
FindDown.Checked = true;
}
//加载文件 myfile.txt 我放在程序目录 (随意打的几个字母)
private void FetchFile(string path)
{
FileStream fs = new FileStream(path,
FileMode.Open, FileAccess.Read, FileShare.None);
StreamReader sr = new StreamReader(fs, Encoding.Default);
txtInput.Text = sr.ReadToEnd();
txtInput.Focus();
fs.Close();
sr.Close();
}
//查找 button
private void but_Find_Click(object sender, EventArgs e)
{
if (FindDown.Checked)
//fileFindDown(txtInput, txt_Count,txt_Find.Text);
fileFindDowns();
else
fileFindUp(txtInput, txt_Find.Text);
}
/// <summary> 类似 VS 的查找方法(不区分大小写向下查找) </summary>
/// <param name="txt">RichTextBox</param>
/// <param name="txtb">TextBox</param>
/// <param name="FindString">查找字符串</param>
private static void fileFindDown(RichTextBox txt,TextBox txtb, string FindString)
{
if (s < 0)
{
s = txt.SelectionStart;
if (s > 10 && s + FindString.Length <= txt.Text.Length)
{
str = txt.Text.Substring(0, s + FindString.Length - 1);
Index = txt.SelectionStart;
r.Text = str;
}
else
{
str = txt.Text;
r.Text = txt.Text;
Index = txt.SelectionStart;
}
}
if (Index < str.Length - (FindString.Length - 1) && Index >= 0)
{
Index = r.Find(FindString, Index, RichTextBoxFinds.None);
if (Index < 0 || Index > r.Text.Length)
{
seeks(FindString);
count = 0;
Index = 0;
s = -1;
}
else
{
txt.Select(Index, FindString.Length);
txt.SelectionColor = Color.Red;
txt.Focus();
count++;
txtb.Text = count.ToString();//
Index += FindString.Length;
if (Index > str.Length - FindString.Length)
{
seeks(FindString);
//txt.SelectionStart = 0;
//txt.Select(txt.SelectionStart, 0);
s = -1;
Index = 0;
count = 0;
}
}
}
else if (Index < txt.Text.Length && Index >= 0)
{
Index = txt.Find(FindString, Index, RichTextBoxFinds.None);
if (Index == -1)//(Index >= txt.Text.Length && Index <= 0)
{
if (r.Text != "")
{
Index = 0; Index = r.Find(FindString, Index, RichTextBoxFinds.None);
if (Index < 0 || Index > r.Text.Length)
{
seeks(FindString);
count = 0;
Index = 0;
s = -1;
}
else
{
txt.Select(Index, FindString.Length);
txt.SelectionColor = Color.Red;
txt.Focus();
Index += FindString.Length;
count++;
txtb.Text = count.ToString();
if (Index > str.Length - FindString.Length)
{
seeks(FindString);
//txt.SelectionStart = 0;
//txt.Select(txt.SelectionStart, 0);
s = -1;
Index = 0;
count = 0;
}
}
}
else
{
seeks(FindString);
s = -1;
Index = 0;
count = 0;
}
}
else
{
txt.Select(Index, FindString.Length);//在RichTextBox控件中搜索关键字
Index += FindString.Length;//递增标识查询关键字的初始长度
txt.SelectionColor = Color.Red;//设定关键字为红色
txt.Focus();
count++;
txtb.Text = count.ToString();
}
}
else
{
Index = 0; Index = r.Find(FindString, Index, RichTextBoxFinds.None);
if (Index < r.Text.Length - FindString.Length && Index >= 0)
{
r.Select(Index, FindString.Length);
txt.Select(Index, FindString.Length);
txt.SelectionColor = Color.Red;//设定关键字为红色
Index += FindString.Length;//递增标识查询关键字的初始长度
txt.Focus();
count++;
txtb.Text = count.ToString();
}
else
{
seeks(FindString);
s = -1;
Index = 0;
count = 0;
}
}
}
/* 简单查找方法,从上而下一直查找. */
private void fileFindDowns()
{
string FindStr = txt_Find.Text;
Index = txtInput.Find(FindStr, Index, RichTextBoxFinds.None);
if (Index < 0 || Index > txtInput.Text.Length)
{
seeks(FindStr);
start = 0;
Index = 0;
}
else
{
txtInput.Select(Index, FindStr.Length);
txtInput.SelectionBackColor = Color.Red;
Index += FindStr.Length;
txtInput.Focus();
count++;
}
}
public static void replaces(RichTextBox rtxt, string FindString, string ReplString)
{
//string txtSt = rtxt.Text.Substring(Index,FindString.Length);
string txtSt = rtxt.SelectedText;
if (FindString.Equals(txtSt))
{
int fs = FindString.Length;
int rs = ReplString.Length;
int cs = fs - rs;
rtxt.SelectionLength = FindString.Length;
rtxt.SelectionColor = Color.Coral;
rtxt.SelectedText = ReplString;//textBox2中放要替换的字符
rtxt.Focus();
MIndex = -1;
rtxt.SelectionStart += cs;
}
replFind(rtxt, FindString, ReplString);
}
int fs = FindString.Length;
int rs = ReplString.Length;
int cs = fs - rs;
rtxt.SelectionStart += cs;
public static void replaces(RichTextBox rtxt, string FindString, string ReplString)
{
//string txtSt = rtxt.Text.Substring(Index,FindString.Length);
string txtSt = rtxt.SelectedText;
if (FindString.Equals(txtSt))
{
int fs = FindString.Length;
int rs = ReplString.Length;
int cs = fs - rs;
rtxt.SelectionLength = FindString.Length;
rtxt.SelectionColor = Color.Coral;
rtxt.SelectedText = ReplString;//textBox2中放要替换的字符
rtxt.Focus();
MIndex = -1;
rtxt.SelectionStart += cs;
replFind(rtxt, FindString, ReplString);
}
else replFind(rtxt, FindString, ReplString);
}
虽然解决了,太还是达不到 VS 那种替换效果..........
//单次替换
public static void replaces(RichTextBox rtxt, string FindString, string ReplString)
{
string txtSt = rtxt.SelectedText;
if (FindString.Equals(txtSt))
{
rtxt.SelectionLength = FindString.Length;
rtxt.SelectionColor = Color.Coral;
rtxt.SelectedText = ReplString;//textBox2中放要替换的字符
rtxt.Focus();
s = -1;
fileFindDown(rtxt, FindString); //查找(那个TextBox 不要了,不是必须的)
}
else fileFindDown(rtxt, FindString); //查找
}
替换会遇到一个问题,
如: 查找(F) 替换(Ho~)
如果2个 F 之间不超出替换字符的 Length ,那么它就会跳过第二个F 查找第三个F,然后找到结尾再找第二个F.现在头脑有点乱乱的,想不出来了...请帮帮忙