文本编辑器代码.docx
《文本编辑器代码.docx》由会员分享,可在线阅读,更多相关《文本编辑器代码.docx(16页珍藏版)》请在冰豆网上搜索。
文本编辑器代码
文本编辑器代码
MdiFrm
privatevoid新建NToolStripMenuItem_Click(objectsender,EventArgse)
{
stringnewFormTitle="新建文本文档";
intiOrder=0;//保存当前应生成的文本文档的窗体标题序号
if(this.MdiChildren!
=null&&this.MdiChildren.Length==0)
{
iOrder=1;
}
else
{
//辅助设计数组,用来存放已经自动生成的窗体的标题序号
int[]iArr=newint[this.MdiChildren.Length];
foreach(FormtempFrminthis.MdiChildren)
{
stringtitle=tempFrm.Text;
Int32iTemp=0;
if(title.StartsWith(newFormTitle)&&
Int32.TryParse(title.Substring(newFormTitle.Length),outiTemp))
{
iArr[iOrder++]=iTemp;
}
}
Array.Sort(iArr);
iOrder=1;
foreach(intiiniArr)
{
if(i==0)
continue;
else
{
if(i==iOrder)
{
iOrder++;
//break;
}
else
{
break;
}
}
}
}
DocumentFrmfrm=newDocumentFrm();
frm.MdiParent=this;
frm.Text=String.Format(newFormTitle+"{0}",iOrder);
frm.Show();
}
privatevoid打开OToolStripMenuItem_Click(objectsender,EventArgse)
{
OpenFileDialogofd=newOpenFileDialog();
ofd.Filter="富文本文档(*.rtf)|*.rtf|文本文档(*.txt)|*.txt|日志文档(*.log)|(*.log)|所有文档(*.*)|*.*";
if(ofd.ShowDialog()==DialogResult.OK)
{
DocumentFrmnewFrm=newDocumentFrm();
newFrm.MdiParent=this;
newFrm.Tag=ofd.FileName;
//newFrm.Name=ofd.SafeFileName;
//if(this.tsmiSettingStatus.Checked)
//newFrm.ssDocumentStatus.Visible=true;
//else
//newFrm.ssDocumentStatus.Visible=false;
try
{
if(ofd.FileName.EndsWith(".txt",true,null))
{
newFrm.rtbContent.LoadFile(ofd.FileName,
RichTextBoxStreamType.PlainText);
}
else
{
if(ofd.FileName.EndsWith(".rtf",true,null))
{
newFrm.rtbContent.LoadFile(ofd.FileName,
RichTextBoxStreamType.RichText);
}
else
{
newFrm.rtbContent.Text=File.ReadAllText(ofd.FileName,
Encoding.Default);
}
}
newFrm.Show();
}
catch(Exceptionex)
{
MessageBox.Show(ex.ToString());
}
}
}
DocumentFrmpublicpartialclassDocumentFrm:
Form
{
publicboolisChanged=false;
publicstringfindString="";
publicintswitchToRowIndex=0;
privateSystem.Drawing.Printing.PrintDocumentprintDocument=newSystem.Drawing.Printing.PrintDocument();
publicDocumentFrm()
{
InitializeComponent();
printDocument.PrintPage+=new
System.Drawing.Printing.PrintPageEventHandler(printDocument_PrintPage);
}
privatevoidDocumentFrm_FormClosing(objectsender,FormClosingEventArgse)
{
if(this.isChanged)
{
//若是打开的文档,则提示是否覆盖当前文档;
//若是新建的文档,则打开文件对话框,让用户选择保存路径;
if(this.Tag!
=null&&!
String.IsNullOrEmpty(this.Tag.ToString()))
{
if(MessageBox.Show(String.Format("是否保存到{0}",this.Tag.ToString()),
"保存文档",MessageBoxButtons.OKCancel)==DialogResult.OK)
{
//覆盖原来的文件
this.rtbContent.SaveFile(this.Tag.ToString());
}
}
else
{
SaveFileDialogsfd=newSaveFileDialog();
if(sfd.ShowDialog()==DialogResult.OK)
{
this.rtbContent.SaveFile(sfd.FileName);
}
}
}
}
privatevoidtsmiColor_Click(objectsender,EventArgse)
{//定义一个颜色对话框对象
ColorDialogcd=newColorDialog();
//将当前RichTextBox中选中的文本颜色赋给颜色对话框对象cd,使打开的窗体有初始化值
if(String.IsNullOrEmpty(this.rtbContent.SelectedText))
{
cd.Color=this.rtbContent.ForeColor;
}
else
{
cd.Color=this.rtbContent.SelectionColor;
}
//确定,则用当前选中的颜色修改RichTextBox中选中内容的颜色
if(cd.ShowDialog()==DialogResult.OK)
{
this.rtbContent.SelectionColor=cd.Color;
}
}
privatevoidtsmiFont_Click(objectsender,EventArgse)
{
//定义一个字体对话框对象
FontDialogfd=newFontDialog();
//将当前RichTextBox中选中的文本字体赋给字体对话框对象fd,使打开的窗体有初始化值
if(String.IsNullOrEmpty(this.rtbContent.SelectedText))
{
fd.Color=rtbContent.ForeColor;
fd.Font=rtbContent.Font;
}
else
{
fd.Color=rtbContent.SelectionColor;
fd.Font=rtbContent.SelectionFont;
}
//确定,则用当前选中的字体修改RichTextBox中选中内容的字体
if(fd.ShowDialog()==DialogResult.OK)
{
rtbContent.SelectionColor=fd.Color;
rtbContent.SelectionFont=fd.Font;
}
}
privatevoidtsmiSave_Click(objectsender,EventArgse)
{
if(this.Tag==null)
{
tsmiSaveAs_Click(sender,e);
}
else
{
//File.WriteAllText(this.Tag.ToString(),this.rtbForWirte.Text);
try
{
if(this.Tag.ToString().EndsWith(".txt",true,null))
{
this.rtbContent.SaveFile(this.Tag.ToString(),RichTextBoxStreamType.PlainText);
}
else
{
if(this.Tag.ToString().EndsWith(".rtf",true,null))
{
this.rtbContent.SaveFile(this.Tag.ToString(),RichTextBoxStreamType.RichText);
}
else
{
File.WriteAllText(this.Tag.ToString(),this.rtbContent.Text);
}
}
}
catch(Exceptionex)
{
MessageBox.Show(ex.ToString());
}
}
}
privatevoidtsmiSaveAs_Click(objectsender,EventArgse)
{
SaveFileDialogsfd=newSaveFileDialog();
sfd.Filter="富文本文档(*.rtf)|*.rtf|文本文档(*.txt)|*.txt|日志文档(*.log)|(*.log)|所有文档(*.*)|*.*";
sfd.AddExtension=true;
if(sfd.ShowDialog()==DialogResult.OK)
{
//File.WriteAllText(sfd.FileName,this.rtbForWirte.Text,E