0% found this document useful (0 votes)
586 views

Note Pad

This document contains code for a C# Notepad application. It includes code for the main form (frmNotepad) which contains menus, menu items and controls for file operations like new, open, save. It also contains code for editing operations like cut, copy, paste. The form initializes components, handles menu item clicks and contains a text box for document content.

Uploaded by

Nguyễn Minh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
586 views

Note Pad

This document contains code for a C# Notepad application. It includes code for the main form (frmNotepad) which contains menus, menu items and controls for file operations like new, open, save. It also contains code for editing operations like cut, copy, paste. The form initializes components, handles menu item clicks and contains a text box for document content.

Uploaded by

Nguyễn Minh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\NotePad.

cs 1
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace MyNotepad
{
/// <summary>
/// Summary description for frmNotepad.
/// </summary>
public class frmNotepad : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenuNotepad;
private System.Windows.Forms.MenuItem menuItemFile;
private System.Windows.Forms.MenuItem menuItemNew;
private System.Windows.Forms.MenuItem menuItemOpen;
private System.Windows.Forms.MenuItem menuItemSeparator1;
private System.Windows.Forms.MenuItem menuItemSave;
private System.Windows.Forms.MenuItem menuItemSaveAs;
private System.Windows.Forms.MenuItem menuItemEdit;
private System.Windows.Forms.MenuItem menuItemCut;
private System.Windows.Forms.MenuItem menuItemCopy;
private System.Windows.Forms.MenuItem menuItemPast;
private System.Windows.Forms.MenuItem menuItemSelectAll;
private System.Windows.Forms.MenuItem menuItemFormat;
private System.Windows.Forms.MenuItem menuItemFont;
private System.Windows.Forms.TextBox txtBody;
private System.Windows.Forms.OpenFileDialog dlgOpenFile;
private System.Windows.Forms.SaveFileDialog dlgSaveFile;
private System.Windows.Forms.FontDialog dlgFont;

private System.Windows.Forms.ContextMenu contextMenuTxtBody;


private System.Windows.Forms.MenuItem ContextCut;
private System.Windows.Forms.MenuItem ContextCopy;
private System.Windows.Forms.MenuItem ContextPaste;
private System.Windows.Forms.MenuItem ContextSeparator1;
private System.Windows.Forms.MenuItem ContextSelectAll;
private System.Windows.Forms.MenuItem ContextSeparator2;
private System.Windows.Forms.MenuItem ContextFont;

// biến này được sử dụng để đánh số trên tên của document khi bạn new 1 document
// ví dụ: đầu tiên new 1 document thì sẽ được tự động đánh tên là kattyfleaDoc.txt
// nếu new 1 document nữa thì sẽ được đánh tên là kattyfleaDoc.txt
private int m_intDocNumber = 0;

// lưu trữ tên file được tạo hoặc được chọn cho việc "Save As..."
private string m_strFileName = "";

//lưu kết quả trả về từ dialog


private DialogResult dlgResult;

//stream dữ liệu để lưu vào file


private StreamWriter m_sw;

//kiểm tra xem dữ liệu có được modified hay không


private bool m_bModified = false;

private MenuItem menuItem1;


private MenuItem menuItem2;
private MenuItem menuItem3;
private MenuItem menuItem4;
C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\NotePad.cs 2
private MenuItem menuItem5;
private MenuItem menuItem7;
private MenuItem menuItem6;
private IContainer components;

public frmNotepad()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
CheckChanged(null,null);
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()
{
this.components = new System.ComponentModel.Container();
this.mainMenuNotepad = new System.Windows.Forms.MainMenu(this.components);
this.menuItemFile = new System.Windows.Forms.MenuItem();
this.menuItemNew = new System.Windows.Forms.MenuItem();
this.menuItemOpen = new System.Windows.Forms.MenuItem();
this.menuItemSeparator1 = new System.Windows.Forms.MenuItem();
this.menuItemSave = new System.Windows.Forms.MenuItem();
this.menuItemSaveAs = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItemEdit = new System.Windows.Forms.MenuItem();
this.menuItemCut = new System.Windows.Forms.MenuItem();
this.menuItemCopy = new System.Windows.Forms.MenuItem();
this.menuItemPast = new System.Windows.Forms.MenuItem();
this.menuItemSelectAll = new System.Windows.Forms.MenuItem();
this.menuItemFormat = new System.Windows.Forms.MenuItem();
this.menuItemFont = new System.Windows.Forms.MenuItem();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.txtBody = new System.Windows.Forms.TextBox();
this.contextMenuTxtBody = new System.Windows.Forms.ContextMenu();
this.ContextFont = new System.Windows.Forms.MenuItem();
this.ContextSeparator2 = new System.Windows.Forms.MenuItem();
this.ContextCut = new System.Windows.Forms.MenuItem();
this.ContextCopy = new System.Windows.Forms.MenuItem();
this.ContextPaste = new System.Windows.Forms.MenuItem();
this.ContextSeparator1 = new System.Windows.Forms.MenuItem();
this.ContextSelectAll = new System.Windows.Forms.MenuItem();
this.dlgOpenFile = new System.Windows.Forms.OpenFileDialog();
this.dlgSaveFile = new System.Windows.Forms.SaveFileDialog();
C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\NotePad.cs 3
this.dlgFont = new System.Windows.Forms.FontDialog();
this.SuspendLayout();
//
// mainMenuNotepad
//
this.mainMenuNotepad.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemFile,
this.menuItemEdit,
this.menuItemFormat,
this.menuItem1,
this.menuItem3});
//
// menuItemFile
//
this.menuItemFile.Index = 0;
this.menuItemFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemNew,
this.menuItemOpen,
this.menuItemSeparator1,
this.menuItemSave,
this.menuItemSaveAs,
this.menuItem7,
this.menuItem6});
this.menuItemFile.Text = "File";
//
// menuItemNew
//
this.menuItemNew.Index = 0;
this.menuItemNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
this.menuItemNew.Text = "&New";
this.menuItemNew.Click += new System.EventHandler(this.menuItemNew_Click);
//
// menuItemOpen
//
this.menuItemOpen.Index = 1;
this.menuItemOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
this.menuItemOpen.Text = "&Open...";
this.menuItemOpen.Click += new System.EventHandler(this.menuItemOpen_Click);
//
// menuItemSeparator1
//
this.menuItemSeparator1.Index = 2;
this.menuItemSeparator1.Text = "-";
//
// menuItemSave
//
this.menuItemSave.Enabled = false;
this.menuItemSave.Index = 3;
this.menuItemSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
this.menuItemSave.Text = "&Save";
this.menuItemSave.Click += new System.EventHandler(this.menuItemSave_Click);
//
// menuItemSaveAs
//
this.menuItemSaveAs.Index = 4;
this.menuItemSaveAs.Text = "Save As...";
this.menuItemSaveAs.Click += new System.EventHandler(this.
menuItemSaveAs_Click);
//
// menuItem7
//
this.menuItem7.Index = 5;
this.menuItem7.Text = "-";
//
// menuItem6
//
this.menuItem6.Index = 6;
C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\NotePad.cs 4
this.menuItem6.Text = "Exit";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItemEdit
//
this.menuItemEdit.Index = 1;
this.menuItemEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemCut,
this.menuItemCopy,
this.menuItemPast,
this.menuItemSelectAll});
this.menuItemEdit.Text = "Edit";
//
// menuItemCut
//
this.menuItemCut.Index = 0;
this.menuItemCut.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
this.menuItemCut.Text = "&Cut";
this.menuItemCut.Click += new System.EventHandler(this.menuItemCut_Click);
//
// menuItemCopy
//
this.menuItemCopy.Index = 1;
this.menuItemCopy.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
this.menuItemCopy.Text = "&Copy";
this.menuItemCopy.Click += new System.EventHandler(this.menuItemCopy_Click);
//
// menuItemPast
//
this.menuItemPast.Index = 2;
this.menuItemPast.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
this.menuItemPast.Text = "&Paste";
this.menuItemPast.Click += new System.EventHandler(this.menuItemPast_Click);
//
// menuItemSelectAll
//
this.menuItemSelectAll.Index = 3;
this.menuItemSelectAll.Shortcut = System.Windows.Forms.Shortcut.CtrlA;
this.menuItemSelectAll.Text = "&Select All";
this.menuItemSelectAll.Click += new System.EventHandler(this.
menuItemSelectAll_Click);
//
// menuItemFormat
//
this.menuItemFormat.Index = 2;
this.menuItemFormat.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemFont});
this.menuItemFormat.Text = "Format";
//
// menuItemFont
//
this.menuItemFont.Index = 0;
this.menuItemFont.Text = "Font...";
this.menuItemFont.Click += new System.EventHandler(this.menuItemFont_Click);
//
// menuItem1
//
this.menuItem1.Index = 3;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2});
this.menuItem1.Text = "View";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "Status Bar";
//
C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\NotePad.cs 5
// menuItem3
//
this.menuItem3.Index = 4;
this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem4,
this.menuItem5});
this.menuItem3.Text = "Help";
//
// menuItem4
//
this.menuItem4.Index = 0;
this.menuItem4.Text = "Help Topics";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem5
//
this.menuItem5.Index = 1;
this.menuItem5.Text = "About Notepad";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// txtBody
//
this.txtBody.ContextMenu = this.contextMenuTxtBody;
this.txtBody.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtBody.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.txtBody.Location = new System.Drawing.Point(0, 0);
this.txtBody.Multiline = true;
this.txtBody.Name = "txtBody";
this.txtBody.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtBody.Size = new System.Drawing.Size(480, 381);
this.txtBody.TabIndex = 0;
this.txtBody.TextChanged += new System.EventHandler(this.txtBody_TextChanged);
//
// contextMenuTxtBody
//
this.contextMenuTxtBody.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]
{
this.ContextFont,
this.ContextSeparator2,
this.ContextCut,
this.ContextCopy,
this.ContextPaste,
this.ContextSeparator1,
this.ContextSelectAll});
//
// ContextFont
//
this.ContextFont.Index = 0;
this.ContextFont.Text = "Font..";
this.ContextFont.Click += new System.EventHandler(this.menuItemFont_Click);
//
// ContextSeparator2
//
this.ContextSeparator2.Index = 1;
this.ContextSeparator2.Text = "-";
//
// ContextCut
//
this.ContextCut.Index = 2;
this.ContextCut.Text = "Cut";
this.ContextCut.Click += new System.EventHandler(this.menuItemCut_Click);
//
// ContextCopy
//
this.ContextCopy.Index = 3;
this.ContextCopy.Text = "Copy";
C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\NotePad.cs 6
this.ContextCopy.Click += new System.EventHandler(this.menuItemCopy_Click);
//
// ContextPaste
//
this.ContextPaste.Index = 4;
this.ContextPaste.Text = "Paste";
this.ContextPaste.Click += new System.EventHandler(this.menuItemPast_Click);
//
// ContextSeparator1
//
this.ContextSeparator1.Index = 5;
this.ContextSeparator1.Text = "-";
//
// ContextSelectAll
//
this.ContextSelectAll.Index = 6;
this.ContextSelectAll.Text = "Select All";
this.ContextSelectAll.Click += new System.EventHandler(this.
menuItemSelectAll_Click);
//
// dlgSaveFile
//
this.dlgSaveFile.FileName = "doc1";
//
// dlgFont
//
this.dlgFont.FontMustExist = true;
this.dlgFont.ScriptsOnly = true;
//
// frmNotepad
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(480, 381);
this.Controls.Add(this.txtBody);
this.Menu = this.mainMenuNotepad;
this.Name = "frmNotepad";
this.Text = "NotePad với C# => Customized by kattyflea.co.cc";
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmNotepad());
}

