0% found this document useful (0 votes)
463 views9 pages

Hướng Dẫn Xóa Ghi Chú Trong Word

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)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
463 views9 pages

Hướng Dẫn Xóa Ghi Chú Trong Word

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)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

cs 1
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace MyNotepad
{
/// <summary>
/// Summary description for frmNotepad.
/// </summary>
public class frmNotepad : [Link]
{
private [Link] mainMenuNotepad;
private [Link] menuItemFile;
private [Link] menuItemNew;
private [Link] menuItemOpen;
private [Link] menuItemSeparator1;
private [Link] menuItemSave;
private [Link] menuItemSaveAs;
private [Link] menuItemEdit;
private [Link] menuItemCut;
private [Link] menuItemCopy;
private [Link] menuItemPast;
private [Link] menuItemSelectAll;
private [Link] menuItemFormat;
private [Link] menuItemFont;
private [Link] txtBody;
private [Link] dlgOpenFile;
private [Link] dlgSaveFile;
private [Link] dlgFont;

private [Link] contextMenuTxtBody;


private [Link] ContextCut;
private [Link] ContextCopy;
private [Link] ContextPaste;
private [Link] ContextSeparator1;
private [Link] ContextSelectAll;
private [Link] ContextSeparator2;
private [Link] 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à [Link]
// nếu new 1 document nữa thì sẽ được đánh tên là [Link]
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\[Link] 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)
{
[Link]();
}
}
CheckChanged(null,null);
[Link]( 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()
{
[Link] = new [Link]();
[Link] = new [Link]([Link]);
[Link] = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
this.menuItemSeparator1 = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
this.menuItem7 = new [Link]();
this.menuItem6 = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
this.menuItem1 = new [Link]();
this.menuItem2 = new [Link]();
this.menuItem3 = new [Link]();
this.menuItem4 = new [Link]();
this.menuItem5 = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
this.ContextSeparator2 = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
this.ContextSeparator1 = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\[Link] 3
[Link] = new [Link]();
[Link]();
//
// mainMenuNotepad
//
[Link](new [Link][] {
[Link],
[Link],
[Link],
this.menuItem1,
this.menuItem3});
//
// menuItemFile
//
[Link] = 0;
[Link](new [Link][] {
[Link],
[Link],
this.menuItemSeparator1,
[Link],
[Link],
this.menuItem7,
this.menuItem6});
[Link] = "File";
//
// menuItemNew
//
[Link] = 0;
[Link] = [Link];
[Link] = "&New";
[Link] += new [Link](this.menuItemNew_Click);
//
// menuItemOpen
//
[Link] = 1;
[Link] = [Link];
[Link] = "&Open...";
[Link] += new [Link](this.menuItemOpen_Click);
//
// menuItemSeparator1
//
[Link] = 2;
[Link] = "-";
//
// menuItemSave
//
[Link] = false;
[Link] = 3;
[Link] = [Link];
[Link] = "&Save";
[Link] += new [Link](this.menuItemSave_Click);
//
// menuItemSaveAs
//
[Link] = 4;
[Link] = "Save As...";
[Link] += new [Link](this.
menuItemSaveAs_Click);
//
// menuItem7
//
[Link] = 5;
[Link] = "-";
//
// menuItem6
//
[Link] = 6;
C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\[Link] 4
[Link] = "Exit";
[Link] += new [Link](this.menuItem6_Click);
//
// menuItemEdit
//
[Link] = 1;
[Link](new [Link][] {
[Link],
[Link],
[Link],
[Link]});
[Link] = "Edit";
//
// menuItemCut
//
[Link] = 0;
[Link] = [Link];
[Link] = "&Cut";
[Link] += new [Link](this.menuItemCut_Click);
//
// menuItemCopy
//
[Link] = 1;
[Link] = [Link];
[Link] = "&Copy";
[Link] += new [Link](this.menuItemCopy_Click);
//
// menuItemPast
//
[Link] = 2;
[Link] = [Link];
[Link] = "&Paste";
[Link] += new [Link](this.menuItemPast_Click);
//
// menuItemSelectAll
//
[Link] = 3;
[Link] = [Link];
[Link] = "&Select All";
[Link] += new [Link](this.
menuItemSelectAll_Click);
//
// menuItemFormat
//
[Link] = 2;
[Link](new [Link][] {
[Link]});
[Link] = "Format";
//
// menuItemFont
//
[Link] = 0;
[Link] = "Font...";
[Link] += new [Link](this.menuItemFont_Click);
//
// menuItem1
//
[Link] = 3;
[Link](new [Link][] {
this.menuItem2});
[Link] = "View";
//
// menuItem2
//
[Link] = 0;
[Link] = "Status Bar";
//
C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\[Link] 5
// menuItem3
//
[Link] = 4;
[Link](new [Link][] {
this.menuItem4,
this.menuItem5});
[Link] = "Help";
//
// menuItem4
//
[Link] = 0;
[Link] = "Help Topics";
[Link] += new [Link](this.menuItem4_Click);
//
// menuItem5
//
[Link] = 1;
[Link] = "About Notepad";
[Link] += new [Link](this.menuItem5_Click);
//
// txtBody
//
[Link] = [Link];
[Link] = [Link];
[Link] = new [Link]("Microsoft Sans Serif", 12F,
[Link], [Link], ((byte)(0)));
[Link] = new [Link](0, 0);
[Link] = true;
[Link] = "txtBody";
[Link] = [Link];
[Link] = new [Link](480, 381);
[Link] = 0;
[Link] += new [Link](this.txtBody_TextChanged);
//
// contextMenuTxtBody
//
[Link](new [Link][]
{
[Link],
this.ContextSeparator2,
[Link],
[Link],
[Link],
this.ContextSeparator1,
[Link]});
//
// ContextFont
//
[Link] = 0;
[Link] = "Font..";
[Link] += new [Link](this.menuItemFont_Click);
//
// ContextSeparator2
//
[Link] = 1;
[Link] = "-";
//
// ContextCut
//
[Link] = 2;
[Link] = "Cut";
[Link] += new [Link](this.menuItemCut_Click);
//
// ContextCopy
//
[Link] = 3;
[Link] = "Copy";
C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\[Link] 6
[Link] += new [Link](this.menuItemCopy_Click);
//
// ContextPaste
//
[Link] = 4;
[Link] = "Paste";
[Link] += new [Link](this.menuItemPast_Click);
//
// ContextSeparator1
//
[Link] = 5;
[Link] = "-";
//
// ContextSelectAll
//
[Link] = 6;
[Link] = "Select All";
[Link] += new [Link](this.
menuItemSelectAll_Click);
//
// dlgSaveFile
//
[Link] = "doc1";
//
// dlgFont
//
[Link] = true;
[Link] = true;
//
// frmNotepad
//
[Link] = new [Link](5, 13);
[Link] = new [Link](480, 381);
[Link]([Link]);
[Link] = [Link];
[Link] = "frmNotepad";
[Link] = "NotePad với C# => Customized by [Link]";
[Link](false);
[Link]();

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
[Link](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, [Link] 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, [Link] e)
{
if(m_bModified)
C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\[Link] 7
{
dlgResult = [Link]("Muốn lưu file ?","[Link]",
[Link],[Link]);
if(dlgResult == [Link])
{
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, [Link] e)
{
CheckChanged(sender,e);
m_strFileName = "";
[Link]();
m_bModified = false;
}

// save as document
private void menuItemSaveAs_Click(object sender, [Link] e)
{

[Link] = "kattyfleaDoc" + m_intDocNumber.ToString() + ".txt";


dlgResult = [Link]();

if(dlgResult == [Link])
return;

try
{
m_strFileName = [Link];
m_sw = new StreamWriter(m_strFileName);
m_sw.Write ([Link]);
m_sw.Close();
[Link] = true;
m_intDocNumber++;
m_bModified = false;
[Link] = "C# Simple Notepad với [Link]: " + m_strFileName;
}
catch(Exception err)
{
[Link]([Link],"lỗi",[Link],MessageBoxIcon.
Error);
}
}

// save file
private void menuItemSave_Click(object sender, [Link] 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 ([Link]);
m_sw.Close();
m_bModified = false;
}
C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\[Link] 8

// open 1 file mới


private void menuItemOpen_Click(object sender, [Link] 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 = [Link]();

if(dlgResult == [Link])
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 = [Link];
StreamReader sr = new StreamReader(m_strFileName);
[Link] = [Link]();
[Link]();
m_bModified = false;
}
catch(Exception err)
{
[Link]([Link],"Error",[Link],MessageBoxIcon.
Error);
}
}

#endregion

#region edit menu


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

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


private void menuItemCopy_Click(object sender, [Link] e)
{
[Link]();
}

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


private void menuItemPast_Click(object sender, [Link] e)
{
[Link]();
}

// select all dữ liệu


private void menuItemSelectAll_Click(object sender, [Link] e)
{
[Link]();
}

#endregion

// thay đổi font chữ


private void menuItemFont_Click(object sender, [Link] e)
{
C:\Documents and Settings\WELLCOME\Desktop\HoangPrj\MyNotepad\[Link] 9
// mở hộp thoại thay đổi font chữ
dlgResult = [Link]();
if(dlgResult == [Link])
return;

FontFamily fmFontName = [Link];


float fFontSize = [Link];
FontStyle fsStyle = [Link];
Font font;
try
{
// apply font chữ cho textbox
font = new Font(fmFontName,fFontSize,[Link] ^ fsStyle);
[Link] = font;
}
catch(ArgumentException)
{
return;
}
}

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


private void menuItem6_Click(object sender, EventArgs e)
{
[Link]();
}

private void menuItem5_Click(object sender, EventArgs e)


{
About aboutKFC = new About();
[Link]();
}

private void menuItem4_Click(object sender, EventArgs e)


{
About abouKFC = new About();
[Link]();
}
}
}

You might also like