【C#源代码】记事本的新建、打开、保存等功能

 对照电脑自带的记事本,来进行窗体设计和功能的添加,在主窗体的基础上,只需额外添加查找、转到、替换三个窗体。以下是部分功能的源代码:

 

//新建
        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            if (richTextBox1.Text != "")
            {
                if (bIsSave == false) //没有保存
                {
                    DialogResult result = MessageBox.Show("文件 无标题 的文字已改变。\n\n想保存文件吗?", "记事本", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                    if (result == DialogResult.Yes)
                    {
                        saveFileDialog1.FileName = "*.txt";
                        saveFileDialog1.Filter = "文本文档(*.txt)|*.txt";
                        saveFileDialog1.Title = "保存";
                        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                            filename = saveFileDialog1.FileName;
                            this.Text = "无标题 - 记事本";
                            richTextBox1.Clear();
                        }
                    }
                    if (result == DialogResult.No)
                    {
                        richTextBox1.Clear();
                    }
                }
                else 
                {
                    richTextBox1.Clear();
                }
            }
        }
 
 
//复制
private void 复制CCtrlCToolStripMenuItem_Click(object sender, EventArgs e)
        {
              // richTextBox1.Copy();
                 this.Cursor = Cursors.WaitCursor;
                 string strTemp = richTextBox1.SelectedText;   //获取RichTextBox中选中的文字
                 if (strTemp.Equals(""))     //Equals 方法确定两个对象是否相等
                     return;  
                 Clipboard.Clear();//清除原有剪切板中内容  
                 Clipboard.SetText(strTemp);//将文字添加到剪切板中,还添加Object类型数据               
                 this.Cursor = Cursors.Default;        
        }
 
  //打开
        private void 打开OCtrlOToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "*.txt";
            openFileDialog1.Filter = "文本文档(*.txt)|*.txt|所有文件|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.Default);
                richTextBox1.Text = sr.ReadToEnd();
                sr.Close();
            }
            filename = openFileDialog1.FileName;
            this.Text = Path.GetFileName(openFileDialog1.FileName)+" - 记事本";
            bIsSave = true;
        }
 
 
 //保存
        private void 保存SCtrlSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            if (filename == "")
            {
                bIsSave = false;
                saveFileDialog1.FileName = "*.txt";
                saveFileDialog1.Filter = "文本文档(*.txt)|*.txt";
                saveFileDialog1.Title = "保存";
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                    filename = saveFileDialog1.FileName;
                    this.Text = "无标题 - 记事本";
                    richTextBox1.Clear();
                }
                else
                {
                    richTextBox1.SaveFile(filename , RichTextBoxStreamType.PlainText);
                }
            }
            bIsSave = true;
        }
 
 
 //时间日期
        private void 时间日期ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //System.DateTime currentTime = new System.DateTime();
            string Time;
            Time = (System.DateTime.Now).ToString ();
            richTextBox1.Text = Time;
        }
 
 //全选
        private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Focus();//设置定位到当前活动的RichTextBox,否则它不能正确执行 
            richTextBox1.SelectAll();
        }
 
 


你可能感兴趣的:(C#)