// sự kiện text thay đổi trong textbox


// bật cờ xác định nội dung của text đã thay đổi lên true
private void txtBody_TextChanged(object sender, System.EventArgs e)
{
m_bModified = true;
}

// nếu dữ liệu đã thay đổi thì khi click vào nút close form thì sẽ hiển thị
// dialog confirm xem user mún lưu hay hem.
private void CheckChanged(object sender, System.EventArgs e)
{
if(m_bModified)
C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\NotePad.cs 7
{
dlgResult = MessageBox.Show("Muốn lưu file ?","kattyflea.co.cc",
MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation);
if(dlgResult == DialogResult.Yes)
{
menuItemSave_Click(sender,e);
}
}
}

#region file menu


// new 1 document mới =>chúng ta chỉ việc xóa toàn bộ dữ liệu trong textbox
// nhưng trước khi xóa, phải confirm xem dữ liệu trên textbox có thay đổi hay
không
// bằng cách sử dụng CheckChanged
private void menuItemNew_Click(object sender, System.EventArgs e)
{
CheckChanged(sender,e);
m_strFileName = "";
txtBody.Clear();
m_bModified = false;
}

// save as document
private void menuItemSaveAs_Click(object sender, System.EventArgs e)
{

dlgSaveFile.FileName = "kattyfleaDoc" + m_intDocNumber.ToString() + ".txt";


dlgResult = dlgSaveFile.ShowDialog();

if(dlgResult == DialogResult.Cancel)
return;

try
{
m_strFileName = dlgSaveFile.FileName;
m_sw = new StreamWriter(m_strFileName);
m_sw.Write (txtBody.Text);
m_sw.Close();
menuItemSave.Enabled = true;
m_intDocNumber++;
m_bModified = false;
this.Text = "C# Simple Notepad với kattyflea.co.cc: " + m_strFileName;
}
catch(Exception err)
{
MessageBox.Show(err.Message,"lỗi",MessageBoxButtons.OK,MessageBoxIcon.
Error);
}
}

