您好,欢迎访问三七文档
实验题目:用C#编写一个简单的写字板实验目的:1.了解C#应用程序的基本结构2.掌握基本代码风格3.进一下掌握类和对象的基本概念、掌握类字段和属性的使用4.学习掌握写字板实现的思想以及编程方法预习内容:一个C#的写字板程序,从编写到运行,要经过哪些步骤?需要用到哪些控件?分别怎么设置属性及触发事件?实验步骤:1.打开C#软件,创建一个Windows应用程序窗口,更改窗口的属性“Text”为“写字板”2.添加menuStrip控件,并增加四大菜单按钮,分别为“文件”“编辑”“格式”“帮助”,其中“文件”的子菜单有新建、打开、保存、另存为、页面设置、打印预览、打印、退出;而“编辑”的子菜单有剪切、复制、粘贴、撤销、恢复、查找和替换;“格式”的子菜单有字体;“帮助”的子菜单为“关于写字板”。并做好相关的属性设置准备。3.点击“项目”为主窗体添加2个Windows窗体,分别为formAbout.cs和formFindReplace.cs,并对主窗体增加对话相关的控件。4.在formAbout.cs中添加2个控件,即一个Text为“版权所有”的label控件一个Text为“确定”的TextBox控件。5.为formFindReplace.cs添加2个Text分别为“查找字符串”、“替换字符串”的label控件,2个Text分别为“查找下一个”、“替换查到字符串”的Button,2个Text为空的textBox控件。6.为各个控件的触发事件编程,写出源程序,实现写字板功能。7.编绎,运行。检查结果。实验内容://主窗体Form1.cs的源代码如下usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;//处理文件必须引入的命名空间usingSystem.Drawing.Printing;//打印必须引入的命名空间namespaceWindowsFormsApplication1{publicpartialclassForm1:Form{//类开始定义变量strings_FileName=;//文件名变量intFindPostion=0;//用来记录下一次查找的开始位置boolbsave=false;//标记文本内容是否被修改,为false表不被修改,为true表关闭文件前要询问用户是否保存当前已被修改的文件StringReaderstreamToPrint=null;FontprintFont;//以下是实现查找和替换功能的语句publicvoidFindRichTextBoxString(stringFindString)//查找方法{if(FindPostion=richTextBox1.Text.Length){MessageBox.Show(已到文本底部,再次查找将从文本开始处查找,提示,MessageBoxButtons.OK);FindPostion=0;return;}FindPostion=richTextBox1.Find(FindString,FindPostion,RichTextBoxFinds.MatchCase);if(FindPostion==-1){MessageBox.Show(已到文本底部,再次查找将从文本开始处查找,提示,MessageBoxButtons.OK);FindPostion=0;}else{richTextBox1.Focus();FindPostion+=FindString.Length;}}publicvoidReplaceRichTextBoxString(stringReplaceString)//替换方法{if(richTextBox1.SelectedText.Length!=0)richTextBox1.SelectedText=ReplaceString;}publicboolIfSaveOldFile()//检查当前文件是否被更改,提示保存已被修改的文件{boolReturnValue=true;if(bsave){System.Windows.Forms.DialogResultdr;dr=MessageBox.Show(this,要保存当前更改吗?,提示?,MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);switch(dr){caseSystem.Windows.Forms.DialogResult.Yes:bsave=false;if(s_FileName.Length!=0)richTextBox1.SaveFile(s_FileName,RichTextBoxStreamType.PlainText);else{SaveFileDialogsaveFileDialog1=newSaveFileDialog();if(saveFileDialog1.ShowDialog()==DialogResult.OK){s_FileName=saveFileDialog1.FileName;richTextBox1.SaveFile(s_FileName,RichTextBoxStreamType.PlainText);}}ReturnValue=true;break;caseSystem.Windows.Forms.DialogResult.No:bsave=false;ReturnValue=true;break;caseSystem.Windows.Forms.DialogResult.Cancel:ReturnValue=false;break;}}returnReturnValue;}publicForm1(){InitializeComponent();}//以下是实现编辑功能privatevoidMenuItemEditCut_Click(objectsender,EventArgse)//剪切{richTextBox1.Cut();}privatevoidMenuItemEditCopy_Click(objectsender,EventArgse)//复制{richTextBox1.Copy();}privatevoidMenuItemEditPaste_Click(objectsender,EventArgse)//粘贴{richTextBox1.Paste();}privatevoidMenuItemEditUndo_Click(objectsender,EventArgse)//撤销{richTextBox1.Undo();}privatevoidMenuItemEditRedo_Click(objectsender,EventArgse)//恢复{richTextBox1.Redo();}//以下是实现文件功能privatevoidMenuItemFileExit_Click(objectsender,EventArgse)//关闭{Close();}privatevoidMenuItemFileNew_Click(objectsender,EventArgse)//新建{if(!IfSaveOldFile())return;s_FileName=;richTextBox1.Clear();}privatevoidMenuItemFileOpen_Click(objectsender,EventArgse)//打开{if(!IfSaveOldFile())return;if(openFileDialog1.ShowDialog()==DialogResult.OK){s_FileName=openFileDialog1.FileName;richTextBox1.LoadFile(openFileDialog1.FileName,RichTextBoxStreamType.PlainText);}}privatevoidMenuItemFileSave_Click(objectsender,EventArgse)//保存{if(s_FileName.Length!=0){bsave=false;richTextBox1.SaveFile(s_FileName,RichTextBoxStreamType.PlainText);}elseMenuItemFileSaveAs_Click(sender,e);}privatevoidMenuItemFileSaveAs_Click(objectsender,EventArgse)//另存为{SaveFileDialogsaveFileDialog1=newSaveFileDialog();saveFileDialog1.Filter=纯文本文件(*.txt)|*.txt|所有文件(*.*)|*.*;saveFileDialog1.FilterIndex=1;if(saveFileDialog1.ShowDialog()==DialogResult.OK){s_FileName=saveFileDialog1.FileName;richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText);bsave=false;}}//以下是实现字体功能privatevoidMenuItemFormatFont_Click(objectsender,EventArgse)//格式-字体设置{if(fontDialog1.ShowDialog()==DialogResult.OK)richTextBox1.SelectionFont=fontDialog1.Font;}//以下是实现帮助功|能privatevoidMenuItemHelpAbout_Click(objectsender,EventArgse)//关于{formAboutAboutDialog=newformAbout();AboutDialog.ShowDialog(this);}privatevoidMenuItemEditFindReplace_Click(objectsender,EventArgse){FindPostion=0;formFindReplaceFindReplaceDialog=newformFindReplace(this);FindReplaceDialog.Show();}privatevoidrichTextBox1_TextChanged(objectsender,EventArgse){bsave=true;}privatevoidForm1_FormClosing(objectsender,FormClosingEventArgse){if(!IfSaveOldFile())e.Cancel=true;}privatevoidprintDocument1_PrintPage(objectsender,PrintPageEventArgse){floatlinesPerPage=0;floatyPos=0;intcount=0;floatleftMargin=e.MarginBounds.Left;floattopMargin=e.MarginBounds.Top;stringline=null;linesPerPage=e.MarginBounds.Height/printFont.GetHeight(e.Graphics);while(countlinesPerPage&&((line=streamToPrint.ReadLine()
本文标题:C_写字板实验报告
链接地址:https://www.777doc.com/doc-3560439 .html