C#記事本查詢與替換問題??
阿新 • • 發佈:2019-01-29
真心鬱悶,向上查詢兼替換行不通,有什麼辦法呢?再找找看,一定要找到向上替換的解決辦法!!
向上向下查詢好像都可以了吧,就是向上替換的時候就。...卡死就是後面還有一串字元是不在查詢內容的,可是就是這些內容讓人捉急了!!
不說了,貼程式碼。
inout1,inout2 存放查詢數量的。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Notepad { public partial class formFind : Form { public formFind() { InitializeComponent(); }
private void formFind_Load(object sender, EventArgs e) { text = ((Form1)this.Owner).RichrichTextBox1.Text; inout2 = text.Length; radioButton2.Checked = true; } string text = string.Empty; string f1 = string.Empty; public int a, b; public int inout1 = 0; public int inout2 = 0; //inout2 要放在Load賦值
private void button1_Click(object sender, EventArgs e) { if (checkBox1.Checked == false) //是否區分大小寫 { MatchSize();//呼叫小寫轉換 } if (radioButton2.Checked == true) { DownSeek();//呼叫向下查詢 } if (radioButton1.Checked == true)//向上查詢 { UpSeek();呼叫向上查詢 } }
private void button2_Click(object sender, EventArgs e)
{
replace();//呼叫替換
}
private void button3_Click(object sender, EventArgs e)
{
if (checkBox1.Checked == false) //是否區分大小寫
{
MatchSize();
}
//if (radioButton2.Checked == true) //向下查詢
//{
//Form1 f1 = (Form1)this.Owner;
//string f1_text = f1.RichTextBox1.Text;
inout1 = 0; a = 0;
a = text.IndexOf(textBox1.Text, inout1);
for (int i = 0; i < a; i++)
{
DownSeek();
replace();
}
}
//關閉視窗
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
MatchSize();
}
}
最主要的來了。。。看好 #region 自定義的--查詢----替換--
/// <summary> 區分大小寫
/// 轉換為小寫定義
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private void MatchSize()
{
Form1 f1 = (Form1)this.Owner;
string f1_text = f1.RichrichTextBox1.Text;
f1_text = f1_text.ToLower();//文字轉換為小寫
textBox1.Text = textBox1.Text.ToLower(); //查詢內容轉換小寫
//textBox2.Text = textBox2.Text.ToLower(); //替換字元轉換為小寫
}
/// <summary> -向下查詢-
/// 自定義向下查詢字串 -向下查詢-
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private void DownSeek()
{
b = text.Length;
inout2 = text.Length;
Form1 f1 = (Form1)this.Owner;
string f1_text = f1.RichrichTextBox1.Text;
//inout1 = 0;
a = f1_text.IndexOf(textBox1.Text, inout1); //向下查詢
if (a >= 0)
{
f1.RichrichTextBox1.Select(a, textBox1.Text.Length);
f1.RichrichTextBox1.Focus();
inout1 = ++a;
}
else
{
MessageBox.Show(this.Owner,
"查詢不到" + "\"" + textBox1.Text + "\"",
"-Mxdr記事本",
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk,
MessageBoxDefaultButton.Button1);
//return;
a = 0;
inout1 = 0;
}
}
/// <summary> --向上查詢--
/// 自定義向上查詢字串 --向上查詢--
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private void UpSeek()
{
a = 0;
inout1 = 0;
Form1 f1 = (Form1)this.Owner;
string f1_text = f1.RichrichTextBox1.Text;
//inout2 = 0;
b = f1_text.LastIndexOf(textBox1.Text, inout2);
if (b >= 0)
{
f1.RichrichTextBox1.Select(b, textBox1.Text.Length);
f1.RichrichTextBox1.Focus();
inout2 = --b;
}
else
{
MessageBox.Show(this.Owner,
"查詢不到" + "\"" + textBox1.Text + "\"",
"-Mxdr記事本",
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk,
MessageBoxDefaultButton.Button1);
//return;
b = text.Length;
inout2 = text.Length;
}
}
/// <summary> --替換--自定義
/// --替換-- 自定義替換字串
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private void replace()
{
Form1 f1 = (Form1)this.Owner;
if (f1.RichrichTextBox1.SelectedText.Length > 0)
f1.RichrichTextBox1.SelectedText = this.textBox2.Text;//textBox2中放要替換的字元
}
#endregion
}
}
。。。。。。。。。。。。
。。。。。。。。。。。。
就下面這段是沒辦法除錯通過的
private void button3_Click(object sender, EventArgs e)
{
if (checkBox1.Checked == false) //是否區分大小寫
{
MatchSize();
}
inout1 = 0; a = 0;
a = text.IndexOf(textBox1.Text, inout1);
for (int i = 0; i < a; i++)
{
DownSeek();
replace();
}
// b = f1_text.LastIndexOf(textBox1.Text, inout2);
//
// for (int i = 0; i < b; i++)
//{
// UpSeek();
// replace();
//}
}
有沒有高手解惑呢??