2009-07-24 21:22:27
最近自己用C# windows应用程序写了两个记事本,一个和系统自带的记事本一样,一个是多文档界面的。
两个程序用的代码大同小异,最大的不同就是多文档界面的需要判断当前活动窗口,然后才实现功能。
两个程序遇到的共同问题就是关闭窗口时,判断其文档内容是否已被修改,提示保存;我的解决办法就是创建一个集合,在新建文件和打开文件时,把文件内容存到集合里去;在关闭窗口时比较当前文件内容和集合里的是否一致,不一致则提示保存。
还遇到一个问题,就是实现打开文件功能时,由于有“所有文件”在,所以不能由FilterIndex来判断要打开哪种类型的文件,我是直接判断文件路径是否包含“.txt”或“.rtf”来确定打开的文件是属于哪种类型的。保存文件时则直接FilterIndex来判断。
以上两个问题或许还有更好的解决方法,欢迎留言共同探讨下~~!
其它的都是些小问题,当程序编好,生成可执行的应用程序后,我把文档的打开方式设置成我自己编的程序打开,可双击打开文档后,都只是打开我自己编的程序,而不是直接进入到那个文档······找不到解决的办法,望告知!
以下给出一个记事本的代码:
- 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; - namespace shimineditor
{
public partial class editorForm1 : Form
{
public ArrayList text = new ArrayList();
string filepath = null;
public editorForm1()
{
InitializeComponent();
text.Add(this.rtbtext.ToString());
} - 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);
}
} - private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
savefile();
text.Add(this.rtbtext);
} - private void savefile()
{
if (this.filepath == null)
{
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
filepath = saveFileDialog1.FileName.ToString();
}
else
{
return;
}
}
if (saveFileDialog1.FilterIndex == 1)
{ - //加载文件
this.rtbtext.SaveFile(filepath, RichTextBoxStreamType.PlainText);
}
if (saveFileDialog1.FilterIndex == 2)
{ - //加载文件
this.rtbtext.SaveFile(filepath, RichTextBoxStreamType.RichText);
}
this.Text = "记事本" + filepath;
text.Add(this.rtbtext);
} - private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
filepath = saveFileDialog1.FileName.ToString();
}
else
{
return;
}
if (saveFileDialog1.FilterIndex == 1)
{ - //加载文件
this.rtbtext.SaveFile(filepath, RichTextBoxStreamType.PlainText);
}
if (saveFileDialog1.FilterIndex == 2)
{ - //加载文件
this.rtbtext.SaveFile(filepath, RichTextBoxStreamType.RichText);
}
this.Text = "记事本" + filepath;
text.Add(this.rtbtext);
} - private void 字体设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.fontDialog1.ShowDialog() == DialogResult.OK)
{
this.rtbtext.SelectionFont = fontDialog1.Font;
}
} - private void 颜色设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.colorDialog1.ShowDialog() == DialogResult.OK)
{
this.rtbtext.SelectionColor = colorDialog1.Color;
}
} - 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();
}
} - private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
} - private void editorForm1_FormClosing(object sender, FormClosingEventArgs e)
{
warming();
} - private void warming()
{
if (this.rtbtext.ToString().Equals(text[0].ToString()))
{ - }
else
{
DialogResult result;
result = MessageBox.Show("数据已更改,是否保存", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result.Equals(DialogResult.Yes))
{
savefile();
} - }
} - private void rtbtext_TextChanged(object sender, EventArgs e)
{ - }
- private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
{
this.rtbtext.Undo();
} - private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
this.rtbtext.Copy();
} - private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
{
this.rtbtext.Paste();
} - private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
{
this.rtbtext.Cut();
} - private void 重复RToolStripMenuItem_Click(object sender, EventArgs e)
{
this.rtbtext.Redo();
} - private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
{
} - private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
{
}
}
}