Note Pad
Note Pad
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;
// 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 = "";
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 );
}
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmNotepad());
}
// 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);
}
}
}
// save as document
private void menuItemSaveAs_Click(object sender, System.EventArgs e)
{
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
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
#endregion