不知C#是不是这么用的,我写的有没有体现C#的OOP思想. 程序没有写完,也很不完善,只是请各位指点一下.谢谢!! ____________________________________________________ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO;
namespace notePad { /// <summary> /// Form1 的摘要说明。 /// </summary> public class notePad : System.Windows.Forms.Form { private System.Windows.Forms.RichTextBox richTextBox1; private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem8; private System.Windows.Forms.MenuItem menuItem10; private System.Windows.Forms.MenuItem menuItemFile; private System.Windows.Forms.MenuItem menuItemEdit; private System.Windows.Forms.MenuItem menuItemOption; private System.Windows.Forms.MenuItem menuItemHelp; private System.Windows.Forms.MenuItem menuItemNew; private System.Windows.Forms.MenuItem menuItemOpen; private System.Windows.Forms.MenuItem menuItemPrint; private System.Windows.Forms.MenuItem menuItemExit; private System.Windows.Forms.MenuItem menuItemFont; private System.Windows.Forms.MenuItem menuItemWordWrap; private System.Windows.Forms.MenuItem menuItemAbout; private System.Windows.Forms.ToolBar toolBarOnly; private System.Windows.Forms.ToolBarButton toolBarButtonNew; private System.Windows.Forms.ToolBarButton toolBarButtonOpen; private System.Windows.Forms.StatusBar statusBarOnly; private System.Windows.Forms.StatusBarPanel statusBarPanelTime; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.ImageList imageList1; private System.Windows.Forms.FontDialog fontDialog1; private System.Windows.Forms.Timer timer1; private System.ComponentModel.IContainer components; private System.Windows.Forms.SaveFileDialog saveFileDialog1; private System.Windows.Forms.MenuItem menuItemSave; private System.Windows.Forms.MenuItem menuItemSaveAs; private System.Windows.Forms.StatusBarPanel statusBarPanelFileName; private string oemText = "";
public notePad() { // // Windows 窗体设计器支持所必需的 // InitializeComponent();
// // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // }
/// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows Form Designer generated code /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(notePad)); this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.menuItemFile = new System.Windows.Forms.MenuItem(); this.menuItemNew = new System.Windows.Forms.MenuItem(); this.menuItemOpen = new System.Windows.Forms.MenuItem(); this.menuItemSave = new System.Windows.Forms.MenuItem(); this.menuItemSaveAs = new System.Windows.Forms.MenuItem(); this.menuItem8 = new System.Windows.Forms.MenuItem(); this.menuItemPrint = new System.Windows.Forms.MenuItem(); this.menuItem10 = new System.Windows.Forms.MenuItem(); this.menuItemExit = new System.Windows.Forms.MenuItem(); this.menuItemEdit = new System.Windows.Forms.MenuItem(); this.menuItemOption = new System.Windows.Forms.MenuItem(); this.menuItemFont = new System.Windows.Forms.MenuItem(); this.menuItemWordWrap = new System.Windows.Forms.MenuItem(); this.menuItemHelp = new System.Windows.Forms.MenuItem(); this.menuItemAbout = new System.Windows.Forms.MenuItem(); this.toolBarOnly = new System.Windows.Forms.ToolBar(); this.toolBarButtonNew = new System.Windows.Forms.ToolBarButton(); this.toolBarButtonOpen = new System.Windows.Forms.ToolBarButton(); this.imageList1 = new System.Windows.Forms.ImageList(this.components); this.statusBarOnly = new System.Windows.Forms.StatusBar(); this.statusBarPanelTime = new System.Windows.Forms.StatusBarPanel(); this.statusBarPanelFileName = new System.Windows.Forms.StatusBarPanel(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.fontDialog1 = new System.Windows.Forms.FontDialog(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog(); ((System.ComponentModel.ISupportInitialize)(this.statusBarPanelTime)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.statusBarPanelFileName)).BeginInit(); this.SuspendLayout(); // // richTextBox1 // this.richTextBox1.AutoWordSelection = true; this.richTextBox1.Location = new System.Drawing.Point(1, 30); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth; this.richTextBox1.Size = new System.Drawing.Size(556, 298); this.richTextBox1.TabIndex = 0; this.richTextBox1.Text = ""; this.richTextBox1.WordWrap = false; // // mainMenu1 // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItemFile, this.menuItemEdit, this.menuItemOption, this.menuItemHelp}); // // menuItemFile // this.menuItemFile.Index = 0; this.menuItemFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItemNew, this.menuItemOpen, this.menuItemSave, this.menuItemSaveAs, this.menuItem8, this.menuItemPrint, this.menuItem10, this.menuItemExit}); this.menuItemFile.Text = "&File"; // // menuItemNew // this.menuItemNew.Index = 0; this.menuItemNew.Text = "&New"; // // menuItemOpen // this.menuItemOpen.Index = 1; this.menuItemOpen.Text = "&Open"; this.menuItemOpen.Click += new System.EventHandler(this.menuItemOpen_Click); // // menuItemSave // this.menuItemSave.Index = 2; this.menuItemSave.Text = "&Save"; // // menuItemSaveAs // this.menuItemSaveAs.Index = 3; this.menuItemSaveAs.Text = "Save &As..."; this.menuItemSaveAs.Click += new System.EventHandler(this.menuItemSaveAs_Click); // // menuItem8 // this.menuItem8.Index = 4; this.menuItem8.Text = "-"; // // menuItemPrint // this.menuItemPrint.Index = 5; this.menuItemPrint.Text = "&Print"; // // menuItem10 // this.menuItem10.Index = 6; this.menuItem10.Text = "-"; // // menuItemExit // this.menuItemExit.Index = 7; this.menuItemExit.Text = "E&xit"; this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click); // // menuItemEdit // this.menuItemEdit.Index = 1; this.menuItemEdit.Text = "&Edit"; // // menuItemOption // this.menuItemOption.Index = 2; this.menuItemOption.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItemFont, this.menuItemWordWrap}); this.menuItemOption.Text = "&Option"; // // menuItemFont // this.menuItemFont.Index = 0; this.menuItemFont.Text = "&Font"; this.menuItemFont.Click += new System.EventHandler(this.menuItemFont_Click); // // menuItemWordWrap // this.menuItemWordWrap.Index = 1; this.menuItemWordWrap.Text = "&WordWrap"; this.menuItemWordWrap.Click += new System.EventHandler(this.menuItemWordWrap_Click); // // menuItemHelp // this.menuItemHelp.Index = 3; this.menuItemHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItemAbout}); this.menuItemHelp.Text = "&Help"; // // menuItemAbout // this.menuItemAbout.Index = 0; this.menuItemAbout.Text = "&About"; // // toolBarOnly // this.toolBarOnly.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] { this.toolBarButtonNew, this.toolBarButtonOpen}); this.toolBarOnly.ButtonSize = new System.Drawing.Size(24, 24); this.toolBarOnly.DropDownArrows = true; this.toolBarOnly.ImageList = this.imageList1; this.toolBarOnly.Name = "toolBarOnly"; this.toolBarOnly.ShowToolTips = true; this.toolBarOnly.Size = new System.Drawing.Size(560, 27); this.toolBarOnly.TabIndex = 1; this.toolBarOnly.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBarOnly_ButtonClick); this.toolBarOnly.MouseEnter += new System.EventHandler(this.toolBarOnly_MouseEnter); // // toolBarButtonNew // this.toolBarButtonNew.ImageIndex = 0; this.toolBarButtonNew.Text = "New"; this.toolBarButtonNew.ToolTipText = "Creat a New text"; // // toolBarButtonOpen // this.toolBarButtonOpen.ImageIndex = 2; this.toolBarButtonOpen.Text = "Open"; this.toolBarButtonOpen.ToolTipText = "Open a text"; // // imageList1 // this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit; this.imageList1.ImageSize = new System.Drawing.Size(16, 16); this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream"))); this.imageList1.TransparentColor = System.Drawing.Color.Transparent; // // statusBarOnly // this.statusBarOnly.AllowDrop = true; this.statusBarOnly.Location = new System.Drawing.Point(0, 324); this.statusBarOnly.Name = "statusBarOnly"; this.statusBarOnly.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] { this.statusBarPanelTime, this.statusBarPanelFileName}); this.statusBarOnly.ShowPanels = true; this.statusBarOnly.Size = new System.Drawing.Size(560, 13); this.statusBarOnly.TabIndex = 2; // // statusBarPanelTime // this.statusBarPanelTime.BorderStyle = System.Windows.Forms.StatusBarPanelBorderStyle.None; this.statusBarPanelTime.Width = 50; // // statusBarPanelFileName // this.statusBarPanelFileName.Text = "Need a name"; this.statusBarPanelFileName.Width = 200; // // timer1 // this.timer1.Enabled = true; this.timer1.Interval = 1000; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // saveFileDialog1 // this.saveFileDialog1.FileName = "doc1"; // // notePad // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(560, 337); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.statusBarOnly, this.toolBarOnly, this.richTextBox1}); this.Menu = this.mainMenu1; this.Name = "notePad"; this.Text = "My NotePad"; this.Closing += new System.ComponentModel.CancelEventHandler(this.notePad_Closing); this.SizeChanged += new System.EventHandler(this.notePad_SizeChanged); ((System.ComponentModel.ISupportInitialize)(this.statusBarPanelTime)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.statusBarPanelFileName)).EndInit(); this.ResumeLayout(false);
} #endregion
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new notePad()); }
private void notePad_SizeChanged(object sender, System.EventArgs e) { richTextBox1.Width = this.Width - 12; richTextBox1.Height = this.Height - 100; }
private void menuItemWordWrap_Click(object sender, System.EventArgs e) { if(menuItemWordWrap.Checked == true) { richTextBox1.WordWrap = false; menuItemWordWrap.Checked = false; } else { richTextBox1.WordWrap = true; menuItemWordWrap.Checked = true; } }
private void menuItemOpen_Click(object sender, System.EventArgs e) { openFileDialog1.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*"; openFileDialog1.ShowDialog(); if(openFileDialog1.FileName.Length > 0) { StreamReader fileReader = new StreamReader(openFileDialog1.FileName,System.Text.Encoding.GetEncoding("gb2312")); richTextBox1.Text = fileReader.ReadToEnd(); oemText = fileReader.ReadToEnd(); statusBarPanelFileName.Text = openFileDialog1.FileName; fileReader.Close(); } }
private void toolBarOnly_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { switch(toolBarOnly.Buttons.IndexOf(e.Button)) { case 0: // break; case 1: this.menuItemOpen_Click(this,null); break; } } private void menuItemFont_Click(object sender, System.EventArgs e) { fontDialog1.ShowDialog(); richTextBox1.Font = fontDialog1.Font; } private void timer1_Tick(object sender, System.EventArgs e) { statusBarPanelTime.Text = DateTime.Now.ToString("hh:mm:ss"); } private void toolBarOnly_MouseEnter(object sender, System.EventArgs e) { // toolBarOnly.Buttons.IndexOf. }
private void menuItemExit_Click(object sender, System.EventArgs e) { this.Close(); }
private void menuItemSaveAs_Click(object sender, System.EventArgs e) { saveFileDialog1.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*"; saveFileDialog1.ShowDialog(); if(saveFileDialog1.FileName.Length > 0) { richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText); statusBarPanelFileName.Text = saveFileDialog1.FileName; } }
private void notePad_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if(oemText != richTextBox1.Text) { DialogResult msgReturn; msgReturn = MessageBox.Show("The text already changed , Are you saved change .","Exit",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question); switch (msgReturn) { case DialogResult.Yes: menuItemSaveAs_Click(this,null); break; case DialogResult.No: //do nothing; return; } } } } }

|