// save file
private void menuItemSave_Click(object sender, System.EventArgs e)
{
if(m_strFileName == "")
{
menuItemSaveAs_Click(sender,e);
return;
}
// đưa dữ liệu từ textbox vào trong file bằng cách
// sử dụng streamWriter
m_sw = new StreamWriter(m_strFileName);
m_sw.Write (txtBody.Text);
m_sw.Close();
m_bModified = false;
}
C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\NotePad.cs 8

// open 1 file mới


private void menuItemOpen_Click(object sender, System.EventArgs e)
{
// kiểm tra file hiện tại có thay đổi hay không ?
CheckChanged(sender,e);

// hộp thoại mở file


dlgResult = dlgOpenFile.ShowDialog();

if(dlgResult == DialogResult.Cancel)
return;

try
{
// mở file và note toàn bộ dữ liệu của file vào trong textbox
// ở đây flea sử dụng StreamReader
m_strFileName = dlgOpenFile.FileName;
StreamReader sr = new StreamReader(m_strFileName);
txtBody.Text = sr.ReadToEnd();
sr.Close();
m_bModified = false;
}
catch(Exception err)
{
MessageBox.Show(err.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.
Error);
}
}

#endregion

#region edit menu


// kiểm tra việc cut dữ liệu
private void menuItemCut_Click(object sender, System.EventArgs e)
{
//đưa dữ liệu được chọn lựa vào trong clipboard
Clipboard.SetDataObject(txtBody.SelectedText);
txtBody.SelectedText = "";
}

