📄 editor.cs
字号:
FileName=this.Text;
CanWrite=true;
}
if(CanWrite)
{
try
{
StreamWriter sw=File.CreateText(FileName);
sw.Write(txtEditor.Text);
sw.Flush();
sw.Close();
this.Text=FileName;
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
public void DoSave()
{
if(this.Text=="Untitled")
{
DoSaveAs(true);
}
DoSaveAs(false);
IsContentModified=false;
}
private void DoNew()
{
if(IsContentModified)
{
string Message="The text in the " + this.Text + " has been changed.\n Do you want to Save Changes?";
switch(MessageBox.Show(Message,"SuperPad",MessageBoxButtons.YesNoCancel))
{
case DialogResult.Yes:
DoSave();
txtEditor.Text="";
this.Text="Untitled";
break;
case DialogResult.No:
txtEditor.Text="";
this.Text="Untitled";
break;
case DialogResult.Cancel:
break;
}
}
else
{
txtEditor.Text="";
this.Text="Untitled";
}
}
private void DoOpen()
{
OpenFileDialog dlgOpen=new OpenFileDialog();
dlgOpen.Filter="Text Files (*.txt)|*.txt|C# Files|*.cs|All Files (*.*)|*.*";
dlgOpen.FilterIndex=2;
dlgOpen.RestoreDirectory=true;
string FileName="";
if(dlgOpen.ShowDialog()==DialogResult.OK)
{
try
{
//Show busy cursor
//但ScrollBar仍可接受处理事件,这样就引起大文本读取时显示问题!!!???
Cursor.Current = Cursors.WaitCursor;
FileName=dlgOpen.FileName;
StreamReader sr=File.OpenText(FileName);
txtEditor.Text="";
string strLine;
while((strLine=sr.ReadLine())!=null)
{
txtEditor.Text+=strLine+"\n";
}
txtEditor.SelectionStart=0;
txtEditor.SelectionLength=0;
this.Text=FileName;
sr.Close();
Cursor.Current = Cursors.Default;//Reset default
}
catch(Exception e)
{
MessageBox.Show("Error Occurred:\n" + e.ToString());
}
}
}
/// <summary>
/// 注意好象对话框的效果、颜色、帮助等等都可设定是否显示。
/// </summary>
public void SetFont()
{
FontDialog dlgFont=new FontDialog();
dlgFont.Font=txtEditor.Font;
dlgFont.ShowColor=true;
dlgFont.Color=txtEditor.ForeColor;
if(dlgFont.ShowDialog()==DialogResult.OK)
{
txtEditor.Font=dlgFont.Font;
txtEditor.ForeColor=dlgFont.Color;
}
}
public int GoTo(string LineNumber)
{
//看来Form是对话框窗口类,ShowDialog用于显示模式窗口
//Show用于显示非模式窗口--继承自Control
Form frmDialog=new Form();
TextBox LineBox=new TextBox();
Button cmdOK=new Button();
Button cmdCancel=new Button();
cmdOK.Text="OK";
cmdOK.DialogResult=DialogResult.OK;
cmdOK.Click-=new EventHandler(Find_Click);
cmdCancel.Text="Cancel";
cmdCancel.DialogResult=DialogResult.Cancel;
LineBox.Text=LineNumber;
if(LineBox.Text.Length==0) LineBox.Text="0";
frmDialog.Size=new Size(230,100);
frmDialog.Text="Go to Line";
frmDialog.AcceptButton=cmdOK;
frmDialog.CancelButton=cmdCancel;
frmDialog.MaximizeBox=false;
frmDialog.MinimizeBox=false;
frmDialog.FormBorderStyle=FormBorderStyle.FixedDialog;
frmDialog.Location=new Point(this.Left+Math.Abs((this.Width-frmDialog.Width))/2,this.Top + 100);
LineBox.Location=new Point(10,10);
cmdOK.Location=new Point(LineBox.Left+LineBox.Width+20,10);
cmdCancel.Location=new Point(cmdOK.Left,cmdOK.Top+cmdOK.Height+10);
frmDialog.Controls.Add(LineBox);
frmDialog.Controls.Add(cmdOK);
frmDialog.Controls.Add(cmdCancel);
frmDialog.ShowDialog(this);
if(frmDialog.DialogResult==DialogResult.OK)
{
try
{
frmDialog.Dispose();
return Int32.Parse(LineBox.Text);
}
catch(Exception)
{
return -1;
}
}
return 0;
}
private void ShowFind()
{
TextBox txtFind=new TextBox();
Label lblFind=new Label();
CheckBox chkMatch=new CheckBox();
GroupBox grpDirection=new GroupBox();
RadioButton optUp=new RadioButton();
RadioButton optDown=new RadioButton();
Button cmdOK=new Button();
Button cmdCancel=new Button();
cmdOK.Text="Find Next";
cmdOK.DialogResult=DialogResult.OK;
cmdOK.Click+=new EventHandler(Find_Click);
cmdCancel.Text="Cancel";
cmdCancel.DialogResult=DialogResult.Cancel;
cmdCancel.Click+=new EventHandler(Cancel_Click);
Form frmDialog=new Form();
frmDialog.Size=new Size(380,150);
frmDialog.Text="Find";
frmDialog.MaximizeBox=false;
frmDialog.MinimizeBox=false;
frmDialog.FormBorderStyle=FormBorderStyle.FixedDialog;
lblFind.Location=new Point(5,15);
lblFind.Text="Fi&nd what:";
lblFind.AutoSize=true;
txtFind.Location=new Point(lblFind.Left+lblFind.Width+5,lblFind.Top);
txtFind.Size=new Size(200,txtFind.Height);
txtFind.Text=TextToFind;
txtFind.TextChanged+=new EventHandler(txtFind_Change);
cmdOK.Location=new Point(txtFind.Left+txtFind.Width+5,lblFind.Top-2);
cmdCancel.Location=new Point(cmdOK.Left,cmdOK.Top+cmdOK.Height+5);
grpDirection.Location=new Point(lblFind.Left,cmdCancel.Top+cmdCancel.Height);
grpDirection.Size=new Size(120,45);
grpDirection.Text="Match Direction";
optUp.Location=new Point(10,15);
optUp.Click+=new EventHandler(Direction_Click);
optUp.Text="&Up";
optUp.Width=45;
optDown.Text="&Down";
optDown.Width=55;
optDown.Click+=new EventHandler(Direction_Click);
optDown.Checked=true;
optDown.Location=new Point(optUp.Left+optUp.Width,optUp.Top);
grpDirection.Controls.Add(optUp);
grpDirection.Controls.Add(optDown);
chkMatch.Location=new Point(grpDirection.Left+grpDirection.Width+5,grpDirection.Top+(grpDirection.Height-chkMatch.Height)/2);
chkMatch.Text="Match Case";
if(txtFind.Text.Trim().Length==0) cmdOK.Enabled=false;
frmDialog.Controls.Add(txtFind);
frmDialog.Controls.Add(lblFind);
frmDialog.Controls.Add(grpDirection);
//frmDialog.Controls.Add(chkMatch);
frmDialog.Controls.Add(cmdOK);
frmDialog.Controls.Add(cmdCancel);
frmDialog.ShowInTaskbar=false;
frmDialog.TopMost=true;
frmDialog.Show();
}
private void ShowReplace()
{
TextBox txtFind=new TextBox();
TextBox txtReplace=new TextBox();
Label lblFind=new Label();
Label lblReplace=new Label();
CheckBox chkMatch=new CheckBox();
GroupBox grpDirection=new GroupBox();
RadioButton optUp=new RadioButton();
RadioButton optDown=new RadioButton();
Button cmdOK=new Button();
Button cmdReplaceALL=new Button();
Button cmdCancel=new Button();
cmdOK.Text="Replace";
cmdOK.DialogResult=DialogResult.OK;
cmdOK.Click+=new EventHandler(Replace_Click);
cmdReplaceALL.Text="Replace All";
cmdReplaceALL.DialogResult=DialogResult.OK;
cmdReplaceALL.Click+=new EventHandler(ReplaceALL_Click);
cmdCancel.Text="Cancel";
cmdCancel.DialogResult=DialogResult.Cancel;
cmdCancel.Click+=new EventHandler(Cancel_Click);
Form frmDialog=new Form();
frmDialog.Size=new Size(380,200);
frmDialog.Text="Replace";
frmDialog.MaximizeBox=false;
frmDialog.MinimizeBox=false;
frmDialog.FormBorderStyle=FormBorderStyle.FixedDialog;
lblFind.Location=new Point(5,15);
lblFind.Text="Find:";
lblFind.AutoSize=true;
txtFind.Location=new Point(lblFind.Left+lblFind.Width+5,lblFind.Top);
txtFind.Size=new Size(200,txtFind.Height);
txtFind.Text=TextToFind;
txtFind.TextChanged+=new EventHandler(txtFind_Change);
lblReplace.Location=new Point(lblFind.Left,lblFind.Top+lblFind.Height+15);
lblReplace.Text="Replace:";
lblReplace.AutoSize=true;
txtReplace.Location=new Point(lblReplace.Left+lblReplace.Width+5,lblReplace.Top);
txtReplace.Size=new Size(200,txtFind.Height);
txtFind.Left=txtReplace.Left;
txtReplace.Text=TextToReplace;
txtReplace.TextChanged+=new EventHandler(txtReplace_Change);
cmdOK.Location=new Point(txtFind.Left+txtFind.Width+5,lblFind.Top-2);
cmdReplaceALL.Location=new Point(cmdOK.Left,cmdOK.Top+cmdOK.Height+5);
cmdCancel.Location=new Point(cmdReplaceALL.Left,cmdReplaceALL.Top+cmdReplaceALL.Height+5);
grpDirection.Location=new Point(lblReplace.Left,cmdCancel.Top+cmdCancel.Height);
grpDirection.Size=new Size(120,45);
grpDirection.Text="Match Direction";
optUp.Location=new Point(10,15);
optUp.Click+=new EventHandler(Direction_Click);
optUp.Text="&Up";
optUp.Width=45;
optDown.Text="&Down";
optDown.Width=55;
optDown.Click+=new EventHandler(Direction_Click);
optDown.Checked=true;
optDown.Location=new Point(optUp.Left+optUp.Width,optUp.Top);
grpDirection.Controls.Add(optUp);
grpDirection.Controls.Add(optDown);
chkMatch.Location=new Point(grpDirection.Left+grpDirection.Width+5,grpDirection.Top+(grpDirection.Height-chkMatch.Height)/2);
chkMatch.Text="Match Case";
if(txtFind.Text.Trim().Length==0)
{
cmdOK.Enabled=false;
cmdReplaceALL.Enabled=false;
}
frmDialog.Controls.Add(txtFind);
frmDialog.Controls.Add(lblFind);
frmDialog.Controls.Add(txtReplace);
frmDialog.Controls.Add(lblReplace);
frmDialog.Controls.Add(grpDirection);
//frmDialog.Controls.Add(chkMatch);
frmDialog.Controls.Add(cmdOK);
frmDialog.Controls.Add(cmdReplaceALL);
frmDialog.Controls.Add(cmdCancel);
frmDialog.ShowInTaskbar=false;
frmDialog.TopMost=true;
frmDialog.Show();
}
private void txtFind_Change(object sender,EventArgs eArgs)
{
TextToFind=((TextBox)sender).Text;
Form frmTemp=(Form)(((Control)sender).Parent);
for(int i=0;i<frmTemp.Controls.Count;i++)
{
if(frmTemp.Controls[i].GetType() ==typeof(Button) && frmTemp.Controls[i].Text!="&Cancel")
{
frmTemp.Controls[i].Enabled=(((TextBox)sender).Text.Length>0);
}
}
frmTemp=null;
}
private void txtReplace_Change(object sender,EventArgs eArgs)
{
TextToReplace=((TextBox)sender).Text;
}
private void Direction_Click(object sender,EventArgs eArgs)
{
string Caption=((RadioButton)sender).Text;
if(Caption=="&Up")
IsDirectionDownward=false;
else
IsDirectionDownward=true;
}
private void Find_Click(object sender,EventArgs eArgs)
{
int Pos=-1;
if(IsDirectionDownward)
{
if(txtEditor.SelectionLength==0)
Pos=txtEditor.Text.IndexOf(TextToFind,txtEditor.SelectionStart);
else
Pos=txtEditor.Text.IndexOf(TextToFind,txtEditor.SelectionStart+txtEditor.SelectionLength);
}
else
{
if(txtEditor.SelectionStart>0)
Pos=txtEditor.Text.LastIndexOf(TextToFind,txtEditor.SelectionStart-1);
}
if(Pos!=-1)
{
txtEditor.SelectionStart=Pos;
txtEditor.SelectionLength=TextToFind.Length;
if(sender!=null) ((Control)sender).Focus();
}
else
{
MessageBox.Show("Cannot Find: \"" + TextToFind + "\"");
}
}
private void Replace_Click(object sender,EventArgs eArgs)
{
Find_Click(null,null);
if(txtEditor.SelectionLength>0)
{
txtEditor.SelectedText=TextToReplace;
txtEditor.SelectionStart=txtEditor.SelectionStart+TextToReplace.Length;
}
}
private void ReplaceALL_Click(object sender,EventArgs eArgs)
{
txtEditor.Text=Replace(txtEditor.Text,TextToFind,TextToReplace);
}
private string Replace(string StrSource,string StrFind,string StrReplace)
{
int iPos=StrSource.IndexOf(StrFind);
String StrReturn="";
while(iPos!=-1)
{
StrReturn+=StrSource.Substring(0,iPos)+StrReplace;
StrSource=StrSource.Substring(iPos+StrFind.Length);
iPos=StrSource.IndexOf(StrFind);
}
if(StrSource.Length>0)
StrReturn+=StrSource;
return StrReturn;
}
private void Cancel_Click(object sender,EventArgs eArgs)
{
((Form)((Control)sender).Parent).Close();
}
// To Start the Editor
[STAThreadAttribute]
public static void Main()
{
Application.Run(new Editor());
}
/// <summary>
/// //To Set Line,Col in StatusBar
/// </summary>
private void ShowInform()
{
try
{
int ColCount=1;
int RowCount=1;
int Pos;
//To Set Column
if(txtEditor.SelectionStart>-1)
{
Pos=txtEditor.Text.LastIndexOf("\n",txtEditor.SelectionStart);
if(Pos>-1)
{
//If the cursor is at CRLF
if(Pos!=txtEditor.SelectionStart)
ColCount=txtEditor.SelectionStart-Pos;
else
{
//Col position is diff between PrevEnter and CurPos
Pos=txtEditor.Text.LastIndexOf("\n",txtEditor.SelectionStart-1);
ColCount=txtEditor.SelectionStart-Pos;
}
}
else
{
ColCount=txtEditor.SelectionStart+1;
}
while(Pos>-1)
{
RowCount++;
Pos=txtEditor.Text.LastIndexOf("\n",Pos-1);
}
}
sbEditor.Panels[1].Text="Col: " + ColCount.ToString();
sbEditor.Panels[0].Text="Line: " + RowCount.ToString();
}
catch(Exception)
{
}
}
/// <summary>
/// 提示是否需要保存,当消息框取消时返回false(供Closing事件用)
/// </summary>
public bool WarnSave()
{
bool retval=true;
if(IsContentModified)
{
string Message="The text in the " + this.Text + " has been changed.\n Do you want to Save Changes?";
switch (MessageBox.Show(Message,"SuperPad",MessageBoxButtons.YesNoCancel))
{
case DialogResult.Yes:
DoSave();
break;
case DialogResult.No:
break;
case DialogResult.Cancel:
retval=false;
break;
}
}
return retval;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -