您好,欢迎访问三七文档
C#日记本系别计算机与通信工程学院专业名称计算机科学与技术班级学号学生姓名成绩2012年6月18日日记本的编写实验报告日记本的介绍:总体结构如下,界面包括菜单栏、工具栏、状态栏以及文本编辑区。功能包括打开保存文本文件(后缀名为.txt),对文本文件的编辑,添加图像。创建项目:运行VS2008,依次执行【文件】→【新建】→【项目】命令,在弹出的“新建项目”对话框中选中“Windows窗体应用程序”模板,选择项目的存放位置并输入项目名称为“TXT”,单击“确定”按钮完成项目的创建。设计步骤:1添加控件新建好“TXT”项目后,定位到记事本程序的窗体设计器窗口,然后依次在窗体上放置以下控件:(1)MenuStrip11个(菜单控件)(2)ToolStrip11个(工具栏控件)(3)RichTextBox11个(多格式文本框控件)(4)StatusStrip11个(状态栏控件)(5)OpenFileDialog11个(打开对话框)(6)SaveFileDialog11个(保存对话框)(7)FontDialog11个(字体对话框)(8)ContextMenuStrip11个(快捷菜单控件)(9)DareTimerPicker11个(日期时间选择控件)(10)PictureBox3个(图像与动画控件)窗体控件属性设置:(1)MenuStrip的属性设置选中“menuStrip1”,在“请在此处键入”处输入“文件(&F)”,则添加了“文件”菜单项,“&F”是用来定义该菜单项的助记符。按照下图所示分别添加其他菜单项及子菜单项。打开文件菜单中的子菜单项的“新建”属性窗口,设置与菜单项关联的快捷键“ShortcutKeys”属性,如下图所示。其他子菜单的快捷键的设置类似。在“格式”菜单的子菜单的“自动换行”属性窗口中,设置“Checked”属性值为“True”。同样,在“查看”菜单的子菜单的“状态栏”属性窗口中,设置“Checked”属性值为“True”。(2)toolStrip1属性设置单击“”图标中倒三角按钮,在下拉列表中选择“Button”选项,,在新添加的“Button”属性窗口中设置“Text”属性值为“新建”,选中“Image”属性,单击“”钮,在弹出的“选择资源”对话框中单击“导入”按钮,如图所示,单击“确定”按钮完成“Image”属性设置。按上述步骤在工具栏中添加工具。(3)richTextBox1属性设置将richTextBox1调整到适当的大小,Anchor属性值设置为“Top,Bottom,Left,Right”,表示绑定到容器的边缘。(4)StatusStrip的属性设置单击右边的按钮,打开【项集合编辑器】对话框,单击【添加】按钮,添加1个StatusLabel,打开所添加的“toolStripStatusLabel1”属性窗口,Text属性值设置为空值(5)contextMenuStrip1属性设置选中“contextMenuStrip1”,在“请在此处键入”处输入“撤销(&U)”,则添加了“撤销”快捷菜单项,按照同样的方法添加其他快捷菜单项,(6)PictureBox属性设置将三个图片分别导入3个PictureBox中,在PictureBox控件的SizeMode属性设置为“StretchImage”得到结果。核心代码:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;namespaceTxt{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();richTextBox1.EnableAutoDragDrop=true;richTextBox1.Font=newFont(this.Font.FontFamily,15);}privatevoid文件ToolStripMenuItem_Click(objectsender,EventArgse){}privatevoid新建NToolStripMenuItem_Click(objectsender,EventArgse){this.richTextBox1.Text=;}privatevoid打开ToolStripMenuItem_Click(objectsender,EventArgse){OpenFileDialogopenFile=newOpenFileDialog();openFile.Filter=Textfile(*.txt)|*.txt|Word(*.doc)|*.doc;openFile.FilterIndex=1;openFile.DefaultExt=txt;openFile.Multiselect=false;stringstrFileName=openFile.FileName;if(openFile.ShowDialog()==DialogResult.OK){this.Text=openFile.FileName;richTextBox1.LoadFile(openFile.FileName,RichTextBoxStreamType.PlainText);}else{}}privatevoid保存ToolStripMenuItem_Click(objectsender,EventArgse){SaveFileDialogSaveFD=newSaveFileDialog();SaveFD.InitialDirectory=@D:\;//*文件的默认路径是D:盘SaveFD.Filter=Textfile(*.txt)|*.txt|Word(*.doc)|*.doc;SaveFD.FileName=newText;//*设置保存时默认的文件名newFileSaveFD.OverwritePrompt=true;if(SaveFD.ShowDialog()==DialogResult.OK){//*单击保存后,若选择Ok后,则调用SaveFile函数将文本的内容加入到默认名为newFile的文件中//richTextBox1.SaveFile(richTextBox1.Text);richTextBox1.SaveFile(SaveFD.FileName,RichTextBoxStreamType.PlainText);}}privatevoid另存为ToolStripMenuItem_Click_1(objectsender,EventArgse){SaveFileDialogSaveAsFD=newSaveFileDialog();SaveAsFD.InitialDirectory=@D:\;SaveAsFD.Filter=Textfile(*.txt)|*.txt|Word(*.doc)|*.doc;SaveAsFD.OverwritePrompt=true;if(SaveAsFD.ShowDialog()==DialogResult.OK){richTextBox1.SaveFile(SaveAsFD.FileName,RichTextBoxStreamType.PlainText);}}privatevoid打印ToolStripMenuItem_Click(objectsender,EventArgse){}privatevoid退出ToolStripMenuItem_Click(objectsender,EventArgse){Application.Exit();}privatevoid撤销ToolStripMenuItem_Click(objectsender,EventArgse){if(richTextBox1.CanUndo==true){richTextBox1.Undo();}}privatevoid剪切ToolStripMenuItem_Click(objectsender,EventArgse){if(richTextBox1.SelectedText!=){richTextBox1.Cut();}}privatevoid复制ToolStripMenuItem_Click(objectsender,EventArgse){if(richTextBox1.SelectedText!=){richTextBox1.Copy();}}privatevoid粘贴ToolStripMenuItem_Click(objectsender,EventArgse){richTextBox1.Paste();}privatevoid删除ToolStripMenuItem_Click(objectsender,EventArgse){if(richTextBox1.SelectedText!=){richTextBox1.SelectedText.Remove(1,richTextBox1.SelectionLength);}}privatevoid全选ToolStripMenuItem_Click(objectsender,EventArgse){richTextBox1.SelectAll();}privatevoidrichTextBox1_TextChanged(objectsender,EventArgse){}privatevoid字体ToolStripMenuItem_Click(objectsender,EventArgse){FontDialogfontDialog=newFontDialog();fontDialog.ShowColor=true;//*设置显示字体的颜色fontDialog.AllowScriptChange=true;//*设置可选字符集fontDialog.AllowVectorFonts=true;//*设置可选的矢量字体fontDialog.ShowEffects=true;//*设置可选字体的效果,如下划线等if(fontDialog.ShowDialog()==DialogResult.OK){//*单击OK后,则对文本框中字体以及颜色作出改变richTextBox1.Font=fontDialog.Font;richTextBox1.ForeColor=fontDialog.Color;}}privatevoiddateTimePicker1_ValueChanged(objectsender,EventArgse){dateTimePicker1.Format=DateTimePickerFormat.Custom;dateTimePicker1.CustomFormat=yyyy年MM月dd日HH时mm分;}privatevoidrichTextBox1_MouseUp(objectsender,MouseEventArgse){}privatevoidrichTextBox1_KeyUp(objectsender,KeyEventArgse){}privatevoid关于日记本ToolStripMenuItem_Click(objectsender,EventArgse){MessageBox.Show(日记本帮助人们记住当天的事情。);}privatevoidcontextMenuStrip1_Opening(objectsender,CancelEventArgse){}privatevoid状态栏ToolStripMenuItem_Click(objectsender,EventArgse){if(状态栏ToolStripMenuItem.Checked==true){状态
本文标题:C#日记本
链接地址:https://www.777doc.com/doc-4690537 .html