实验四真的是自己做的最墨迹的一次实验了,要求是做一个类似记事本,但功能不要求全部实现。
这是一张系统自带记事本的图
这是自己做出来的
这是自己做出来的记事本的图
实现的功能:1.文件部分:打开文件,保存文件,关闭文件(我查到用RichTextBox保存另存为啥的都一句话的事,但当时快交作业了懒得改了,没做另存为文件,后期再改进吧)
2.操作功能:剪切,复制,粘贴,查找,替换,加下划线,加粗,文字变斜体,更改字号,更改颜色,靠左对齐,居中对齐,靠右对齐,全选
1.打开文件
private void MOpen_Click(object sender, EventArgs e)
{
//这个按钮是用来打开文件的
DialogResult ans;
openFileDialog1.Filter = "所有*txt文件|*.txt";//筛选只要txt文件
openFileDialog1.InitialDirectory = "D://";//初始化打开路径
ans = openFileDialog1.ShowDialog();//打开文件
if (ans == DialogResult.OK)//如果用户点击了OK,就一个一个字节的将文本读入
{
richTextBox1.Text = " ";
fn = openFileDialog1.FileName;
FileStream fs = new FileStream(fn, FileMode.Open, FileAccess.ReadWrite);
long n = fs.Length;
byte[] b = new byte[n];
for (int i = 0; i < n; i++)//按字节读入
{
b[i] = Convert.ToByte(fs.ReadByte());
}
richTextBox1.Text = Encoding.Default.GetString(b);
fs.Close();
}
}
①要创建一个openFileDialog,注意这个组件的过滤器功能(Filter)和初始化功能(InitialDirectory)
②要注意窗口打开后的那个写法,就背下来就好if(ans=DialogResult.OK)
③文件流部分分三步,创建流,读/写文件,关闭流,缺一不可
④为了解决中文乱码问题需要转码,这个和java里用RandomAccessFile流是一个道理,这个也背下来就行
⑤c#里类型转换用的是Convert.
2.保存文件
private void MSave_Click(object sender, EventArgs e)
{
//这个按钮是用来保存文件的
if (fn == null)
MessageBox.Show("请先打开文件");
else
{
FileStream fs = new FileStream(fn, FileMode.Open, FileAccess.Write);
byte[] b = Encoding.Default.GetBytes(richTextBox1.Text);//把字符串编成字节
for (int i = 0; i < b.Length; i++)
fs.WriteByte(b[i]);
fs.Close();
}
}
逻辑:要想保存文件首先要有已经打开的文件,要是没有已经打开的文件要用MessageBox.Show来提示,然后创建写文件流来将文件写回去,这里也涉及到转码的问题,将文件内容转换到字节数组里,再一个一个字节的写回文件。最后关闭流。
3.退出按钮
private void MExit_Click(object sender, EventArgs e)
{
//这是退出按钮
//
this.Close();
}
直接将当前的窗口关闭了。
4.查找
private void MFind_Click(object sender, EventArgs e)
{
Form2 fr = new Form2(richTextBox1);
fr.ShowDialog();
}
public partial class Form2 : Form
{
private RichTextBox richTextBox2;
public Form2()
{
InitializeComponent();
}
public Form2(RichTextBox richTextBox1)
{
InitializeComponent();
richTextBox2 = richTextBox1;
}
private void Form2_Load(object sender, EventArgs e)
{
}
int start = 0;
int search = 0;//查询的个数
private void Button1_Click(object sender, EventArgs e)
{
int pos = richTextBox2.Text.IndexOf(textFind.Text,start);
DialogResult ans;
if (pos != -1)
{
richTextBox2.SelectionStart = pos;
richTextBox2.SelectionLength = textFind.Text.Length;
search++;
start = pos + textFind.Text.Length;
}
else
{
if(search==0)
{
ans=MessageBox.Show("没有查找到!");
}
else
{
ans=MessageBox.Show("已经查找到末尾!");
start = 0;
search = 0;
Focus();
}
}
}
private void Button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
①窗口跳转:首先要新建一个Form2,然后在Form1里面的按钮Click事件里创建一个Form2对象,把想传过去的参数传过去,然后显示Form2,在Form2里要新建一个你跟你传过来的参数一样的组件(比如这里传的是RichTextBox),然后新建一个初始化函数,把新组件和旧组件的内容弄一样了就可以了。
②查找,主要就是三个函数IndexOf,SelectionStart,SelectionLength,注意在那个Form1里要把HideSelection那个属性改成false,要不然你查找到的东西不会底色变蓝!(这个改了好久问同学才知道,老师明明讲过都怪自己不好好记笔记)
5.替换(替换的本质和查找差不多,找到了就换了呗)
private void MReplace_Click(object sender, EventArgs e)
{
Form3 fr = new Form3(richTextBox1);
fr.ShowDialog();
}
private RichTextBox richTextBox3;//新建一个该类中的要操作的对象
public Form3()//Form3的无参构造函数
{
InitializeComponent();
}
public Form3(RichTextBox richTextBox1)//Form3的有参构造函数,将第一个界面中的文本框作为参数传进来
{
InitializeComponent();
richTextBox3 = richTextBox1;
}
private void Button4_Click(object sender, EventArgs e)
{
this.Close();
}
int start = 0;
int search = 0;
private void Button1_Click(object sender, EventArgs e)
{
int pos = richTextBox3.Text.IndexOf(textBox1.Text,start);//从起始位置开始搜索
DialogResult ans;
if (pos != -1)
{
richTextBox3.SelectionStart = pos;
richTextBox3.SelectionLength = textBox1.Text.Length;
search++;
start = pos + textBox1.Text.Length;
}
else
{
if (search == 0)
{
ans = MessageBox.Show("没有查找到!");
}
else
{
if (search == 0)
{
ans = MessageBox.Show("没有查找到!");
}
else
{
ans = MessageBox.Show(string.Format("已经查找到末尾!共找到{0}个结果", search), "查找结果", MessageBoxButtons.OK, MessageBoxIcon.Warning);
if (ans == DialogResult.Yes)
{
search = 0;
start = 0;
Focus();
}
}
}
}
}
int start2 = 0;
private void Button2_Click(object sender, EventArgs e)
{
//这是替换按钮功能的实现
int pos = richTextBox3.Text.IndexOf(textBox2.Text, start2);
richTextBox3.SelectedText=textBox2.Text;
start2 = pos + textBox2.Text.Length;
}
int start1 = 0;
private void Button3_Click(object sender, EventArgs e)
{
//这是全部替换按钮功能的实现
int pos = richTextBox3.Text.IndexOf(textBox1.Text, start1);//从起始位置开始搜索
if (pos != -1)
{
richTextBox3.SelectionStart = pos;
richTextBox3.SelectionLength = textBox1.Text.Length;
search++;
start1 = pos + textBox1.Text.Length;
richTextBox3.Text = richTextBox3.Text.Replace(richTextBox3.SelectedText, textBox2.Text);
}
}
}
6.布局组件:最上面一栏叫MenuStrip,它下面那栏叫toolStrip
7.一种奇妙的颜色盘叫colorDialog
8.剩下的复制剪切粘贴就一句话的事,把函数记清。
完整代码如下
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace win_lib4_new
{
public partial class Form1 : Form
{
private string fn;
public Form1()
{
InitializeComponent();
}
private void ToolStripComboBox1_Click(object sender, EventArgs e)
{
}
private void 查看VToolStripMenuItem_Click(object sender, EventArgs e)
{
}
/*-----------文件按钮:打开保存文件,退出-----------*/
private void MOpen_Click(object sender, EventArgs e)
{
//这个按钮是用来打开文件的
DialogResult ans;
openFileDialog1.Filter = "所有*txt文件|*.txt";//筛选只要txt文件
openFileDialog1.InitialDirectory = "D://";//初始化打开路径
ans = openFileDialog1.ShowDialog();//打开文件
if (ans == DialogResult.OK)//如果用户点击了OK,就一个一个字节的将文本读入
{
richTextBox1.Text = " ";
fn = openFileDialog1.FileName;
FileStream fs = new FileStream(fn, FileMode.Open, FileAccess.ReadWrite);
long n = fs.Length;
byte[] b = new byte[n];
for (int i = 0; i < n; i++)//按字节读入
{
b[i] = Convert.ToByte(fs.ReadByte());
}
richTextBox1.Text = Encoding.Default.GetString(b);
fs.Close();
}
}
private void MOpen_2_Click(object sender, EventArgs e)
{
//这个按钮是用来打开文件的,和上一个按钮一样
DialogResult ans;
ans = openFileDialog1.ShowDialog();//打开文件
openFileDialog1.Filter = "所有*txt文件|*.txt";//筛选只要txt文件
openFileDialog1.InitialDirectory = "D://";//初始化打开路径
if (ans == DialogResult.OK)//如果用户点击了OK,就一个一个字节的将文本读入
{
richTextBox1.Text = " ";
fn = openFileDialog1.FileName;
FileStream fs = new FileStream(fn, FileMode.Open, FileAccess.ReadWrite);
long n = fs.Length;
byte[] b = new byte[n];
for (int i = 0; i < n; i++)//按字节读入
{
b[i] = Convert.ToByte(fs.ReadByte());
}
richTextBox1.Text = Encoding.Default.GetString(b);
fs.Close();
}
}
private void MSave_Click(object sender, EventArgs e)
{
//这个按钮是用来保存文件的
if (fn == null)
MessageBox.Show("请先打开文件");
else
{
FileStream fs = new FileStream(fn, FileMode.Open, FileAccess.Write);
byte[] b = Encoding.Default.GetBytes(richTextBox1.Text);//把字符串编成字节
for (int i = 0; i < b.Length; i++)
fs.WriteByte(b[i]);
fs.Close();
}
}
private void MSave_2_Click(object sender, EventArgs e)
{
//这个按钮是用来保存文件的,和上一个按钮一样
if (fn == null)
MessageBox.Show("请先打开文件");
else
{
FileStream fs = new FileStream(fn, FileMode.Open, FileAccess.Write);
byte[] b = Encoding.Default.GetBytes(richTextBox1.Text);//把字符串编成字节
for (int i = 0; i < b.Length; i++)
{
fs.WriteByte(b[i]);
}
fs.Close();
}
}
private void MCut_2_Click(object sender, EventArgs e)
{
//这是剪切按钮
richTextBox1.Cut();
}
private void MCopy_2_Click(object sender, EventArgs e)
{
//这是复制按钮
richTextBox1.Copy();
}
private void MPaste_2_Click(object sender, EventArgs e)
{
//这是黏贴按钮
richTextBox1.Paste();
}
private void MExit_Click(object sender, EventArgs e)
{
//这是退出按钮
//
this.Close();
}
private void MCut_Click(object sender, EventArgs e)
{
richTextBox1.Cut();
}
private void MCopy_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}
private void MPaste_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
private void MFont_Click(object sender, EventArgs e)
{
}
private void ToolStripMenuItem3_Click(object sender, EventArgs e)
{
//这是黑色菜单项对应的Click事件
richTextBox1.SelectionColor = Color.Black;
MBlack.Checked = true;
MMoreColor.Checked = false;
MRed.Checked = false;
MBlue.Checked = false;
MGreen.Checked = false;
MYellow.Checked = false;
}
bool un = false;
private void MUnderline_Click(object sender, EventArgs e)
{
//这个按钮是用来将字体加上下划线的
if (un == false)
{
richTextBox1.SelectionFont = new Font(this.Font.Name, this.Font.Size, FontStyle.Underline);
un = true;
}
else
{
richTextBox1.SelectionFont = new Font(this.Font.Name, this.Font.Size, FontStyle.Regular);
un = false;
}
}
bool it = false;
private void MItalic_Click(object sender, EventArgs e)
{
//这个按钮是用来将字体变成斜体的
if (it == false)
{
richTextBox1.SelectionFont = new Font(this.Font.Name, this.Font.Size, FontStyle.Italic);
it = true;
}
else
{
richTextBox1.SelectionFont = new Font(this.Font.Name, this.Font.Size, FontStyle.Regular);
it = false;
}
}
bool ov = false;
private void MOverstriking_Click(object sender, EventArgs e)
{
//这个按钮是用来将字体加粗的
if (ov == false)
{
richTextBox1.SelectionFont = new Font(this.Font.Name, this.Font.Size, FontStyle.Bold);
ov = true;
}
else
{
richTextBox1.SelectionFont = new Font(this.Font.Name, this.Font.Size, FontStyle.Regular);
ov = false;
}
}
private void 黑体ToolStripMenuItem_Click(object sender, EventArgs e)
{
//黑体菜单项对应的Click事件
richTextBox1.SelectionFont = new Font("黑体", richTextBox1.Font.Size);
MHei.Checked = true;
MSong.Checked = false;
MKai.Checked = false;
MLi.Checked = false;
}
private void MSong_Click(object sender, EventArgs e)
{
//宋体菜单项对应的Click事件
richTextBox1.SelectionFont = new Font("宋体", richTextBox1.Font.Size);
MSong.Checked = true;
MHei.Checked = false;
MKai.Checked = false;
MLi.Checked = false;
}
private void MKai_Click(object sender, EventArgs e)
{
//楷书菜单项对应的Click事件
richTextBox1.SelectionFont = new Font("楷书", richTextBox1.Font.Size);
MKai.Checked = true;
MHei.Checked = false;
MSong.Checked = false;
MLi.Checked = false;
}
private void MLi_Click(object sender, EventArgs e)
{
//隶书菜单项对应的Click事件
richTextBox1.SelectionFont = new Font("隶书", richTextBox1.Font.Size);
MLi.Checked = true;
MHei.Checked = false;
MKai.Checked = false;
MSong.Checked = false;
}
private void 更多颜色选择ToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult ans;
ans = colorDialog1.ShowDialog();
if (ans == DialogResult.OK)
{
richTextBox1.SelectionColor = colorDialog1.Color;
}
MMoreColor.Checked = true;
MBlack.Checked = false;
MRed.Checked = false;
MBlue.Checked = false;
MGreen.Checked = false;
MYellow.Checked = false;
}
private void MFind_Click(object sender, EventArgs e)
{
Form2 fr = new Form2(richTextBox1);
fr.ShowDialog();
}
private void MFile_Click(object sender, EventArgs e)
{
}
private void 帮助HToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void MReplace_Click(object sender, EventArgs e)
{
Form3 fr = new Form3(richTextBox1);
fr.ShowDialog();
}
private void MRed_Click(object sender, EventArgs e)
{
//这是红色菜单项对应的Click事件
richTextBox1.SelectionColor = Color.Red;
MRed.Checked = true;
MBlack.Checked = false;
MMoreColor.Checked = false;
MBlue.Checked = false;
MGreen.Checked = false;
MYellow.Checked = false;
}
private void MBlue_Click(object sender, EventArgs e)
{
//这是蓝色菜单项对应的Click事件
richTextBox1.SelectionColor = Color.Blue;
MBlue.Checked = true;
MRed.Checked = false;
MBlack.Checked = false;
MMoreColor.Checked = false;
MGreen.Checked = false;
MYellow.Checked = false;
}
private void MGreen_Click(object sender, EventArgs e)
{
//这是绿色菜单项对应的Click事件
richTextBox1.SelectionColor = Color.Green;
MGreen.Checked = true;
MRed.Checked = false;
MBlack.Checked = false;
MMoreColor.Checked = false;
MBlue.Checked = false;
MYellow.Checked = false;
}
private void MYellow_Click(object sender, EventArgs e)
{
//这是黄色菜单项对应的Click事件
richTextBox1.SelectionColor = Color.Yellow;
MYellow.Checked = true;
MRed.Checked = false;
MBlack.Checked = false;
MMoreColor.Checked = false;
MBlue.Checked = false;
MGreen.Checked = false;
}
private void ToolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (toolStripComboBox1.Text == "6")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 6) ;
else if (toolStripComboBox1.Text == "7")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 7);
else if (toolStripComboBox1.Text == "8")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 8);
else if (toolStripComboBox1.Text == "9")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 9);
else if (toolStripComboBox1.Text == "10")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 10);
else if (toolStripComboBox1.Text == "11")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 11);
else if (toolStripComboBox1.Text == "12")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 12);
else if (toolStripComboBox1.Text == "13")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 13);
else if (toolStripComboBox1.Text == "14")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 14);
else if (toolStripComboBox1.Text == "15")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 15);
else if (toolStripComboBox1.Text == "16")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 16);
else if (toolStripComboBox1.Text == "17")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 17);
else if (toolStripComboBox1.Text == "18")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 18);
else if (toolStripComboBox1.Text == "19")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 19);
else if (toolStripComboBox1.Text == "20")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 20);
else if (toolStripComboBox1.Text == "21")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 21);
else if (toolStripComboBox1.Text == "22")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 22);
else if (toolStripComboBox1.Text == "23")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 23);
else if (toolStripComboBox1.Text == "24")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 24);
else if (toolStripComboBox1.Text == "25")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 25);
else if (toolStripComboBox1.Text == "26")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 26);
else if (toolStripComboBox1.Text == "27")
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 27);
}
private void ToolStripComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (toolStripComboBox2.Text == "红色")
richTextBox1.SelectionColor = Color.Red;
else if (toolStripComboBox2.Text == "绿色")
richTextBox1.SelectionColor = Color.Green;
else if (toolStripComboBox2.Text == "黑色")
richTextBox1.SelectionColor = Color.Black;
else if (toolStripComboBox2.Text == "蓝色")
richTextBox1.SelectionColor = Color.Blue;
else if (toolStripComboBox2.Text == "黄色")
richTextBox1.SelectionColor = Color.Yellow;
else if (toolStripComboBox2.Text == "更多颜色选择")
{
DialogResult ans;
ans = colorDialog1.ShowDialog();
if (ans == DialogResult.OK)
{
richTextBox1.SelectionColor = colorDialog1.Color;
}
}
}
private void 小6磅ToolStripMenuItem_Click(object sender, EventArgs e)
{
//这是小菜单项对应的Click事件
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 6);
MSmall.Checked = true;
MMiddle.Checked = false;
MBig.Checked = false;
MSuperBig.Checked = false;
}
private void 中12磅ToolStripMenuItem_Click(object sender, EventArgs e)
{
//这是中菜单项对应的Click事件
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 12);
MMiddle.Checked = true;
MSmall.Checked = false;
MBig.Checked = false;
MSuperBig.Checked = false;
}
private void 大18磅ToolStripMenuItem_Click(object sender, EventArgs e)
{
//这是菜单项对应的Click事件
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 18);
MBig.Checked = true;
MSmall.Checked = false;
MMiddle.Checked = false;
MSuperBig.Checked = false;
}
private void 超大24磅ToolStripMenuItem_Click(object sender, EventArgs e)
{
//这是超大菜单项对应的Click事件
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 24);
MSuperBig.Checked = true;
MSmall.Checked = false;
MMiddle.Checked = false;
MBig.Checked = false;
}
//设置界面属性
private void setpos()
{
richTextBox1.Left = 0;
richTextBox1.Top = toolStrip1.Height + menuStrip1.Height;
richTextBox1.Width = this.ClientSize.Width;
richTextBox1.Height = this.ClientSize.Height - menuStrip1.Height - toolStrip1.Height;
}
private void Form1_Load(object sender, EventArgs e)
{
setpos();
}
private void Form1_Resize(object sender, EventArgs e)
{
setpos();
}
private void ToolStripButton1_Click(object sender, EventArgs e)
{
//全选按钮
richTextBox1.SelectAll();
}
private void MLeft_Click(object sender, EventArgs e)
{
richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
}
private void MCenter_Click(object sender, EventArgs e)
{
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
}
private void MRight_Click(object sender, EventArgs e)
{
richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
}
}
}
Form1视图
Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace win_lib4_new
{
public partial class Form2 : Form
{
private RichTextBox richTextBox2;
public Form2()
{
InitializeComponent();
}
public Form2(RichTextBox richTextBox1)
{
InitializeComponent();
richTextBox2 = richTextBox1;
}
private void Form2_Load(object sender, EventArgs e)
{
}
int start = 0;
int search = 0;//查询的个数
private void Button1_Click(object sender, EventArgs e)
{
int pos = richTextBox2.Text.IndexOf(textFind.Text,start);
DialogResult ans;
if (pos != -1)
{
richTextBox2.SelectionStart = pos;
richTextBox2.SelectionLength = textFind.Text.Length;
search++;
start = pos + textFind.Text.Length;
}
else
{
if(search==0)
{
ans=MessageBox.Show("没有查找到!");
}
else
{
ans=MessageBox.Show("已经查找到末尾!");
start = 0;
search = 0;
Focus();
}
}
}
private void Button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Form2的视图
Form3.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace win_lib4_new
{
public partial class Form3 : Form
{
private RichTextBox richTextBox3;//新建一个该类中的要操作的对象
public Form3()//Form3的无参构造函数
{
InitializeComponent();
}
public Form3(RichTextBox richTextBox1)//Form3的有参构造函数,将第一个界面中的文本框作为参数传进来
{
InitializeComponent();
richTextBox3 = richTextBox1;
}
private void Button4_Click(object sender, EventArgs e)
{
this.Close();
}
int start = 0;
int search = 0;
private void Button1_Click(object sender, EventArgs e)
{
int pos = richTextBox3.Text.IndexOf(textBox1.Text,start);//从起始位置开始搜索
DialogResult ans;
if (pos != -1)
{
richTextBox3.SelectionStart = pos;
richTextBox3.SelectionLength = textBox1.Text.Length;
search++;
start = pos + textBox1.Text.Length;
}
else
{
if (search == 0)
{
ans = MessageBox.Show("没有查找到!");
}
else
{
if (search == 0)
{
ans = MessageBox.Show("没有查找到!");
}
else
{
ans = MessageBox.Show(string.Format("已经查找到末尾!共找到{0}个结果", search), "查找结果", MessageBoxButtons.OK, MessageBoxIcon.Warning);
if (ans == DialogResult.Yes)
{
search = 0;
start = 0;
Focus();
}
}
}
}
}
int start2 = 0;
private void Button2_Click(object sender, EventArgs e)
{
//这是替换按钮功能的实现
int pos = richTextBox3.Text.IndexOf(textBox2.Text, start2);
richTextBox3.SelectedText=textBox2.Text;
start2 = pos + textBox2.Text.Length;
}
int start1 = 0;
private void Button3_Click(object sender, EventArgs e)
{
//这是全部替换按钮功能的实现
int pos = richTextBox3.Text.IndexOf(textBox1.Text, start1);//从起始位置开始搜索
if (pos != -1)
{
richTextBox3.SelectionStart = pos;
richTextBox3.SelectionLength = textBox1.Text.Length;
search++;
start1 = pos + textBox1.Text.Length;
richTextBox3.Text = richTextBox3.Text.Replace(richTextBox3.SelectedText, textBox2.Text);
}
}
}
}
Form3视图
另外注意把按钮的ToolTipText记得改了,这样鼠标放上去会显示提示,不改就会很丑。
ps.这个博客纯粹是自己想总结一下当成一个学习笔记用的,写的时候可能没有那么严谨,就是把自己当时的一些思考的过程,总结的东西,放到这里,没想到有人会看。万一有错或者方法比较笨,看到的也麻烦告诉我一下,感谢~(还是觉得根本不会有人看hhh)