C#编的记事本

2009-07-24 21:22:27

 

最近自己用C# windows应用程序写了两个记事本,一个和系统自带的记事本一样,一个是多文档界面的。

 

两个程序用的代码大同小异,最大的不同就是多文档界面的需要判断当前活动窗口,然后才实现功能。

 

两个程序遇到的共同问题就是关闭窗口时,判断其文档内容是否已被修改,提示保存;我的解决办法就是创建一个集合,在新建文件和打开文件时,把文件内容存到集合里去;在关闭窗口时比较当前文件内容和集合里的是否一致,不一致则提示保存。

 

还遇到一个问题,就是实现打开文件功能时,由于有“所有文件”在,所以不能由FilterIndex来判断要打开哪种类型的文件,我是直接判断文件路径是否包含“.txt”或“.rtf”来确定打开的文件是属于哪种类型的。保存文件时则直接FilterIndex来判断。

 

以上两个问题或许还有更好的解决方法,欢迎留言共同探讨下~~!

 

其它的都是些小问题,当程序编好,生成可执行的应用程序后,我把文档的打开方式设置成我自己编的程序打开,可双击打开文档后,都只是打开我自己编的程序,而不是直接进入到那个文档······找不到解决的办法,望告知!

 

 

以下给出一个记事本的代码:

 

  1. using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
  2. namespace shimineditor
    {
       
        public partial class editorForm1 : Form
        {
            public ArrayList text = new ArrayList();
            string filepath = null;
            public editorForm1()
            {
                InitializeComponent();
                text.Add(this.rtbtext.ToString());
            }
  3.         private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.filepath = this.openFileDialog1.FileName;     //获得文件路径
                    string f="";
                    f = filepath.Substring(filepath.LastIndexOf("."));
                    if (f.ToLower() == ".txt")                                      //加载文件
                    {
                        this.rtbtext.LoadFile(filepath, RichTextBoxStreamType.PlainText);
                    }
                    if (f.ToLower() == ".rtf")
                    {
                        this.rtbtext.LoadFile(filepath, RichTextBoxStreamType.RichText);
                    }
                    this.Text = "记事本" + filepath;
                    text.Add(this.rtbtext);
                }
            }
  4.         private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
            {
                savefile();
                text.Add(this.rtbtext);
            }
  5.         private void savefile()
            {
                if (this.filepath == null)
                {
                    if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        filepath = saveFileDialog1.FileName.ToString();
                    }
                    else
                    {
                        return;
                    }
                }
                if (saveFileDialog1.FilterIndex == 1)
                {
  6.                 //加载文件
                    this.rtbtext.SaveFile(filepath, RichTextBoxStreamType.PlainText);
                }
                if (saveFileDialog1.FilterIndex == 2)
                {
  7.                 //加载文件
                    this.rtbtext.SaveFile(filepath, RichTextBoxStreamType.RichText);
                }
                this.Text = "记事本" + filepath;
                text.Add(this.rtbtext);
            }
  8.         private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
            {
                  if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        filepath = saveFileDialog1.FileName.ToString();
                    }
                    else
                    {
                        return;
                    }
               
                if (saveFileDialog1.FilterIndex == 1)
                {
  9.                 //加载文件
                    this.rtbtext.SaveFile(filepath, RichTextBoxStreamType.PlainText);
                }
                if (saveFileDialog1.FilterIndex == 2)
                {
  10.                 //加载文件
                    this.rtbtext.SaveFile(filepath, RichTextBoxStreamType.RichText);
                }
                this.Text = "记事本" + filepath;
                text.Add(this.rtbtext);
            }
  11.         private void 字体设置ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (this.fontDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.rtbtext.SelectionFont = fontDialog1.Font;
                }
            }
  12.         private void 颜色设置ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (this.colorDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.rtbtext.SelectionColor = colorDialog1.Color;
                }
            }
  13.         private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (this.rtbtext.ToString().Equals(text[0].ToString()))
                {
                    this.rtbtext.Clear();
                }
                else
                {
                    DialogResult result;
                    result = MessageBox.Show("数据已更改,是否保存", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                                   
                    if (result.Equals(DialogResult.Yes))
                    {
                        savefile();
                       
                    }
                    this.rtbtext.Clear();
                }
            }
  14.         private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Close();
            }
  15.         private void editorForm1_FormClosing(object sender, FormClosingEventArgs e)
            {
                warming();
            }
  16.         private void warming()
            {
                if (this.rtbtext.ToString().Equals(text[0].ToString()))
                {
  17.             }
                else
                {
                    DialogResult result;
                    result = MessageBox.Show("数据已更改,是否保存", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    
                    if (result.Equals(DialogResult.Yes))
                    {
                        savefile();
                       
                    }
  18.             }
            }
  19.         private void rtbtext_TextChanged(object sender, EventArgs e)
            {
  20.         }
  21.         private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.rtbtext.Undo();
            }
  22.         private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.rtbtext.Copy();
            }
  23.         private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.rtbtext.Paste();
            }
  24.         private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.rtbtext.Cut();
            }
  25.         private void 重复RToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.rtbtext.Redo();
            }
  26.         private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
            {
              
            }
  27.         private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
            {
               
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值