运用C#中的控件实现简单的记事本功能。参考电脑中的记事本,完成关于新建,保存,粘贴,复制的基本功能的完成。
解题思路:合理的运用C#中的相关控件。
(1)新建功能的代码
private void 新建NCtrlNToolStripMenuItem_Click(object sender, EventArgs e)//新建 { if (b == true || rtxtNotepad.Text.Trim() != "") { if (s == false) { string result; result = MessageBox.Show("文件尚未保存,是否保存?", "保存文件", MessageBoxButtons.YesNoCancel).ToString(); switch (result) { case "Yes": if (b == true) { rtxtNotepad.SaveFile(odlgNotepad.FileName); } else if (sdlgNotepad.ShowDialog() == DialogResult.OK) { rtxtNotepad.SaveFile(sdlgNotepad.FileName); } s = true; rtxtNotepad.Text = ""; break; case "No": b = false; rtxtNotepad.Text = ""; break; } } } }
代码中的b,s是两个用于判断的元素。b用于判断文件是新建的还是从磁盘打开的,s用于判断文件是否被保存。
(2)打开功能代码
private void tsmiOpen_Click(object sender, EventArgs e)//打开 { if (b == true || rtxtNotepad.Text.Trim() != "") { if (s == false) { string result; result = MessageBox.Show("文件未保存,是否保存?", "保存文件", MessageBoxButtons.YesNoCancel).ToString(); switch (result) { case "Yes": if (b == true) { rtxtNotepad.SaveFile(odlgNotepad.FileName); } else if (sdlgNotepad.ShowDialog() == DialogResult.OK) { rtxtNotepad.SaveFile(sdlgNotepad.FileName); } s = true; break; case "No": b = false; rtxtNotepad.Text = ""; break; } } } odlgNotepad.RestoreDirectory = true; if ((odlgNotepad.ShowDialog() == DialogResult.OK) && odlgNotepad.FileName != "") { rtxtNotepad.LoadFile(odlgNotepad.FileName);//打开代码语句 b = true; } s = true; }
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 Notepad { public partial class frmNotepad : Form { bool b = false; bool s = true; public frmNotepad() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e) { } private void 格式ToolStripMenuItem_Click(object sender, EventArgs e) { } private void 新建NCtrlNToolStripMenuItem_Click(object sender, EventArgs e)//新建 { if (b == true || rtxtNotepad.Text.Trim() != "") { if (s == false) { string result; result = MessageBox.Show("文件尚未保存,是否保存?", "保存文件", MessageBoxButtons.YesNoCancel).ToString(); switch (result) { case "Yes": if (b == true) { rtxtNotepad.SaveFile(odlgNotepad.FileName); } else if (sdlgNotepad.ShowDialog() == DialogResult.OK) { rtxtNotepad.SaveFile(sdlgNotepad.FileName); } s = true; rtxtNotepad.Text = ""; break; case "No": b = false; rtxtNotepad.Text = ""; break; } } } } private void rtxtNotepad_TextChanged(object sender, EventArgs e) { //文本被修改后,s变为false,表示未保存 s = false; string str = rtxtNotepad.Text; int m = rtxtNotepad.SelectionStart; int Ln = 0; int Col = 0; for (int i = m - 1; i >= 0; i--) { if (str[i] == '\n') { Ln++; } if (Ln < 1) { Col++; } } Ln = Ln + 1; Col = Col + 1; tssLbl3.Text = "行:" + Ln.ToString() + "," + "列:" + Col.ToString(); } private void tsmiPaste_Click(object sender, EventArgs e)//粘贴 { rtxtNotepad.Paste(); } private void tsmiOpen_Click(object sender, EventArgs e)//打开 { if (b == true || rtxtNotepad.Text.Trim() != "") { if (s == false) { string result; result = MessageBox.Show("文件未保存,是否保存?", "保存文件", MessageBoxButtons.YesNoCancel).ToString(); switch (result) { case "Yes": if (b == true) { rtxtNotepad.SaveFile(odlgNotepad.FileName); } else if (sdlgNotepad.ShowDialog() == DialogResult.OK) { rtxtNotepad.SaveFile(sdlgNotepad.FileName); } s = true; break; case "No": b = false; rtxtNotepad.Text = ""; break; } } } odlgNotepad.RestoreDirectory = true; if ((odlgNotepad.ShowDialog() == DialogResult.OK) && odlgNotepad.FileName != "") { rtxtNotepad.LoadFile(odlgNotepad.FileName);//打开代码语句 b = true; } s = true; } private void tsmiSave_Click(object sender, EventArgs e)//保存 { if (b == true && rtxtNotepad.Modified == true) { rtxtNotepad.SaveFile(odlgNotepad.FileName); s = true; } else if (b == false && rtxtNotepad.Text.Trim()!=""&& sdlgNotepad.ShowDialog()==DialogResult.OK) { rtxtNotepad.SaveFile(odlgNotepad.FileName); s = true; b = true; odlgNotepad.FileName = sdlgNotepad.FileName; } } private void tsmiSaveAs_Click(object sender, EventArgs e)//另存为 { if (sdlgNotepad.ShowDialog() == DialogResult.OK) { rtxtNotepad.SaveFile(sdlgNotepad.FileName); s = true; } } private void tsmiClose_Click(object sender, EventArgs e)//关闭 { Application.Exit(); } private void tsmiUndo_Click(object sender, EventArgs e)//撤销 { rtxtNotepad.Undo(); } private void tsmiCopy_Click(object sender, EventArgs e)//复制 { rtxtNotepad.Copy(); } private void tsmiCut_Click(object sender, EventArgs e)//剪切 { rtxtNotepad.Cut(); } private void tsmiSelectAll_Click(object sender, EventArgs e)//全选 { rtxtNotepad.SelectAll(); } private void tsmiDate_Click(object sender, EventArgs e)//显示时间 { rtxtNotepad.AppendText(System.DateTime.Now.ToString()); } private void tsmiAuto_Click(object sender, EventArgs e)//自动换行 { if (tsmiAuto.Checked == false) { tsmiAuto.Checked = true; rtxtNotepad.WordWrap = true; } else { tsmiAuto.Checked = false; rtxtNotepad.WordWrap = false; } } private void tsmiFont_Click(object sender, EventArgs e)//字体 { fdlgNotepad.ShowColor = true; if (fdlgNotepad.ShowDialog()==DialogResult.OK) { rtxtNotepad.SelectionColor = fdlgNotepad.Color; rtxtNotepad.SelectionFont = fdlgNotepad.Font; } } private void tsmiToolStrip_Click(object sender, EventArgs e) { Point point; if (tsmiToolStrip.Checked == true) { // 隐藏工具栏时,把坐标设为(0,24),因为菜单的高度为24 point = new Point(0, 24); tsmiToolStrip.Checked = false; tlsNotepad.Visible = false; // 设置多格式文本框左上角位置 rtxtNotepad.Location = point; // 隐藏工具栏后,增加文本框高度 rtxtNotepad.Height += tlsNotepad.Height; } else { point = new Point(0, 49); tsmiToolStrip.Checked = true; tlsNotepad.Visible = true; rtxtNotepad.Location = point; rtxtNotepad.Height -= tlsNotepad.Height; } } private void tsmiStatusStrip_Click(object sender, EventArgs e) { if (tsmiStatusStrip.Checked == true) { tsmiStatusStrip.Checked = false; stsNotepad.Visible = false; rtxtNotepad.Height += stsNotepad.Height; } else { tsmiStatusStrip.Checked = true; stsNotepad.Visible = true; rtxtNotepad.Height -= stsNotepad.Height; } } private void tsmiAbout_Click(object sender, EventArgs e)//显示帮助 { frmAbout ob_FrmAbout = new frmAbout(); ob_FrmAbout.Show(); } private void tlsNotepad_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { int n; n = tlsNotepad.Items.IndexOf(e.ClickedItem); switch (n) { case 0: 新建NToolStripButton_Click(sender, e); break; case 1: tsmiOpen_Click(sender, e); break; case 2: 保存SToolStripButton_Click(sender, e); break; case 3: tsmiCopy_Click(sender, e); break; case 4: 剪切UToolStripButton_Click(sender, e); break; case 5: 粘贴PToolStripButton_Click(sender, e); break; case 6: tsmiPaste_Click(sender, e); break; case 7: tsmiAbout_Click(sender, e); break; } } private void 新建NToolStripButton_Click(object sender, EventArgs e) { } private void 打开OToolStripButton_Click(object sender, EventArgs e) { } private void 保存SToolStripButton_Click(object sender, EventArgs e) { } private void 剪切UToolStripButton_Click(object sender, EventArgs e) { } private void 复制CToolStripButton_Click(object sender, EventArgs e) { } private void 粘贴PToolStripButton_Click(object sender, EventArgs e) { } private void tmrNotepad_Tick(object sender, EventArgs e) { tssLbl2.Text = System.DateTime.Now.ToString(); } private void odlgNotepad_FileOk(object sender, CancelEventArgs e) { } private void tssLbl2_Click(object sender, EventArgs e) { } } }
主界面视图截图
本次实验熟悉了C#中部分常用控件,包括lable,RichTextBox,ToolStrip,StatusStrip以及与对话框相关的控件。
源代码仓库