// copy dữ liệu đang được chọn lựa trong textbox


private void menuItemCopy_Click(object sender, System.EventArgs e)
{
txtBody.Copy();
}

// paste dữ liệu vào trong textbox


private void menuItemPast_Click(object sender, System.EventArgs e)
{
txtBody.Paste();
}

// select all dữ liệu


private void menuItemSelectAll_Click(object sender, System.EventArgs e)
{
txtBody.SelectAll();
}

#endregion

// thay đổi font chữ


private void menuItemFont_Click(object sender, System.EventArgs e)
{
C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\NotePad.cs 9
// mở hộp thoại thay đổi font chữ
dlgResult = dlgFont.ShowDialog();
if(dlgResult == DialogResult.Cancel)
return;

FontFamily fmFontName = dlgFont.Font.FontFamily;


float fFontSize = dlgFont.Font.Size;
FontStyle fsStyle = dlgFont.Font.Style;
Font font;
try
{
// apply font chữ cho textbox
font = new Font(fmFontName,fFontSize,txtBody.Font.Style ^ fsStyle);
txtBody.Font = font;
}
catch(ArgumentException)
{
return;
}
}

// thoát khỏi ứng dụng


private void menuItem6_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void menuItem5_Click(object sender, EventArgs e)


{
About aboutKFC = new About();
aboutKFC.ShowDialog();
}

private void menuItem4_Click(object sender, EventArgs e)


{
About abouKFC = new About();
abouKFC.ShowDialog();
}
}
}

You might also like