Form1窗口:
namespace Interpreter{ public class InterpForm : System.Windows.Forms.Form{ private System.Windows.Forms.TextBox txCommand; private System.Windows.Forms.Button btCompute; private System.Windows.Forms.ListBox lsResults; private StudentData kdata; public InterpForm(){ InitializeComponent(); init();} private void init() { kdata = new StudentData ("data.txt");}
[STAThread] static void Main() { Application.Run(new InterpForm());}
private void btCompute_Click(object sender, System.EventArgs e) {
Parser par = new Parser (txCommand.Text ,kdata, lsResults); par.Execute ();}}} 13、 结合命令模式和备忘录模式实现程序的undo和redo功能。 答:Form1:
public Form1() { InitializeComponent(); init();}
private void init() {
btBlue.setCommand (blueC=new BlueCommand (pBox)); btRed.setCommand (redC=new RedCommand (pBox)); btUndo.setCommand (undoC = new UndoComd ()); btRedo.setCommand(redoC = new RedoComd()); EventHandler evh = new EventHandler (commandClick); btBlue.Click += evh; btRed.Click += evh; btUndo.Click += evh; btRedo.Click += evh; pBox.Paint += new PaintEventHandler (paintHandler); redoundo();}
public void redoundo(){
RedoComd.redolist = new ArrayList(); if (RedoComd.redolist.Count < 1){ btRedo.Enabled = false;}
UndoComd.undolist = new ArrayList(); if (UndoComd.undolist.Count < 1){ btUndo.Enabled = false;}} private void commandClick(object sender, EventArgs e) { Command comd = ((CommandHolder)sender).getCommand ();