实验目的:
掌握常用控件的使用方法
掌握菜单、工具栏和状态栏的使用方法
掌握常用对话框窗体的用法
掌握窗体间传递数据的方法
掌握MDI窗体的设计方法
实验内容:
设计“草稿纸”窗体,外观如图所示,工具栏按钮包括有:新纸、页面、打印、剪切、复制、粘贴、左对齐、右对齐和加粗。 菜单项如下表所示。
(1)增加状态栏,用于显示即时时间
private void timer1_Tick(object sender, EventArgs e)
{
this.toolStripStatusLabel1.Text = "系统当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
}
(2) 添加OpenFileDialog,当点击”打开”菜单项时,可以打开文本文件,添加SaveFileDialog,经编辑后点击“保存”菜单项,可以保存文件
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "文本文件 (*.txt) |*.txt|富文本 (*.rtf)|*.rtf|全部文件 (*.*)|*.*";//设置可以支持打开的文件类型
open.Title = "打开";
if (open.ShowDialog() == DialogResult.OK)
{
string filePath = open.FileName;
System.IO.StreamReader sReader = new System.IO.StreamReader(filePath, Encoding.Default);
form3.richTextBox1.Text = sReader.ReadToEnd();
sReader.Dispose();
}
open.Dispose();
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
if (MessageBox.Show("是否保存?", "温馨提示", MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
SaveFileDialog save = new SaveFileDialog();
save.Title = "保存文件";
save.Filter = "文本文件 (*.txt)|*.txt|全部文件 (*.*)|*.*";
if (save.ShowDialog() == DialogResult.OK)
{
string filePath = save.FileName;
form3.richTextBox1.SaveFile(filePath, RichTextBoxStreamType.PlainText);
}
}
else
{
Close();
}
}
(3) 添加FontDialog控件和ColorDialog控件,通过”格式”菜单下的“字体”和“颜色”菜单项,可以设置草稿纸中文字的字体和背景色。
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
if (fontDialog1.ShowDialog() == DialogResult.OK)
form3.richTextBox1.SelectionFont = fontDialog1.Font;
}
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
if (colorDialog1.ShowDialog() == DialogResult.OK)
form3.richTextBox1.BackColor = colorDialog1.Color;
}
(4) 选择”新纸”菜单项时,若草稿纸中已经编辑新的内容,并且没有保存过,则弹出“是否保存”警告消息框,单击”Yes”控件,则打开保存对话框保存,否则不提示,并把草稿纸内容清空。
private void 新纸ToolStripMenuItem_Click(object sender, EventArgs e)
{
NewFile();
}
private void NewFile()
{
form3 = new Form3();
form3.MdiParent = this;//设置子窗体的父文档
form3.Text = "文档" + documentCount++;//文档号
form3.Show();//显示子窗体
//form3.isSaved = false;
}
(5) 设计FindDialog和ReplaceDialog,如图所示。点击”编辑”菜单下的”查找”或者“替换”菜单项时以模式对话框的方式弹出,在RichTextBox控件中查找或替换相应的文本。
private void 查找与替换ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
Form2 form2 = new Form2(form3);
form2.Show();
}
(6) 把草稿纸改为MDI窗体形式。中间的RichTextBox控件放在子窗口中。
实验代码:
From1.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 FormDesign
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Form3 form3;
int documentCount = 1;//文档计数
private void 新纸ToolStripMenuItem_Click(object sender, EventArgs e)
{
NewFile();
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "文本文件 (*.txt) |*.txt|富文本 (*.rtf)|*.rtf|全部文件 (*.*)|*.*";//设置可以支持打开的文件类型
open.Title = "打开";
if (open.ShowDialog() == DialogResult.OK)
{
string filePath = open.FileName;
System.IO.StreamReader sReader = new System.IO.StreamReader(filePath, Encoding.Default);
form3.richTextBox1.Text = sReader.ReadToEnd();
sReader.Dispose();
}
open.Dispose();
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
if (MessageBox.Show("是否保存?", "温馨提示", MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
SaveFileDialog save = new SaveFileDialog();
save.Title = "保存文件";
save.Filter = "文本文件 (*.txt)|*.txt|全部文件 (*.*)|*.*";
if (save.ShowDialog() == DialogResult.OK)
{
string filePath = save.FileName;
form3.richTextBox1.SaveFile(filePath, RichTextBoxStreamType.PlainText);
}
}
else
{
Close();
}
}
private void 页面ToolStripMenuItem_Click(object sender, EventArgs e)
{
pageSetupDialog1.Document = printDocument1;
try
{
pageSetupDialog1.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印出错");
}
}
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
try
{
printDocument1.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印出错");
}
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
if (form3.richTextBox1.CanUndo)
{
form3.richTextBox1.Undo();//撤回
}
}
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
form3.richTextBox1.Cut();//剪切
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
form3.richTextBox1.Copy();//复制
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
this.Cursor = Cursors.WaitCursor;
form3.richTextBox1.Paste();
this.Cursor = Cursors.Default;
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
form3.richTextBox1.Clear();//清空
}
private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
form3.richTextBox1.SelectAll();//全选
}
private void 转到ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 换行ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
if (form3.richTextBox1.WordWrap == true)
{
form3.richTextBox1.WordWrap = false;
}
else
{
form3.richTextBox1.WordWrap = true;
}
}
private void 主题ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("暂时无");
}
private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("by 源", "关于'草稿纸'");
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "文本文件 (*.txt) |*.txt|富文本 (*.rtf)|*.rtf|全部文件 (*.*)|*.*";//设置可以支持打开的文件类型
open.Title = "打开";
if (open.ShowDialog() == DialogResult.OK)
{
string filePath = open.FileName;
System.IO.StreamReader sReader = new System.IO.StreamReader(filePath, Encoding.Default);
form3.richTextBox1.Text = sReader.ReadToEnd();
sReader.Dispose();
}
open.Dispose();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
if (MessageBox.Show("是否保存?", "温馨提示", MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
SaveFileDialog save = new SaveFileDialog();
save.Title = "保存文件";
save.Filter = "文本文件 (*.txt)|*.txt|全部文件 (*.*)|*.*";
if (save.ShowDialog() == DialogResult.OK)
{
string filePath = save.FileName;
form3.richTextBox1.SaveFile(filePath, RichTextBoxStreamType.PlainText);
}
}
else
{
Close();
}
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
form3.richTextBox1.Copy();//复制
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
form3.richTextBox1.Cut();//剪切
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
form3.richTextBox1.SelectAll();//全选
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
form3.richTextBox1.Clear();//清空
}
private void toolStripButton7_Click(object sender, EventArgs e)//粘贴
{
form3 = (Form3)this.ActiveMdiChild;
this.Cursor = Cursors.WaitCursor;
form3.richTextBox1.Paste();
this.Cursor = Cursors.Default;
}
private void toolStripButton8_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
if (form3.richTextBox1.CanUndo)
{
form3.richTextBox1.Undo();//撤回
}
}
private void toolStripButton9_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
try
{
printDocument1.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印出错");
}
}
}
private void toolStripButton10_Click(object sender, EventArgs e)//预览
{
printPreviewDialog1.Document = printDocument1;
try
{
printPreviewDialog1.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印出错");
}
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
if (fontDialog1.ShowDialog() == DialogResult.OK)
form3.richTextBox1.SelectionFont = fontDialog1.Font;
}
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
if (colorDialog1.ShowDialog() == DialogResult.OK)
form3.richTextBox1.BackColor = colorDialog1.Color;
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
Graphics g = e.Graphics;
string[] str = form3.richTextBox1.Text.Split('\n');
int i = 0;
foreach (string s in str)
{
g.DrawString(str[i], fontDialog1.Font, new SolidBrush(form3.richTextBox1.ForeColor),
new PointF(100, 80 + form3.richTextBox1.Font.Height * i));
i++;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
this.toolStripStatusLabel1.Text = "系统当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
}
private void 日期ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("系统当日期为:" + DateTime.Now.ToString("yyyy-MM-dd "), "实时日期");
}
private void 时间ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("系统当时间为:" + DateTime.Now.ToString(" hh:mm:ss"), "实时时间");
}
//相应的查找子菜单
private void 查找与替换ToolStripMenuItem_Click(object sender, EventArgs e)
{
form3 = (Form3)this.ActiveMdiChild;
Form2 form2 = new Form2(form3);
form2.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.MdiParent = this;
f3.Show();
}
//新纸
private void NewFile()
{
form3 = new Form3();
form3.MdiParent = this;//设置子窗体的父文档
form3.Text = "文档" + documentCount++;//文档号
form3.Show();//显示子窗体
//form3.isSaved = false;
}
}
}
From2.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 FormDesign
{
public partial class Form2 : Form
{
Form3 parentForm; //定义包含富文本框的窗体变量
public Form2(Form3 parent)//Form1 form1
{
InitializeComponent();
this.parentForm = parent;
}
public int len1 = 0;
public int len2 = 0;
public int findlen = 0;
public int flag = 0;
public bool initfindindex = true;
private void Form2_Load(object sender, EventArgs e)
{
radioButton2.Checked = true;//默认开启向下查找功能
}
private void button1_Click_1(object sender, EventArgs e)
{
Form3 mainfrom3 = new Form3();
RichTextBox rbox = parentForm.richTextBox1;
string str = this.textBox1.Text;
if (initfindindex)//变量initfindindex判断是否重新输入搜索关键字,或者选择不同查找方式
{
len1 = rbox.SelectionStart;//获取光标所在文本位置,在该位置向上或向下搜索
len2 = rbox.SelectionStart;
initfindindex = false;
}
//rbox.Focus();//控件获取焦点
if (radioButton2.Checked)
{
rbox.Focus();//控件获取焦点
if (this.checkBox1.Checked)//是否按下区分大小写
{
findlen = rbox.Find(str, len1, RichTextBoxFinds.MatchCase);//区分大小写查找
}
else
{
findlen = rbox.Find(str, len1, RichTextBoxFinds.None);//不区分大小写并且不全字匹配查找
}
if (findlen < 0)//查找失败
{
if (len1 == rbox.SelectionStart)
{
if (this.checkBox1.Checked)//区分大小写是否按下
{
findlen = rbox.Find(str, 0, RichTextBoxFinds.MatchCase);//区分大小写查找
}
else
{
findlen = rbox.Find(str, 0, RichTextBoxFinds.None);//不区分大小写并且不全字匹配查找
}
if (findlen < 0)
{
len1 = 0;//长度清零
MessageBox.Show("未找到相关内容!");
}
else
{
len1 = 0;//长度清零
//MessageBox.Show("查找达到了搜索的起始点!");
len1 = findlen + str.Length;
}
}
else
{
if (rbox.TextLength != 0)//myGreat_Great_Great_Grandson.richTextBox1是查找内容超文本控件
{
len1 = 0;//长度清零
//MessageBox.Show("查找达到了搜索的起始点!");
if (this.checkBox1.Checked)//区分大小写单选框是否按下
{
findlen = rbox.Find(str, 0, RichTextBoxFinds.MatchCase);//区分大小写查找
}
else
{
findlen = rbox.Find(str, 0, RichTextBoxFinds.None);//不区分大小写并且不全字匹配查找
if (findlen == 0)
{
MessageBox.Show("查找超出范围,应将光标放在文字最后!", "提示");
}
}
len1 = findlen + str.Length;
}
}
}
else
{
len1 = findlen + str.Length;
if (len1 == rbox.Text.Length)
{
MessageBox.Show("查找达到了末尾!", "提示");
}
}
}
else// if(radioButton1.Checked)//向上查找
{
rbox.Focus();//控件获取焦点
if (this.checkBox1.Checked)//是否按下区分大小写
{
findlen = rbox.Find(str, 0, len2, RichTextBoxFinds.MatchCase | RichTextBoxFinds.Reverse);//区分大小写查找
}
else
{
findlen = rbox.Find(str, 0, len2, RichTextBoxFinds.Reverse);//不区分大小写并且不全字匹配查找
}
if (findlen < 0)//查找失败
{
if (this.checkBox1.Checked)//区分大小写是否按下
{
findlen = rbox.Find(str, 0, rbox.TextLength, RichTextBoxFinds.MatchCase | RichTextBoxFinds.Reverse);//区分大小写查找
}
else
{
findlen = rbox.Find(str, 0, rbox.TextLength, RichTextBoxFinds.Reverse);//不区分大小写并且不全字匹配查找
}
if (findlen < 0)
{
len2 = 0;
MessageBox.Show("未找到相关内容!");
}
else
{
len2 = 0;//长度清零
MessageBox.Show("查找达到了搜索的起始点!");
len2 = findlen;
}
}
else
{
len2 = findlen;
if (len2 == 0)
{
MessageBox.Show("查找达到了搜索的起始点!");
}
}
}
}
//替换textBox1中的文本为textBox2中的文本
private void button2_Click_1(object sender, EventArgs e)
{
string str0 = this.textBox1.Text, str1 = this.textBox2.Text;
this.replace(str0, str1);
}
//全部替换
private void button3_Click_1(object sender, EventArgs e)
{
Form3 mainfrom3 = new Form3();
RichTextBox rbox = parentForm.richTextBox1;
string str0 = this.textBox1.Text, str1 = this.textBox2.Text;
this.ReplaceAll(rbox, str0, str1);
}
private void button4_Click_1(object sender, EventArgs e)
{
this.Close();
}
//替换全部的函数
private void ReplaceAll(RichTextBox rbox, string str0, string str1)
{
rbox.Text = rbox.Text.Replace(str0, str1);
}
private void replace(string str0, string str1)
{
Form3 mainfrom3 = new Form3();
RichTextBox rbox = parentForm.richTextBox1;
rbox.SelectionLength = str0.Length;
rbox.SelectedText = str1;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
}
}
From3.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 FormDesign
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public Font SelectionFont { get; internal set; }
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
演示结果:
结果演示