【Java精品源码栏目提醒】:网学会员为广大网友收集整理了,C#实验-记事本(带源码) - 其它资料,希望对大家有所帮助!
1 记事本——实验报告一、实验目的 创建一个 Windows 窗体应用程序,实现记事本的基本功能,具体包括新建文件、打开文件、保存文件、查找等功能。
该实验的目的是掌握: (一) 窗体程序的开发 (二) 常用控件的使用 (三) 常用事件的处理 (四) 对话框的设计和使用 (五) 文件访问的基本方法二、实验内容 (一)主窗口 Form1 图 1 主窗口 主窗口界面如图 1 所示,功能包括基本编辑操作、主菜单和其它快捷键功能。
1、编辑功能用文本框实现。
2、窗口标题与文件名相一致。
未打开文件时为“无标题”,打开文件另存为后为文件名。
3、支持菜单的热键和快捷键。
二者的区别是前者是激活菜单且显示出该菜单项时有效,后者在任何时候有效。
4、实现新建、打开、保存和查找功能。
。
5、支持 F3(查找下一个) 表 1Form1 控件列表控件 属性/事件 值 说明 否则 Form1 的Form1 KeyPreview True KeyDown 无效文件 Text 文件F 形成下划线FToolStripMenuItem Click 文件 NToolStripMenuItem_Click文件 Text 新建NNToolStripMenuItem Click 打开 OToolStripMenuItem_Click 2文件 ShortcutKeys CtrlN 快捷键NToolStripMenuItem Click 保存 SToolStripMenuItem_Click查找 Text 查找F...FToolStripMenuItem 查找 Click 查找 FToolStripMenuItem_Click FToolStripMenuItem Muiltiline True 输入多行数据 Dock Fill 填充客户区 Text 空白textBox1 窗口非活动时 HideSelection False 显示选择部分 的内容 KeyDown textBox1_KeyDown 处理A 等按键using Systemusing System.Collections.Genericusing System.ComponentModelusing System.Datausing System.Drawingusing System.Linqusing System.Textusing System.Windows.Formsusing System.IOnamespace WindowsFormsApplication1 public partial class Form1 : Form public Form2 fm2 null public string searchText public Form1 InitializeComponent private void saveFile if textBox1.Text.Length 0 textBox1.Modified 3 if MessageBox.Show想保存文件吗 记事本 MessageBoxButtons.YesNoCancel MessageBoxIcon.Warning DialogResult.Yes SaveFileDialog d new SaveFileDialog d.Filter 文本文件.txt.txt所有文件.. if d.ShowDialog DialogResult.OK FileStream fs File.OpenWrited.FileName StreamWriter sr new StreamWriterfs sr.WritetextBox1.Text sr.Flush fs.Close private void 文件NToolStripMenuItem_Clickobject sender EventArgs e saveFile textBox1.Text Text 无标题 - 记事本 private void OpenFile OpenFileDialog d new OpenFileDialog d.Filter 文本文件.txt.txt所有文件.. if d.ShowDialog DialogResult.OK FileStream fs File.OpenReadd.FileName StreamReader sr new StreamReaderfs string s string s1 while s sr.ReadLine null s1 s textBox1.Text s1 string fname d.FileName.Substringd.FileName.LastIndexOf 1 Text fname - 记事本 4 private void 打开OToolStripMenuItem_Clickobject sender EventArgs e saveFile OpenFile private void 保存SToolStripMenuItem_Clickobject sender EventArgs e saveFile private void 查找FToolStripMenuItem_Clickobject sender EventArgs e if fm2 null fm2 new Form2 fm2.fm1 this Form2.textBox2 textBox1 fm2.Show else fm2.Activate private void textBox1_KeyDownobject sender KeyEventArgs e if e.KeyCode Keys.A e.Control e.Shift e.Alt textBox1.SelectAll e.Handled true else if e.KeyCode Keys.F3 e.Control e.Shift e.Alt Form2.findNext 5 (二)查找对话框 图 2 查找对话框 查找对话框的界面(图 2)与记事本的相同。
1、支持帮助按钮。
2、如果“查找文件”内容为空,则“查找下一个”按钮灰度显示。
3、显示为无模式对话框,即不关闭它也能回主窗口继续操作。
4、按 Esc 键退出查找对话框。
5、前一次输入查询内容且执行查询后,下一次该内容自动出现在对话框中。
6、向后查询的查找范围是选定范围末尾到文件尾,向前查询的查找范围是文件首到选定范围的开端。
7、在主窗口中按 F3 功能键实现“查找下一个”。
表 2 查找对话框控件列表 控件 属性 值 说明 Text 查找 FormBorderStyle FixedDialog 不能改变大小 MinimizedBox False 设置控制按钮,使Form2 它们变成只有“帮 MaxmizedBox False 助”和“关闭”两 ControlBox True 个按钮 HelpButton True Text 查找下一个F Enabled false 必须输入查找内 容button1 Click button1_Click Tag 查找在“查找内容”中指定 文字的下一个实例。
HelpRequested textBox1_HelpRequested 在对话框中单击 6 “帮助按钮”后, 鼠标形状改变为 带问号的箭头,再 单击一控件 Text 取消 CancleButton true 按 ESC 键关闭对 话框 ResultDialog Cancelbutton2 Click button2_Click Tag 关闭对话框,不保存任何更 改。
HelpRequested textBox1_HelpRequested Text 向上 Checked false Tag 指定从文档中插入点开始搜radioButton1 索的方向。
单击“向上”,从 插入点向上进行搜索。
单击 , “向下” 从插入点向下进行 搜索。
HelpRequested textBox1_HelpRequested Text 向下 Checked true Tag 指定从文档中插入点开始搜radioButton2 索的方向。
单击“向上”,从 插入点向上进行搜索。
单击 , “向下” 从插入点向下进行 搜索。
HelpRequested textBox1_HelpRequested Text 区分大小写C Tag 只查找与“查找内容”中指checkbox1 定 的 大 小写 字 符一 致 的文 本。
HelpRequested textBox1_HelpRequested函数 MessageBox.Show找不到 searchText 记事本 7 MessageBoxButtons.OK MessageBoxIcon.Information的效果是下列消息框。
图 3 消息框using Systemusing System.Collections.Genericusing System.ComponentModelusing System.Datausing System.Drawingusing System.Linqusing System.Textusing System.Windows.Formsnamespace WindowsFormsApplication1 public partial class Form2 : Form public Form1 fm1 private static string searchText private static bool forward true public static TextBox textBox2 null public Form2 InitializeComponent textBox1.Text searchText private static void showNotFound MessageBox.Show找不到 searchText 记事本 MessageBoxButtons.OK MessageBoxIcon.Information public static void findNext int pos if searchText.Length 0 return 8 if forward pos textBox2.SelectionStart - 1 if pos 0 pos textBox2.Text.LastIndexOfsearchText pos textBox2.Selectpos searchText.Length else showNotFound else pos textBox2.SelectionStart textBox2.SelectionLength pos textBox2.Text.IndexOfsearchText pos if pos 0 textBox2.Selectpos searchText.Length else showNotFound private void button1_Clickobject sender EventArgs e searchText textBox1.Text forward radioButton2.Checked
上一篇:
【精品】Android开发内存泄漏及检查工具使用培训资料
下一篇:
手机媒体营销分析