📄 form1.cs
字号:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace TextEditor
{
public partial class Form1 : Form
{
#region 全局变量
private bool docHasData = false; //是否加载了文件
private bool docHasChanged = false; //文本是否发生改变
private string docName=""; //当前加载的文件名
private bool docHasSaved=false; //当前文件是否保存
private int row, field; //行列
private bool autoNewLine = false; //自动换行
#endregion
#region 窗体载入时的处理
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.CenterToScreen();
this.richTextBox1.Location = new System.Drawing.Point(0, 24 + this.toolBar1.Height);
this.richTextBox1.Size = new System.Drawing.Size(this.Width - 10, this.Height - this.toolBar1.Height - this.menuStrip1.Height - 36 - this.statusBar1.Height);
this.statusBar1.Width = this.Width - 8;
this.statusBarLocation.Width = statusBar1.Width/3;
this.statusBarLines.Width = statusBar1.Width/3;
this.statusBarTime.Width = statusBar1.Width - statusBarLocation.Width - statusBarLines.Width;
}
#endregion
#region 窗体和文本框接受拖进来的数据的事件处理
private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Link;
else
e.Effect = DragDropEffects.None;
}
private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
if (!docHasData || !docHasChanged || docHasSaved)
{
docName = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
if (docName != "")
this.Text = "TextEditor - " + docName;
if (docName!= "")
{
FileStream file = new FileStream(docName, FileMode.Open, FileAccess.ReadWrite);
StreamReader reader = new StreamReader(file, System.Text.Encoding.GetEncoding("GB2312"));
richTextBox1.Text = reader.ReadToEnd();
docHasData = true;
reader.Close();
file.Close();
}
}
else if (docHasChanged || !docHasSaved)
{
string strask1;
if (docName != "")
strask1 = "是否保存: " + docName + " ?";
else
strask1 = "是否保存 未命名.txt ?";
DialogResult asksave = MessageBox.Show(strask1, "退出", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (asksave == DialogResult.Yes)
{
menuFileSave_Click(sender, e);
}
docName = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
if (docName != "")
this.Text = "TextEditor - " + docName;
if (docName != "")
{
FileStream file = new FileStream(docName, FileMode.Open, FileAccess.ReadWrite);
StreamReader reader = new StreamReader(file, System.Text.Encoding.GetEncoding("GB2312"));
richTextBox1.Text = reader.ReadToEnd();
docHasData = true;
reader.Close();
file.Close();
}
}
}
private void richTextBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Link;
else
e.Effect = DragDropEffects.None;
}
private void richTextBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
if (!docHasData || !docHasChanged || docHasSaved)
{
docName = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
if (docName != "")
this.Text = "TextEditor - " + docName;
if (docName != "")
{
FileStream file = new FileStream(docName, FileMode.Open, FileAccess.ReadWrite);
StreamReader reader = new StreamReader(file, System.Text.Encoding.GetEncoding("GB2312"));
richTextBox1.Text = reader.ReadToEnd();
docHasData = true;
reader.Close();
file.Close();
}
}
else if (docHasChanged || !docHasSaved)
{
string strask1;
if (docName != "")
strask1 = "是否保存: " + docName + " ?";
else
strask1 = "是否保存 未命名.txt ?";
DialogResult asksave = MessageBox.Show(strask1, "退出", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (asksave == DialogResult.Yes)
{
menuFileSave_Click(sender, e);
}
docName = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
if (docName != "")
this.Text = "TextEditor - " + docName;
if (docName != "")
{
FileStream file = new FileStream(docName, FileMode.Open, FileAccess.ReadWrite);
StreamReader reader = new StreamReader(file, System.Text.Encoding.GetEncoding("GB2312"));
richTextBox1.Text = reader.ReadToEnd();
docHasData = true;
reader.Close();
file.Close();
}
}
}
#endregion
#region 改变窗体大小时的处理
//保证文本框等能够根据窗体大小的改变而自适应地改变大小
private void Form1_ClientSizeChanged(object sender, EventArgs e)
{
this.richTextBox1.Location = new System.Drawing.Point(0, 24+this.toolBar1.Height);
this.richTextBox1.Size = new System.Drawing.Size(this.Width-10, this.Height-this.toolBar1.Height-this.menuStrip1.Height-36-this.statusBar1.Height);
this.statusBar1.Width = this.Width - 8;
this.statusBarLocation.Width = statusBar1.Width/3;
this.statusBarLines.Width = statusBar1.Width/3;
this.statusBarTime.Width = statusBar1.Width-statusBarLocation.Width-statusBarLines.Width;
}
#endregion
#region 点击文本框以及在文本框中按下键和文本框内容改变时的处理
private void richTextBox1_Click(object sender, EventArgs e)
{
int begin = 0;
int fronting = richTextBox1.SelectionStart;
row = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
while (begin < fronting)
{
if (row == richTextBox1.GetLineFromCharIndex(begin))
{
break;
}
else
{
begin++;
}
}
field = fronting - begin;
row++;
field++;
statusBarLocation.Text = "行:" + row + " 列:" + field;
}
private void richTextBox1_KeyDown(object sender, EventArgs e)
{
int begin = 0;
int fronting = richTextBox1.SelectionStart;
row = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
while (begin < fronting)
{
if (row == richTextBox1.GetLineFromCharIndex(begin))
{
break;
}
else
{
begin++;
}
}
field = fronting - begin;
row++;
field++;
statusBarLocation.Text = "行:" + row + " 列:" + field;
docHasChanged = true;
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
docHasChanged = true;
string[] lines = richTextBox1.Lines;
statusBarLines.Text = "共有 " + (lines.Length) + " 个回车";
}
#endregion
#region 工具条按钮处理
private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
switch (e.Button.Name)
{
case "toolBarNew":
menuFileNew_Click(sender, e);
break;
case "toolBarOpen":
menuFileOpen_Click(sender, e);
break;
case "toolBarSave":
menuFileSave_Click(sender, e);
break;
case "toolBarUndo":
menuEditUndo_Click(sender, e);
break;
case "toolBarSelectAll":
menuEditSelectAll_Click(sender, e);
break;
case "toolBarCut":
richTextBox1.Cut();
break;
case "toolBarCopy":
richTextBox1.Copy();
break;
case "toolBarPaste":
richTextBox1.Paste();
break;
case "toolBarDelete":
menuEditDelete_Click(sender, e);
break;
case "toolBarFind":
menuEditFind_Click(sender, e);
break;
case "toolBarReplace":
menuEditReplace_Click(sender, e);
break;
case "toolBarFont":
FontDialog font = new FontDialog();
if (font.ShowDialog() == DialogResult.OK)
richTextBox1.SelectionFont= font.Font;
docHasChanged = true;
break;
case "toolBarColor":
ColorDialog color = new ColorDialog();
if (color.ShowDialog() == DialogResult.OK)
richTextBox1.SelectionColor = color.Color;
docHasChanged = true;
break;
case "toolBarHelp":
menuHelpTheme_Click(sender, e);
break;
case "toolBarAbout":
menuHelpAbout_Click(sender, e);
break;
}
}
#endregion
#region 所有菜单处理
//
//打开
//
private void menuFileOpen_Click(object sender, EventArgs e)
{
if (!docHasData || !docHasChanged || docHasSaved)
{
openFileDialog1.ShowDialog();
docName = openFileDialog1.FileName;
if (docName != "")
this.Text = "TextEditor - " + docName;
if (openFileDialog1.FileName != "")
{
FileStream file = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.ReadWrite);
StreamReader reader = new StreamReader(file, System.Text.Encoding.GetEncoding("GB2312"));
richTextBox1.Text = reader.ReadToEnd();
docHasData = true;
docHasSaved = false;
reader.Close();
file.Close();
}
}
else if (docHasChanged || !docHasSaved)
{
string strask1;
if (docName != "")
strask1 = "是否保存: " + docName + " ?";
else
strask1 = "是否保存 未命名.txt ?";
DialogResult asksave = MessageBox.Show(strask1, "退出", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (asksave == DialogResult.Yes)
{
menuFileSave_Click(sender, e);
}
openFileDialog1.ShowDialog();
docName = openFileDialog1.FileName;
if (docName != "")
this.Text = "TextEditor - " + docName;
if (openFileDialog1.FileName != "")
{
FileStream file = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.ReadWrite);
StreamReader reader = new StreamReader(file, System.Text.Encoding.GetEncoding("GB2312"));
richTextBox1.Text = reader.ReadToEnd();
docHasData = true;
file.Close();
reader.Close();
docHasSaved = false;
}
}
}
//
//全选
//
private void menuEditSelectAll_Click(object sender, EventArgs e)
{
richTextBox1.SelectAll();
}
//
//剪切
//
private void menuEditCut_Click(object sender, EventArgs e)
{
richTextBox1.Cut();
}
//
//复制
//
private void menuEditCopy_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}
//
//粘贴
//
private void menuEditPaste_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
//
//插入时期时间
//
private void menuEditDate_Click(object sender, EventArgs e)
{
richTextBox1.SelectedText = DateTime.Now.ToString();
}
//
//撤消
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -