381
Chương 9: File, thư mục, và I/O
private System.Windows.Forms.MenuItem mnuSave;
private System.Windows.Forms.MenuItem mnuExit;
private System.Windows.Forms.RichTextBox rtDoc;
// (Bỏ qua phần mã designer.)
private void mnuOpen_Click(object sender, System.EventArgs e) {
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Rich Text Files (*.rtf)|*.RTF|" +
"All files (*.*)|*.*";
dlg.CheckFileExists = true;
dlg.InitialDirectory = Application.StartupPath;
if (dlg.ShowDialog() == DialogResult.OK) {
rtDoc.LoadFile(dlg.FileName);
rtDoc.Enabled = true;
}
}
private void mnuSave_Click(object sender, System.EventArgs e) {
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "RichText Files (*.rtf)|*.RTF|Text Files (*.txt)|
*.TXT" +
"|All files (*.*)|*.*";
dlg.CheckFileExists = true;
dlg.InitialDirectory = Application.StartupPath;
if (dlg.ShowDialog() == DialogResult.OK) {
rtDoc.SaveFile(dlg.FileName);
}
}
private void mnuExit_Click(object sender, System.EventArgs e) {
this.Close();
}