完整程式碼:
form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace P20160328_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Menu_File_Exit_Click(object sender, EventArgs e)
{
this.Close();
}
private void Menu_File_Open_Click(object sender, EventArgs e)
{
if (OFDialog.ShowDialog() == DialogResult.OK)
{
Form2 frm = new Form2() ;
frm.MdiParent = this;
frm.Show() ;
frm.WindowState = FormWindowState.Maximized;
if(OFDialog.FilterIndex == 1)
frm.RTB.LoadFile(OFDialog.FileName, RichTextBoxStreamType.PlainText);
else
frm.RTB.LoadFile(OFDialog.FileName, RichTextBoxStreamType.RichText);
frm.Text = OFDialog.FileName;
}
}
private void Menu_File_Save_Click(object sender, EventArgs e)
{
//get avtive mdi child window
Form2 activefrm = (Form2)this.ActiveMdiChild;
if (activefrm != null)
{
//check file format
if (activefrm.Text.Substring(activefrm.Text.Length - 3, 3) == "txt")
activefrm.RTB.SaveFile(activefrm.Text.Substring(2, activefrm.Text.Length - 2),
RichTextBoxStreamType.PlainText);
else
activefrm.RTB.SaveFile(activefrm.Text.Substring(2, activefrm.Text.Length - 2),
RichTextBoxStreamType.RichText);
//modify text of the active window
activefrm.Text = activefrm.Text.Substring(2, activefrm.Text.Length - 2);
}
}
private void Menu_File_SaveAs_Click(object sender, EventArgs e)
{
//ok button of save file dialog is pressed
if (SFDialog.ShowDialog() == DialogResult.OK)
{
Form2 activefrm = (Form2)this.ActiveMdiChild;
//check file format
if (SFDialog.FilterIndex == 1)
{
activefrm.RTB.SaveFile(SFDialog.FileName,
RichTextBoxStreamType.PlainText);
}
else
{
activefrm.RTB.SaveFile(SFDialog.FileName,
RichTextBoxStreamType.RichText);
}
//modify text of the active window
activefrm.Text = SFDialog.FileName;
}
}
private void Menu_Edit_Font_Click(object sender, EventArgs e)
{
Form2 activefrm = (Form2)this.ActiveMdiChild;
if (activefrm != null)
if (FDialog.ShowDialog() == DialogResult.OK)
activefrm.RTB.SelectionFont = FDialog.Font;
}
private void Menu_Edit_Color_Click(object sender, EventArgs e)
{
Form2 activefrm = (Form2)this.ActiveMdiChild;
if (activefrm != null)
if (CDialog.ShowDialog() == DialogResult.OK)
activefrm.RTB.SelectionColor = CDialog.Color;
}
private void Form1_MdiChildActivate(object sender, EventArgs e)
{
Form2 activefrm = (Form2)this.ActiveMdiChild;
if (activefrm != null)
{
Menu_File_Save.Enabled = true;
Menu_File_SaveAs.Enabled = true;
Menu_Edit_Font.Enabled = true;
Menu_Edit_Color.Enabled = true;
}
else
{
Menu_File_Save.Enabled = false;
Menu_File_SaveAs.Enabled = false;
Menu_Edit_Font.Enabled = false;
Menu_Edit_Color.Enabled = false;
}
}
}
}
form2.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace P20160328_1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void RTB_TextChanged(object sender, EventArgs e)
{
if (this.Text.Substring(0, 1) != "*")
this.Text = "* " + this.Text;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.Text.Substring(0, 1) == "*")
{
if (MessageBox.Show("Save?", "File modified !", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
if (this.Text.Substring(this.Text.Length - 3, 3) == "txt")
RTB.SaveFile(this.Text.Substring(2, this.Text.Length - 2), RichTextBoxStreamType.PlainText);
else
RTB.SaveFile(this.Text.Substring(2, this.Text.Length - 2), RichTextBoxStreamType.RichText);
}
}
}
}
}
form1 視窗設計
form2 視窗設計
沒有留言:
張貼留言