文章目录
实验要求
- 增加状态栏,用于显示即时时间。
- 添加OpenFileDialog,当点击”打开”菜单项时,可以打开文本文件,添加SaveFileDialog,经编辑后点击“保存”菜单项,可以保存文件。
- 添加FontDialog控件和ColorDialog控件,通过”格式”菜单下的“字体”和“颜色”菜单项,可以设置草稿纸中文字的字体和背景色。
- 选择”新纸”菜单项时,若草稿纸中已经编辑新的内容,并且没有保存过,则弹出“是否保存”警告消息框,单击”Yes”控件,则打开保存对话框保存,否则不提示,并把草稿纸内容清空。
- 设计FindDialog和ReplaceDialog,如图所示。点击”编辑”菜单下的”查找”或者“替换”菜单项时以模式对话框的方式弹出,在RichTextBox控件中查找或替换相应的文本。
- 把草稿纸改为MDI窗体形式。中间的RichTextBox控件放在子窗口中。
定义全局变量
因为后续需判断草稿纸内容是否保存,所以需定义一个全局变量,用来判断
定义全局变量直接写在 public partial class Form1 : Form {}类里面就行
public partial class Form1 : Form
{
//定义全局变量
bool issave;
初始化窗口
在窗口的 public Form1() 方法中,可以初始化设置组件
public Form1()
{
InitializeComponent();
//初始化openFileDialog,设置打开文件的类型和窗口标题
openFileDialog1.Filter = "(*.txt)|*.txt";
openFileDialog1.Title = "选择打开的文本";
//初始化saveFileDialog,设置保存文件的类型和窗口标题
saveFileDialog2.Filter = "(*.txt)|*.txt";
saveFileDialog2.Title = "选择要保存的文件";
//初始化富文本框的大小
richTextBox1.Size = new Size(this.Size.Width - 18, this.Size.Height - 128);
//初始化计时器,使其一开始就展示当前时间,不需要等待一秒
toolStripStatusLabel1.Text = DateTime.Now.ToLongTimeString().ToString();
//初始化保存状态
issave = true;
//初始化文本框字体
richTextBox1.SelectionFont = new Font("楷体", 12, FontStyle.Regular);
}
如图
修改标题来判断是否已保存
richTextBox 中有个 TextChanged 方法,当文本有修改时,设置为未保存
//文本框内容有修改时,将issave设置为false
if (issave)
{
issave = false;
this.Text = "*" + this.Text;
}
//当未保存且文本框为空时,视为已保存状态
if (!issave && richTextBox1.Text == "")
{
issave = true;
this.Text = this.Text.Substring(1, this.Text.Length - 1);
}
状态栏显示时间
在窗体设计中添加timer,并将Interval属性设置为1000(一秒运行一次)
//计时器
toolStripStatusLabel1.Text = DateTime.Now.ToLongTimeString().ToString();
其它格式可参考:https://blog.youkuaiyun.com/zq9955/article/details/109239410
openFileDialog
DialogResult rt = openFileDialog1.ShowDialog();
if (rt == DialogResult.OK )
{
//获取所保存的文件名
string fName = openFileDialog1.FileName;
richTextBox1.LoadFile(fName, RichTextBoxStreamType.PlainText);
}
saveFileDialog
DialogResult dr = saveFileDialog2.ShowDialog();
//获取所保存的文件名
string filename = saveFileDialog2.FileName;
if (dr == DialogResult.OK && !string.IsNullOrEmpty(filename))
{
richTextBox1.SaveFile(filename, RichTextBoxStreamType.PlainText);
//当之前未被保存时
if (!issave)
{
issave = true;
//当保存的时候
this.Text = this.Text.Substring(1,this.Text.Length - 1);
}
}
新纸
//当保存时,直接清除
if (issave)
{
richTextBox1.Clear();
}
else//当为保存时,弹出提示框
{
if (MessageBox.Show("是否进行保存", "提示", MessageBoxButtons.YesNo) == DialogResult.OK)
{
DialogResult dr = saveFileDialog2.ShowDialog();
//获取所保存的文件名
string filename = saveFileDialog2.FileName;
if (dr == DialogResult.OK && !string.IsNullOrEmpty(filename))
{
richTextBox1.SaveFile(filename, RichTextBoxStreamType.PlainText);
//当之前未被保存时
if (!issave)
{
issave = true;
//当保存的时候
this.Text = this.Text.Substring(1,this.Text.Length - 1);
}
}
}
else
{
richTextBox1.Clear();
}
}
字体设置
DialogResult dr = fontDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
richTextBox1.Font = fontDialog1.Font;
}
字体颜色设置
DialogResult dr = colorDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
richTextBox1.ForeColor = colorDialog1.Color;
}
查找替换
子窗体设计
链接父窗体
在子窗体(Form2)中设置传入父窗体
在父窗体(Form1)中定义方法,向子窗体传入 richTextBox1
//向子窗体返回richtextbox
public RichTextBox RichTextBox
{
get
{
return this.richTextBox1;
}
}
在父窗体(Form1)打开子窗体
Form2 f2 = new Form2(this);
f2.Show();
定义子窗体的全局变量
在子窗体中定义全局变量
//index为查询开始的数字
int index = 0;
//sum为查询到的个数
int sum = 0;
查询
//获取父窗体中的richTextBox1
RichTextBox rbox = mainform1.RichTextBox;
string findtext = textBox1.Text;
int textlong = findtext.Length;
//查询 分为四种查询模式
if (checkBox1.Checked)
{
if (checkBox2.Checked)
{
int b = rbox.Find(findtext, index, RichTextBoxFinds.MatchCase);
int a = rbox.Find(findtext, index, RichTextBoxFinds.WholeWord);
while (a != b)
{
if (a == -1 || b == -1)
{
break;
}
else if (a > b)
{
index = b + textlong;
b = rbox.Find(findtext, index, RichTextBoxFinds.MatchCase);
}
else
{
index = a + textlong;
a = rbox.Find(findtext, index, RichTextBoxFinds.WholeWord);
}
}
if (a == -1 || b == -1)
{
index = -1;
}
else
{
index = a;
}
}
else
{
index = rbox.Find(findtext, index, RichTextBoxFinds.WholeWord);
}
}
else
{
if (checkBox2.Checked)
{
index = rbox.Find(findtext, index, RichTextBoxFinds.MatchCase);
}
else
{
index = rbox.Find(findtext, index, RichTextBoxFinds.None);
}
}
if (index >= 0)
{
rbox.SelectionStart = index;
rbox.SelectionLength = textlong;
rbox.Focus();
sum++;
}
else
{
if(sum == 0){
MessageBox.Show("未查询到对应元素", "提示", MessageBoxButtons.OK);
sum = 0;
}
else
{
}
}
index += textlong;
if (index > rbox.Text.Length)
{
index = 0;
}
替换
RichTextBox rbox = mainform1.RichTextBox;
string findtext = textBox1.Text;
int textlong = findtext.Length;
//是否全字匹配
if (checkBox1.Checked)
{
if (checkBox2.Checked)
{
int b = rbox.Find(findtext, index, RichTextBoxFinds.MatchCase);
int a = rbox.Find(findtext, index, RichTextBoxFinds.WholeWord);
while (a != b)
{
if (a == -1 || b == -1)
{
break;
}
else if (a > b)
{
index = b + textlong;
b = rbox.Find(findtext, index, RichTextBoxFinds.MatchCase);
}
else
{
index = a + textlong;
a = rbox.Find(findtext, index, RichTextBoxFinds.WholeWord);
}
}
if (a == -1 || b == -1)
{
index = -1;
}
else
{
index = a;
}
}
else
{
index = rbox.Find(findtext, index, RichTextBoxFinds.WholeWord);
}
}
else
{
if (checkBox2.Checked)
{
index = rbox.Find(findtext, index, RichTextBoxFinds.MatchCase);
}
else
{
index = rbox.Find(findtext, index, RichTextBoxFinds.None);
}
}
//MessageBox.Show("" + index, "提示", MessageBoxButtons.OK);//查看查询的位置
if (index >= 0)
{
rbox.SelectionStart = index;
rbox.SelectionLength = textlong;
//替换
rbox.SelectedText = textBox2.Text;
rbox.SelectionStart = index;
rbox.SelectionLength = textBox2.Text.Length;
//给焦点
rbox.Focus();
//累加
sum++;
}
else
{
if (sum == 0)
{
MessageBox.Show("未查询到对应元素", "提示", MessageBoxButtons.OK);
sum = 0;
}
else
{
MessageBox.Show(string.Format("已将所有\"{0}\"替换为\"{1}\"",findtext,textBox2.Text), "提示", MessageBoxButtons.OK);
sum = 0;
}
}
index += textBox2.Text.Length;
if(index > rbox.Text.Length){
index = 0;
}
全部替换
RichTextBox rbox = mainform1.RichTextBox;
string findtext = textBox1.Text;
int textlong = findtext.Length;
bool end = false;
while (!end) {
//是否全字匹配
if (checkBox1.Checked)
{
if (checkBox2.Checked)
{
int b = rbox.Find(findtext, index, RichTextBoxFinds.MatchCase);
int a = rbox.Find(findtext, index, RichTextBoxFinds.WholeWord);
while (a != b) {
if(a == -1 || b == -1)
{
break;
}
else if (a > b)
{
index = b + textlong;
b = rbox.Find(findtext, index, RichTextBoxFinds.MatchCase);
}
else
{
index = a + textlong;
a = rbox.Find(findtext, index, RichTextBoxFinds.WholeWord);
}
}
if(a == -1 || b == -1)
{
index = -1;
}
else
{
index = a;
}
}
else
{
index = rbox.Find(findtext, index, RichTextBoxFinds.WholeWord);
}
}
else
{
if (checkBox2.Checked)
{
index = rbox.Find(findtext, index, RichTextBoxFinds.MatchCase);
}
else
{
index = rbox.Find(findtext, index, RichTextBoxFinds.None);
}
}
if (index >= 0)
{
rbox.SelectionStart = index;
rbox.SelectionLength = textlong;
rbox.SelectedText = textBox2.Text;
rbox.SelectionStart = index;
rbox.SelectionLength = textBox2.Text.Length;
rbox.Focus();
sum++;
}
else
{
if (sum == 0)
{
MessageBox.Show("未查询到对应元素", "提示", MessageBoxButtons.OK);
sum = 0;
end = true;
}
else
{
MessageBox.Show(string.Format("已将所有\"{0}\"替换为\"{1}\"", findtext, textBox2.Text), "提示", MessageBoxButtons.OK);
sum = 0;
end = true;
}
}
index += textBox2.Text.Length;
}
注意:当文本框有变化时
private void textBox1_TextChanged(object sender, EventArgs e)
{
//查询的文本框内容变化时,从头开始查找
index = 0;
}
草稿纸改为MDI窗体形式(源码链接)
因为这需要改动的比较多,所以直接上源代码。
仓库链接