c#记事本代码
using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace NotePad
{
/// <summary>
/// Summary description for NotePad.
/// </summary>
public class NotePad : Form
{
private TextBox textBox;
private MenuItem menuFile;
private MenuItem menuHelp;
private MenuItem menuFile_Open;
private MenuItem menuFile_SaveAs;
private MenuItem menuFile_Exit;
private MenuItem menuHelp_About;
private MenuItem menuFile_Line1;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private MainMenu mainMenu;
private System.ComponentModel.IContainer components;
private MenuItem menuFile_Save;
private MenuItem menuEdit;
private MenuItem menuEdit_Undo;
private MenuItem menuEdit_Redo;
private MenuItem menuEdit_Line1;
private MenuItem menuEdit_Cut;
private MenuItem menuEdit_Copy;
private MenuItem menuEdit_Paste;
private MenuItem menuEdit_Del;
private MenuItem menuFile_New;
private System.Windows.Forms.StatusBar statusBar;
public string oldFileName = ""; //初始打开文件的路径及名称(为空)。
public NotePad()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(NotePad));
this.textBox = new System.Windows.Forms.TextBox();
this.mainMenu = new System.Windows.Forms.MainMenu();
this.menuFile = new System.Windows.Forms.MenuItem();
this.menuFile_New = new System.Windows.Forms.MenuItem();
this.menuFile_Open = new System.Windows.Forms.MenuItem();
this.menuFile_Save = new System.Windows.Forms.MenuItem();
this.menuFile_SaveAs = new System.Windows.Forms.MenuItem();
this.menuFile_Line1 = new System.Windows.Forms.MenuItem();
this.menuFile_Exit = new System.Windows.Forms.MenuItem();
this.menuEdit = new System.Windows.Forms.MenuItem();
this.menuEdit_Undo = new System.Windows.Forms.MenuItem();
this.menuEdit_Redo = new System.Windows.Forms.MenuItem();
this.menuEdit_Cut = new System.Windows.Forms.MenuItem();
this.menuEdit_Copy = new System.Windows.Forms.MenuItem();
this.menuEdit_Paste = new System.Windows.Forms.MenuItem();
this.menuEdit_Del = new System.Windows.Forms.MenuItem();
this.menuHelp = new System.Windows.Forms.MenuItem();
this.menuHelp_About = new System.Windows.Forms.MenuItem();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.menuEdit_Line1 = new System.Windows.Forms.MenuItem();
this.statusBar = new System.Windows.Forms.StatusBar();
this.SuspendLayout();
//
// textBox
//
this.textBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox.Font = new System.Drawing.Font("NSimSun", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.textBox.MaxLength = 1048576;
this.textBox.Multiline = true;
this.textBox.Name = "textBox";
this.textBox.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox.Size = new System.Drawing.Size(632, 430);
this.textBox.TabIndex = 0;
this.textBox.Text = "";
this.textBox.TextChanged += new System.EventHandler(this.textBox_TextChanged);
//
// mainMenu
//
this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuFile,
this.menuEdit,
this.menuHelp});
//
// menuFile
//
this.menuFile.Index = 0;
this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuFile_New,
this.menuFile_Open,
this.menuFile_Save,
this.menuFile_SaveAs,
this.menuFile_Line1,
this.menuFile_Exit});
this.menuFile.Text = "文件";
//
// menuFile_New
//
this.menuFile_New.Index = 0;
this.menuFile_New.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
this.menuFile_New.Text = "新建";
this.menuFile_New.Click += new System.EventHandler(this.menuFile_New_Click);
//
// menuFile_Open
//
this.menuFile_Open.Index = 1;
this.menuFile_Open.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
this.menuFile_Open.Text = "打开";
this.menuFile_Open.Click += new System.EventHandler(this.menuFile_Open_Click);
//
// menuFile_Save
//
this.menuFile_Save.Index = 2;
this.menuFile_Save.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
this.menuFile_Save.Text = "保存";
this.menuFile_Save.Click += new System.EventHandler(this.menuFile_Save_Click);
//
// menuFile_SaveAs
//
this.menuFile_SaveAs.Index = 3;
this.menuFile_SaveAs.Shortcut = System.Windows.Forms.Shortcut.CtrlShiftS;
this.menuFile_SaveAs.Text = "保存为...";
this.menuFile_SaveAs.Click += new System.EventHandler(this.menuFile_SaveAs_Click);
//
// menuFile_Line1
//
this.menuFile_Line1.Index = 4;
this.menuFile_Line1.Text = "-";
//
// menuFile_Exit
//
this.menuFile_Exit.Index = 5;
this.menuFile_Exit.Shortcut = System.Windows.Forms.Shortcut.CtrlE;
this.menuFile_Exit.Text = "退出";
this.menuFile_Exit.Click += new System.EventHandler(this.menuFile_Exit_Click);
//
// menuEdit
//
this.menuEdit.Index = 1;
this.menuEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuEdit_Undo,
this.menuEdit_Redo,
this.menuEdit_Cut,
this.menuEdit_Copy,
this.menuEdit_Paste,
this.menuEdit_Del});
this.menuEdit.Text = "编辑";
//
// menuEdit_Undo
//
this.menuEdit_Undo.Index = 0;
this.menuEdit_Undo.Shortcut = System.Windows.Forms.Shortcut.CtrlZ;
this.menuEdit_Undo.Text = "撤消";
this.menuEdit_Undo.Click += new System.EventHandler(this.menuEdit_Undo_Click);
//
// menuEdit_Redo
//
this.menuEdit_Redo.Enabled = false;
this.menuEdit_Redo.Index = 1;
this.menuEdit_Redo.Shortcut = System.Windows.Forms.Shortcut.CtrlR;
this.menuEdit_Redo.Text = "重做";
this.menuEdit_Redo.Click += new System.EventHandler(this.menuEdit_Undo_Click);
//
// menuEdit_Cut
//
this.menuEdit_Cut.Index = 2;
this.menuEdit_Cut.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
this.menuEdit_Cut.Text = "剪切";
this.menuEdit_Cut.Click += new System.EventHandler(this.menuEdit_Cut_Click);
//
// menuEdit_Copy
//
this.menuEdit_Copy.Index = 3;
this.menuEdit_Copy.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
this.menuEdit_Copy.Text = "复制";
this.menuEdit_Copy.Click += new System.EventHandler(this.menuEdit_Copy_Click);
//
// menuEdit_Paste
//
this.menuEdit_Paste.Index = 4;
this.menuEdit_Paste.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
this.menuEdit_Paste.Text = "粘贴";
this.menuEdit_Paste.Click += new System.EventHandler(this.menuEdit_Paste_Click);
//
// menuEdit_Del
//
this.menuEdit_Del.Index = 5;
this.menuEdit_Del.Shortcut = System.Windows.Forms.Shortcut.Del;
this.menuEdit_Del.Text = "删除";
this.menuEdit_Del.Click += new System.EventHandler(this.menuEdit_Del_Click);
//
// menuHelp
//
this.menuHelp.Index = 2;
this.menuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuHelp_About});
this.menuHelp.Text = "帮助";
this.menuHelp.Click += new System.EventHandler(this.menuHelp_Click);
//
// menuHelp_About
//
this.menuHelp_About.Index = 0;
this.menuHelp_About.Shortcut = System.Windows.Forms.Shortcut.F1;
this.menuHelp_About.Text = "关于...";
this.menuHelp_About.Click += new System.EventHandler(this.menuHelp_About_Click);
//
// openFileDialog
//
this.openFileDialog.DefaultExt = "*.TXT, *.CS";
this.openFileDialog.Filter = "(*.TXT; *.CS)|*.TXT; *.CS|(All Files)|*.*";
this.openFileDialog.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog_FileOk);
//
// saveFileDialog
//
this.saveFileDialog.FileName = "FileName";
this.saveFileDialog.Filter = "Text File (*.TXT)|*.TXT|C Sharp File (*.CS))|*.CS";
this.saveFileDialog.FileOk += new System.ComponentModel.CancelEventHandler(this.saveFileDialog_FileOk);
//
// menuEdit_Line1
//
this.menuEdit_Line1.Index = -1;
this.menuEdit_Line1.Text = "";
//
// statusBar
//
this.statusBar.Font = new System.Drawing.Font("SimSun", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.statusBar.Location = new System.Drawing.Point(0, 408);
this.statusBar.Name = "statusBar";
this.statusBar.ShowPanels = true;
this.statusBar.Size = new System.Drawing.Size(632, 22);
this.statusBar.TabIndex = 1;
this.statusBar.Text = "状态栏";
this.statusBar.PanelClick += new System.Windows.Forms.StatusBarPanelClickEventHandler(this.statusBar_PanelClick);
//
// NotePad
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(632, 430);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.statusBar,
this.textBox});
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Menu = this.mainMenu;
this.Name = "NotePad";
this.Text = "三毛.Net 之 C#记事本";
this.Load += new System.EventHandler(this.NotePad_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new NotePad());
}
private void NotePad_Load(object sender, EventArgs e)
{
}
private void menuHelp_Click(object sender, EventArgs e)
{
}
private void openFileDialog_FileOk(object sender, CancelEventArgs e)
{
}
private void saveFileDialog_FileOk(object sender, CancelEventArgs e)
{
}
private void menuFile_Open_Click(object sender, EventArgs e) //打开文件
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFileDialog.FileName, Encoding.Default);
oldFileName = openFileDialog.FileName;
textBox.Text = sr.ReadToEnd();
sr.Close();
}
}
private void menuFile_New_Click(object sender, System.EventArgs e)
{
textBox.Text = "这是新建立的文件,程序设计时可以有1M的文件大小,请放心使用吧。";
oldFileName = "";
}
private void menuFile_Save_Click(object sender, EventArgs e) //保存文件
{
if (oldFileName == "")
{
menuFile_SaveAs_Click(sender, e);
}
else
{
StreamWriter sw = new StreamWriter(oldFileName);
sw.Write(textBox.Text);
sw.Close();
}
}
private void menuFile_SaveAs_Click(object sender, EventArgs e) //另存文件
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
StreamWriter asw = new StreamWriter(saveFileDialog.FileName, true);
asw.Write(textBox.Text);
asw.Close();
}
}
private void menuFile_Exit_Click(object sender, EventArgs e) //关闭主程序
{
Close();
}
private void menuHelp_About_Click(object sender, EventArgs e) //打开“关于”窗口
{
(new About()).ShowDialog();
}
private void textBox_TextChanged(object sender, EventArgs e) //内容主体文本框
{
}
private void menuEdit_Undo_Click(object sender, System.EventArgs e)
{
menuEdit_Redo.Enabled=true;
textBox.Undo();
}
private void menuEdit_Cut_Click(object sender, System.EventArgs e)
{
textBox.Cut();
}
private void menuEdit_Copy_Click(object sender, System.EventArgs e)
{
textBox.Copy();
}
private void menuEdit_Paste_Click(object sender, System.EventArgs e)
{
textBox.Paste();
}
private void menuEdit_Del_Click(object sender, System.EventArgs e)
{
textBox.SelectedText = "";
}
private void statusBar_PanelClick(object sender, System.Windows.Forms.StatusBarPanelClickEventArgs e)
{
}
}
}
FeedBack:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using TextBoox;
using System.Drawing.Printing;
namespace TextBook
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
MenuItem401.Checked = true; //"查看"已选中
MenuItem301.Checked = false; //"自动换行"未选中
MenuItem402.Checked = true; //"工具栏"选中
}
//新建文本
private void MenuItem101_Click(object sender, EventArgs e)
{
//如果当前文本内容处于修改状态,则提示保存
if (textBox1.Modified == true)
{
DialogResult re = MessageBox.Show("文件" + Text + "的内容已改变,需要保存吗?", "保存文件", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
switch (re)
{
case DialogResult.Yes:
this.MenuItem103_Click(sender, e);
textBox1.Clear();
this.Text = "新建-记事本";
fileAddress.Text = "null";
break;
case DialogResult.No:
textBox1.Clear();
this.Text = "新建-记事本";
fileAddress.Text = "null";
break;
case DialogResult.Cancel:
break;
}
}
else
{
textBox1.Clear();
this.Text = "新建-记事本";
textBox1.Modified = false;
fileAddress.Text = "null";
}
} //打开文件
private void MenuItem102_Click(object sender, EventArgs e)
{
try//捕获异常事件
{
//如果当前文本内容被修改,则提示保存
if (textBox1.Modified == true)
{
//提示保存对话框
DialogResult re = MessageBox.Show("文件" + this.Text + "的内容已经改变,是否需要保存?", "保存文件", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (re == DialogResult.Cancel)
{
return;
}
if (re == DialogResult.OK)
{
this.MenuItem103_Click(sender, e);
}
}
else
{
textBox1.Clear();
this.Text = "新建-记事本";
textBox1.Modified = false;
fileAddress.Text = "null";
}
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.FileName = "";
openFileDialog1.Filter = "Text Document(*.txt)|*.txt|All Files(*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//打开选定路径上的文件
StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.Default);
fileAddress.Text = openFileDialog1.FileName;
this.Text = Path.GetFileName(openFileDialog1.FileName) + "-记事本";
textBox1.Text = sr.ReadToEnd();
sr.Close();
}
}
//处理异常事件
catch (Exception error)
{
MessageBox.Show(error.Message.ToString());
}
}
//关闭窗体前提示是否需要保存
private void Form_Close(object sender, System.ComponentModel.CancelEventArgs err)
{
if (textBox1.Modified == true)
{
DialogResult re = MessageBox.Show(this.Text + "内容已更改,是否需要保存?", "保存提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk);
switch (re)
{
case DialogResult.Yes:
this.MenuItem103_Click(sender, err);
if (textBox1.Modified == true)
{
err.Cancel = true;
}
else
{
Application.Exit();
}
break;
case DialogResult.No:
Application.Exit();
break;
case DialogResult.Cancel:
err.Cancel = true;
break;
}
}
}
//保存文件
private void MenuItem103_Click(object sender, EventArgs e)
{
//捕获异常事件
try
{
if (this.Text == "新建-记事本")
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "保存";
saveFileDialog1.FileName = "新建.txt";
saveFileDialog1.Filter = "Text Document(*.txt)|*.txt|All Files(*.*)|*.*";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//保存的实现
this.Text = saveFileDialog1.FileName;
using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
{
sw.Write(textBox1.Text);
sw.Close();
textBox1.Modified = false;
this.Text = Path.GetFileName(saveFileDialog1.FileName) + "-记事本";
fileAddress.Text = saveFileDialog1.FileName;
}
}
}
else
{
StreamWriter sw = new StreamWriter(fileAddress.Text);
sw.Write(textBox1.Text);
sw.Close();
textBox1.Modified = false;
}
}
//处理异常事件
catch (Exception er)
{
MessageBox.Show(er.Message.ToString());
}
}
//另存为
private void MenuItem104_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "另存为";
saveFileDialog1.FileName = "";
saveFileDialog1.Filter = "Text Document(*.txt)|*.txt|All Files(*.*)|*.*";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
sw.Write(textBox1.Text);
textBox1.Modified = false;
this.Text = saveFileDialog1.FileName;
sw.Close();
this.Text = Path.GetFileName(saveFileDialog1.FileName) + "-记事本";
fileAddress.Text = saveFileDialog1.FileName;
}
}
catch (Exception er)
{
MessageBox.Show(er.Message.ToString());
}
}
//打印清单
private void pd_PrintPage(object sender, PrintPageEventArgs ev) {
float linesPerPage = 0;//页面的行号
float yPos = 0;//绘制字符串的纵向位置
int count = 0;//行计数器
float leftMargin = ev.MarginBounds.Left ;//左边距
float topMargin = ev.MarginBounds.Top ;//上边距
string line = null;//行字符串
System.Drawing.Font printFont;
System.IO.StreamReader streamToPrint;
printFont = new Font("Arial", 10);
streamToPrint = new StreamReader(fileAddress.Text);
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);//每页可打印的行数
// Print each line of the file.
//逐行的循环打印一页
while (count < linesPerPage &&
((line = streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
} //页面设置
private void MenuItem105_Click(object sender, EventArgs e)
{
if (textBox1.Modified == true)
{
this.MenuItem103_Click(sender, e);
textBox1.Modified = false;
}
PageSetupDialog pageSet = new PageSetupDialog();
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pageSet.Document = pd;
pageSet.ShowDialog();
}
//打印预览
private void MenuItem108_Click(object sender, EventArgs e)
{
if (textBox1.Modified == true)
{
this.MenuItem103_Click(sender, e);
textBox1.Modified = false;
}
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
printPreviewDialog.Document = printDocument;
try
{
printPreviewDialog.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//打印
private void MenuItem106_Click(object sender, EventArgs e)
{
if (textBox1.Modified == true)
{
this.MenuItem103_Click(sender, e);
textBox1.Modified = false;
}
System.Drawing.Font printFont;
System.IO.StreamReader streamToPrint;
try
{
streamToPrint = new StreamReader(fileAddress.Text);
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//退出
private void MenuItem107_Click(object sender, EventArgs e)
{
this.Close();
}
//撤消上一步
private void MenuItem201_Click(object sender, EventArgs e)
{
if (textBox1.CanUndo == true)
{
textBox1.Undo();
//textBox1.ClearUndo();
}
}
//恢复
private void MenuItem212_Click(object sender, EventArgs e)
{
if (textBox1.CanRedo == true)
{
textBox1.Redo();
}
}
//剪切
private void MenuItem202_Click(object sender, EventArgs e)
{
if (textBox1.SelectedText != "")
{
textBox1.Cut();
}
}
//复制
private void MenuItem203_Click(object sender, EventArgs e)
{
if (textBox1.SelectedText != "")
{
textBox1.Copy();
}
}
//粘贴
private void MenuItem204_Click(object sender, EventArgs e)
{
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
{
textBox1.Paste();
}
}
//删除
private void MenuItem205_Click(object sender, EventArgs e)
{
if (textBox1.SelectedText != "")
{
textBox1.SelectedText = "";
}
}
//查找
private void MenuItem206_Click(object sender, EventArgs e)
{
new Find(textBox1).Show();
}
//查找下一个
private void MenuItem207_Click(object sender, EventArgs e)
{
this.MenuItem206_Click(sender, e);
}
private void MenuItem208_Click(object sender, EventArgs e)
{
new Replace(textBox1).Show();
}
//跳转
private void MenuItem209_Click(object sender, EventArgs e)
{
new Goto(textBox1).Show();
}
//全选
private void MenuItem210_Click(object sender, EventArgs e)
{
textBox1.SelectAll();
}
//时间/日期,加到文本最后
private void MenuItem211_Click(object sender, EventArgs e)
{
textBox1.AppendText(DateTime.Now.ToString());
}
//字体设置
private void MenuItem302_Click(object sender, EventArgs e)
{
FontDialog fontDialog1 = new FontDialog();
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Font = fontDialog1.Font;
textBox1.ForeColor = fontDialog1.Color;
}
}
//字体颜色
private void MenuItem303_Click(object sender, EventArgs e)
{
ColorDialog colorDialog1 = new ColorDialog();
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.ForeColor = colorDialog1.Color;
}
} //背景颜色
private void MenuItem304_Click(object sender, EventArgs e)
{
ColorDialog colorDialog1 = new ColorDialog();
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.BackColor = colorDialog1.Color;
}
}
//状态栏
private void MenuItem401_Click(object sender, EventArgs e)
{
if (MenuItem401.Checked == true)
{
statusStrip1.Visible = false;
MenuItem401.Checked = false;
toolStripStatusLabel1.Text = "";
}
else
{
statusStrip1.Visible = true;
MenuItem401.Checked = true;
status(sender, e);
}
}
//选择区域发生变化时(即光标位置变化时,动态更新显示其位置)
private void textBox1_SelectionChanged(object sender, EventArgs e)
{ if (MenuItem401.Enabled == true)
{
if (MenuItem401.Checked == true)
{
status(sender, e);
}
else
{
toolStripStatusLabel1.Text = "";
}
}
}
//显示光标所在行数与列数
private void status(object sender, EventArgs e)
{
int line = this.textBox1.GetLineFromCharIndex(textBox1.SelectionStart);
int col;
int start = 0;
int cursor = textBox1.SelectionStart;
while (start < cursor)
{
if (line == this.textBox1.GetLineFromCharIndex(start))
{
break;
}
else
start++;
}
col = cursor - start;
line++;
col++;
toolStripStatusLabel1.Text = "( line " + line + " col " + col + " )";
}
//显示帮助
private void MenuItem501_Click(object sender, EventArgs e)
{
aboutDialog aboutdlg = new aboutDialog();
aboutdlg.ShowDialog();
}
//显示关于
private void MenuItem502_Click(object sender, EventArgs e)
{
aboutDialog aboutdlg = new aboutDialog();
aboutdlg.ShowDialog();
}
//设置或取消自动换行
private void MenuItem301_CheckedChanged(object sender, EventArgs e)
{
//修改texBox1的相关属性
if (this.MenuItem301.Checked == false)
{
if (MenuItem401.Checked == true)
{
status(sender, e);
}
MenuItem209.Enabled = true;
textBox1.ScrollBars = RichTextBoxScrollBars.ForcedBoth;
textBox1.WordWrap = false;
}
else
{
MenuItem209.Enabled = false;
textBox1.ScrollBars = RichTextBoxScrollBars.ForcedVertical;
textBox1.WordWrap = true;
}
}
//当自动换行改变时,设置状态栏可用与否
private void MenuItem301_Click(object sender, EventArgs e)
{
if (MenuItem301.Checked == true)
{
MenuItem301.Checked = false;
MenuItem401.Enabled = true;
statusStrip1.Visible = true;
}
else
{
MenuItem301.Checked = true;
MenuItem401.Enabled = false;
statusStrip1.Visible = false;
}
}
//tool复制
private void toolStripButton1_Click(object sender, EventArgs e)
{
this.MenuItem101_Click(sender, e);
}
//tool打开
private void toolStripButton2_Click(object sender, EventArgs e)
{
this.MenuItem102_Click(sender, e);
}
//tool保存
private void toolStripButton3_Click(object sender, EventArgs e)
{
this.MenuItem103_Click(sender, e);
}
//tool打印
private void toolStripButton4_Click(object sender, EventArgs e)
{
this.MenuItem106_Click(sender, e);
}
//tool打印预览
private void toolStripButton5_Click(object sender, EventArgs e)
{
this.MenuItem108_Click(sender, e);
}
//tool查找
private void toolStripButton6_Click(object sender, EventArgs e)
{
this.MenuItem206_Click(sender, e);
}
//tool剪切
private void toolStripButton7_Click(object sender, EventArgs e)
{
this.MenuItem202_Click(sender, e);
}
}
}
posted on 2009-04-25 21:44 xufeng001 阅读(4393) 评论(0) 编辑 收藏 举报