记事本C#.docx
《记事本C#.docx》由会员分享,可在线阅读,更多相关《记事本C#.docx(9页珍藏版)》请在冰豆网上搜索。
![记事本C#.docx](https://file1.bdocx.com/fileroot1/2022-12/13/ac295bf9-c0a9-4f1f-b993-132095e01bd0/ac295bf9-c0a9-4f1f-b993-132095e01bd01.gif)
记事本C#
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
usingSystem.IO;
namespaceNotePad
{
publicclassForm1:
System.Windows.Forms.Form
{
privateSystem.Windows.Forms.MainMenumainMenu1;
privateSystem.Windows.Forms.MenuItemmenuItem1;
privateSystem.Windows.Forms.MenuItemmenuItem3;
privateSystem.Windows.Forms.MenuItemmenuItem4;
privateSystem.Windows.Forms.MenuItemmenuItem5;
privateSystem.Windows.Forms.MenuItemmenuItem6;
privateSystem.Windows.Forms.MenuItemmenuItem7;
privateSystem.Windows.Forms.MenuItemmenuItem8;
privateSystem.Windows.Forms.MenuItemmenuItem9;
privateSystem.Windows.Forms.MenuItemmenuItem10;
privateSystem.Windows.Forms.MenuItemmenuItem14;
privateSystem.Windows.Forms.MenuItemmenuItem15;
privateSystem.Windows.Forms.MenuItemmenuItem16;
privateSystem.Windows.Forms.RichTextBoxrichTextBox1;
privateSystem.Windows.Forms.FontDialogfontDialog1;
privateSystem.Windows.Forms.SaveFileDialogsaveFileDialog1;
privateSystem.Windows.Forms.OpenFileDialogopenFileDialog1;
privateSystem.ComponentModel.Containercomponents=null;
publicForm1()
{
InitializeComponent();
}
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!
=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#regionWindowsFormDesignergeneratedcode
privatevoidInitializeComponent()
{
this.mainMenu1=newSystem.Windows.Forms.MainMenu();
this.menuItem1=newSystem.Windows.Forms.MenuItem();
this.menuItem6=newSystem.Windows.Forms.MenuItem();
this.menuItem7=newSystem.Windows.Forms.MenuItem();
this.menuItem8=newSystem.Windows.Forms.MenuItem();
this.menuItem9=newSystem.Windows.Forms.MenuItem();
this.menuItem10=newSystem.Windows.Forms.MenuItem();
this.menuItem14=newSystem.Windows.Forms.MenuItem();
this.menuItem3=newSystem.Windows.Forms.MenuItem();
this.menuItem15=newSystem.Windows.Forms.MenuItem();
this.menuItem16=newSystem.Windows.Forms.MenuItem();
this.menuItem4=newSystem.Windows.Forms.MenuItem();
this.menuItem5=newSystem.Windows.Forms.MenuItem();
this.richTextBox1=newSystem.Windows.Forms.RichTextBox();
this.fontDialog1=newSystem.Windows.Forms.FontDialog();
this.saveFileDialog1=newSystem.Windows.Forms.SaveFileDialog();
this.openFileDialog1=newSystem.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
this.mainMenu1.MenuItems.AddRange(newSystem.Windows.Forms.MenuItem[]{
this.menuItem1,
this.menuItem3,
this.menuItem4});
this.menuItem1.Index=0;
this.menuItem1.MenuItems.AddRange(newSystem.Windows.Forms.MenuItem[]{
this.menuItem6,
this.menuItem7,
this.menuItem8,
this.menuItem9,
this.menuItem10,
this.menuItem14});
this.menuItem1.Text="文件(&F)";
this.menuItem6.Index=0;
this.menuItem6.Shortcut=System.Windows.Forms.Shortcut.CtrlN;
this.menuItem6.Text="新建(&N)";
this.menuItem6.Click+=newSystem.EventHandler(this.menuItem6_Click);
this.menuItem7.Index=1;
this.menuItem7.Shortcut=System.Windows.Forms.Shortcut.CtrlO;
this.menuItem7.Text="打开(&O)...";
this.menuItem7.Click+=newSystem.EventHandler(this.menuItem7_Click);
this.menuItem8.Index=2;
this.menuItem8.Shortcut=System.Windows.Forms.Shortcut.CtrlS;
this.menuItem8.Text="保存(&S)";
this.menuItem8.Click+=newSystem.EventHandler(this.menuItem8_Click);
this.menuItem9.Index=3;
this.menuItem9.Text="另存为(&A)...";
this.menuItem9.Click+=newSystem.EventHandler(this.menuItem9_Click);
this.menuItem10.Index=4;
this.menuItem10.Text="-";
this.menuItem14.Index=5;
this.menuItem14.Text="退出(&X)";
this.menuItem14.Click+=newSystem.EventHandler(this.menuItem14_Click);
this.menuItem3.Index=1;
this.menuItem3.MenuItems.AddRange(newSystem.Windows.Forms.MenuItem[]{
this.menuItem15,
this.menuItem16});
this.menuItem3.Text="格式(&O)";
this.menuItem15.Index=0;
this.menuItem15.Text="自动换行(&W)";
this.menuItem15.Click+=newSystem.EventHandler(this.menuItem15_Click);
this.menuItem16.Index=1;
this.menuItem16.Text="字体(&F)...";
this.menuItem16.Click+=newSystem.EventHandler(this.menuItem16_Click);
this.menuItem4.Index=2;
this.menuItem4.MenuItems.AddRange(newSystem.Windows.Forms.MenuItem[]{
this.menuItem5});
this.menuItem4.Text="帮助(&H)";
this.menuItem5.Index=0;
this.menuItem5.Text="关于记事本(&A)";
this.menuItem5.Click+=newSystem.EventHandler(this.menuItem5_Click);
this.richTextBox1.Dock=System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.Name="richTextBox1";
this.richTextBox1.ScrollBars=System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth;
this.richTextBox1.Size=newSystem.Drawing.Size(480,311);
this.richTextBox1.TabIndex=0;
this.richTextBox1.Text="";
this.richTextBox1.WordWrap=false;
this.richTextBox1.KeyPress+=newSystem.Windows.Forms.KeyPressEventHandler(this.richTextBox1_KeyPress);
this.saveFileDialog1.FileName="doc1";
this.AutoScaleBaseSize=newSystem.Drawing.Size(8,18);
this.ClientSize=newSystem.Drawing.Size(480,311);
this.Controls.AddRange(newSystem.Windows.Forms.Control[]{
this.richTextBox1});
this.Menu=this.mainMenu1;
this.Name="Form1";
this.StartPosition=System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text="记事本";
this.Closing+=newSystem.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.ResumeLayout(false);
}
#endregion
[STAThread]
staticvoidMain()
{
Application.Run(newForm1());
}
privatebooldirty=false;
privatestringfilepath="";
privatevoidMySaving()
{
if(dirty)
{
if(MessageBox.Show("需要保存文件吗?
","提示框",MessageBoxButtons.YesNo)==DialogResult.Yes)
{
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{
dirty=false;
if(filepath.Length==0)
{
filepath=saveFileDialog1.FileName;
}
StreamWritersTmp=newStreamWriter(filepath);
sTmp.Write(richTextBox1.Text);
sTmp.Flush();
sTmp.Close();
}
}
}
}
privatevoidmenuItem14_Click(objectsender,System.EventArgse)
{
MySaving();
if(dirty==false)
Close();
}
privatevoidForm1_Closing(objectsender,System.ComponentModel.CancelEventArgse)
{
MySaving();
if(dirty==false)
e.Cancel=false;
}
privatevoidmenuItem15_Click(objectsender,System.EventArgse)
{
if(richTextBox1.WordWrap==true)
{
richTextBox1.WordWrap=false;
mainMenu1.MenuItems[2].MenuItems[0].Checked=false;
}
else
{
richTextBox1.WordWrap=true;
mainMenu1.MenuItems[2].MenuItems[0].Checked=true;
}
}
privatevoidmenuItem16_Click(objectsender,System.EventArgse)
{
if(fontDialog1.ShowDialog()==DialogResult.OK)
{
richTextBox1.Font=fontDialog1.Font;
}
}
privatevoidmenuItem5_Click(objectsender,System.EventArgse)
{
MessageBox.Show("版权所有盗用必究,信管0801专用","关于记事本");
}
privatevoidrichTextBox1_KeyPress(objectsender,System.Windows.Forms.KeyPressEventArgse)
{
dirty=true;
}
privatevoidmenuItem6_Click(objectsender,System.EventArgse)
{
MySaving();
dirty=false;
richTextBox1.Text="";
}
privatevoidmenuItem7_Click(objectsender,System.EventArgse)
{
MySaving();
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
dirty=false;
StreamReadersTmp=newStreamReader(openFileDialog1.FileName,System.Text.Encoding.ASCII);
filepath=openFileDialog1.FileName;
richTextBox1.Text="";
richTextBox1.Text=sTmp.ReadToEnd();
}
}
privatevoidmenuItem8_Click(objectsender,System.EventArgse)
{
MySaving();
}
privatevoidmenuItem9_Click(objectsender,System.EventArgse)
{
MySaving();
}
}
}