📄 form1.cs
字号:
if(openFileDialog1.ShowDialog()==DialogResult.OK||openFileDialog1.FileName.Length>=0) {//如果选择了要打开的文件,且点击了"打开"按钮 m_strFileName=openFileDialog1.FileName; FileName=m_strFileName; } else { return; } richTextBox1.Text="";//清空richTextBOx FileStream m_FileStream=null;//声明一个文件流对象 string m_strFileContents=""; m_strOriginText=""; if(m_strFileName!="") {//如果选择了要打开的文件 try { m_strTemp=""; int m_GetByte=1; //创建一个FileStream类的对象m_FileStream m_FileStream=new FileStream(m_strFileName,FileMode.Open,FileAccess.Read); Byte[] m_Byte=new Byte[1024];//声明一个数组 m_Byte.Initialize();//初始化该数组 for(;m_GetByte!=0;) {//按字节读文件 System.Text.Encoding encoding=System.Text.Encoding.UTF8; m_Byte.Initialize(); m_strTemp=""; m_GetByte=m_FileStream.Read(m_Byte,0,1024); m_strTemp=encoding.GetString(m_Byte,0,1024); m_strFileContents+=m_strTemp;//把结果保存在m_strFileContents中 } } catch(FileNotFoundException e1) {//捕获错误 string m_strmsg="文件没有找到:"+ e1; System.Windows.Forms.MessageBox.Show(m_strmsg,"写字板"); return; } finally { if(m_FileStream!=null) { m_FileStream.Close(); } } } m_strOriginText=m_strFileContents; richTextBox1.Text=m_strFileContents; this.Text=m_strFileName+"蒋枝枝——写字板"; statusBar1.Text="就绪"; ExistFlag=true; IsSavedFlag=false; ModifiedFlag=false; richTextBox1.Select(0,0); } private void menuBaocun_Click(object sender, System.EventArgs e) { SaveFile();//调用SaveFile方法 statusBar1.Text="就绪";//设置状态栏信息 } private void SaveDocFile(string m_strFileName) { this.Text=m_strFileName+"——写字板";//改变窗体文本状态 richTextBox1.Select(0,0);//定义文本框的输入光标位置 FileStream m_strStream=null;//声名一个文件流对象 try {//保存文件 System.Text.Encoding encoding=System.Text.Encoding.UTF8;//设定编码方式 //创建一个FileStream类对象 m_strStream=new FileStream(m_strFileName,FileMode.Create,FileAccess.Write); Byte[] m_Byte; //声名数组用于保存文本框的内容 m_Byte=new byte [richTextBox1.Text.Length];//声明一个文件大小的数组 m_Byte.Initialize(); //写文件到指定的文件名中 m_Byte=encoding.GetBytes(richTextBox1.Text.ToString()); m_strStream.Write(m_Byte,0,m_Byte.Length); } catch(Exception e) { string m_strmsg="Error:"+e;//捕捉错误信息 MessageBox.Show(m_strmsg,"写字板"); return; } finally //对错误处理方法 { if(m_strStream!=null) m_strStream.Close(); } this.Text=m_strFileName+"——写字板"; IsSavedFlag=true; ModifiedFlag=false; } private void SaveFile() { string m_strFileName=""; //用于保存文件名 saveFileDialog1.Filter="文本文件(*.txt)|*.txt";//设置文件过滤器 statusBar1.Text="保存文件";//在状态栏显示状态 if(FileName=="蒋枝枝")//判定FileName状态 //调用保存文件对话框 if(saveFileDialog1.ShowDialog()==DialogResult.OK) { //如果点击了对话框中的保存 FileName=m_strFileName=saveFileDialog1.FileName;//则为文件命名 } else { return; } SaveDocFile(FileName);//调用SaveDocFile方法 } private void menuWenjian_Click(object sender, System.EventArgs e) { } string FileName;//保存文件名的位置 bool ModifiedFlag;//标识文件是否被修改过 bool IsSavedFlag;//标识文件是否保存过 bool ExistFlag;//标识richTextBox中是否存在文件 string m_strOriginText;//保存richTextBox中原始的文件 private void Form1_Load(object sender, System.EventArgs e) { ModifiedFlag=true;//设置文件被修改过 IsSavedFlag=false;//设置文件被保存过 FileName="蒋枝枝";//设置文件名 this.Text=FileName+"——写字板";//设置“我的//记事本”的窗口标题 m_strOriginText="girl:06计软-蒋枝枝";//状态栏设置 } private void menuLingcunwei_Click_1(object sender, System.EventArgs e) { FileName="蒋枝枝"; menuBaocun_Click(sender,e);//调用保存文件菜单的事件代码 } private void menuTuichu_Click(object sender, System.EventArgs e) { this.Close(); } private void menuItem5_Click(object sender, System.EventArgs e) { richTextBox1.SelectAll(); }
private void menuItem5_Click_1(object sender, System.EventArgs e)
{
Form5 fm=new Form5();
fm.ShowDialog();
}
private void menuZiti_Click(object sender, System.EventArgs e) { fontDialog1.ShowDialog();
richTextBox1.Font = fontDialog1.Font;
richTextBox1.BackColor = fontDialog1.Color; } private void menuNiantie_Click(object sender, System.EventArgs e) { richTextBox1.Paste(); } private void menuChexiao_Click(object sender, System.EventArgs e) { richTextBox1.Undo(); } private void menuJianqie_Click(object sender, System.EventArgs e) { richTextBox1.Cut(); } private void menuFuzhi_Click(object sender, System.EventArgs e) { richTextBox1.Copy(); } private void menuShanchu_Click(object sender, System.EventArgs e) { richTextBox1.Clear(); this.Text="蒋枝枝——写字板"; } private void menuQuanxuan_Click(object sender, System.EventArgs e) { richTextBox1.SelectAll(); } private void menuGuanyujishiben_Click(object sender, System.EventArgs e) { bangzhu(); } private void bangzhu() { string str="06计软(R) 写字板\n\n版本 1.1(内部版本号 SP2_2007_03)\n\n版权所有 (C)2007 06计软(蒋枝枝)。\n&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n本版本符合最终用户许可协议,受给我:\n\n\n06计软(蒋枝颖)\n\n\n\t\t\t^_^"; MessageBox.Show(str,"关于 写字板"); } private void menuGuanyuzhuti_Click(object sender, System.EventArgs e) { zhuti(); } private void zhuti() { string str="作业,用来学习,还可以用来练字呢!\n\n\t &^_^*"; MessageBox.Show(str,"关于 主题"); } private void menuChexiao2_Click(object sender, System.EventArgs e) { richTextBox1.Undo(); } private void menuJianqie2_Click(object sender, System.EventArgs e) { richTextBox1.Cut(); } private void menuFuzhi2_Click(object sender, System.EventArgs e) { richTextBox1.Copy(); } private void menuNiantie2_Click(object sender, System.EventArgs e) { richTextBox1.Paste(); } private void menuShanchu2_Click(object sender, System.EventArgs e) { richTextBox1.Clear(); } private void menuZhidonghuanhang_Click(object sender, System.EventArgs e) { colorDialog1.ShowDialog(); richTextBox1.ForeColor = colorDialog1.Color; } private void menuXinjian_Click(object sender, System.EventArgs e) { System.Windows.Forms.DialogResult yzResult; //定义变量来保存对话框选择的结果 string yzMsg; yzMsg="是否保存文档?\n"+FileName; richTextBox1.Select(0,0); if(richTextBox1.Text!=m_strOriginText)//当前richTextBox1中的文本与上次不一样时 ModifiedFlag=true;//将modifiedFlag设为True,表示文本被修改过 if(richTextBox1.Text!="") { if(ModifiedFlag==true)//如果richTextBox1中有文本 {//如果文本被修改过,就提示是否保存文件 yzResult=MessageBox.Show(this,yzMsg,"文本文件",System.Windows.Forms.MessageBoxButtons.YesNoCancel,System.Windows.Forms.MessageBoxIcon.Exclamation,System.Windows.Forms.MessageBoxDefaultButton.Button1); if(yzResult==System.Windows.Forms.DialogResult.Yes) {//如果保存的对话框选择结果为Yes,则保存文件 if(IsSavedFlag) { //如果已经保存过,调用保存文件菜单的PerformClick方法 menuBaocun.PerformClick(); } else {//如果还没有保存过,调用另存为文件菜单的PerformClick方法 menuLingcunwei.PerformClick(); } richTextBox1.Text="";//变量初始化 ModifiedFlag=false; ExistFlag=false; IsSavedFlag=false; FileName="新建文件"; this.Text=FileName+"——蒋枝枝"; statusBar1.Text="新建完成"; } else if(yzResult==System.Windows.Forms.DialogResult.No) {//如果保存的对话框选择的结果为No,则保存文件 richTextBox1.Text=""; richTextBox1.SelectAll(); ModifiedFlag=false; ExistFlag=false; IsSavedFlag=false; FileName="新建文件"; this.Text=FileName+"——蒋枝枝"; statusBar1.Text="新建完成"; } } else {//如果richTextBox1中没有文本的话 richTextBox1.Text=""; ModifiedFlag=false; ExistFlag=false; IsSavedFlag=false; FileName="新建文件"; this.Text=FileName+"——蒋枝枝"; statusBar1.Text="新建完成"; } } }
private void menuItem2_Click(object sender, System.EventArgs e)
{
Form2 dlg=new Form2();
dlg.ShowDialog();
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
Form3 dlm=new Form3();
dlm.ShowDialog();
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
Form4 fc=new Form4();
fc.ShowDialog();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -