Adding the Menu


For my initial cut at the menu, I ll add InsertSection right at the top. The code looks like this:

 public XMLNotepad() { 
model = new TextModel();
Text = "XML Notepad";
MenuItem insertSection = new MenuItem ( "Insert &Section",
new EventHandler(MenuInsertSection));
Menu = new MainMenu(new MenuItem[] {insertSection} );
textbox = new TestableTextBox();
textbox.Parent = this;
textbox.Dock = DockStyle.Fill;
textbox.BorderStyle = BorderStyle.None;
textbox.Multiline = true;
textbox.ScrollBars = ScrollBars.Both;
textbox.AcceptsTab = true;
textbox.KeyDown += new KeyEventHandler(XMLKeyDownHandler);
textbox.KeyPress += new KeyPressEventHandler(XMLKeyPressHandler);
this.textbox.Visible = true;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[]
{ this.textbox});
this.Name = "XMLNotepad";
}
void MenuInsertSection(object obj, EventArgs ea) {
Console.WriteLine("Insert Section");
}

I just created a new MenuItem, with an accelerator of Alt+S (signified by the &S in Section). When clicked, it will invoke the handler MenuInsertSection, which I ve got set up just to display something to the console. Sure enough, the menu comes up on the screen, as shown here, and the console is printing the message.

You might be wondering about that @Send thing showing up in the menu bar. That s from my fax software, eFax. It adds that icon to any window that it thinks contains text that you might want to fax. I m not sure why it chose to appear in this app, but it s perfectly OK with me that it did, at least for now.

Now it s time to make the menu item work. I ll just extract the code from the AltS in the key handler and move it over to the MenuInsertSection. Cleverly, I m going to remember to move the lines that read and reset the TextBox:

 void MenuInsertSection(object obj, EventArgs ea) { 
model.SetLines(textbox.Lines);
model.SelectionStart = textbox.SelectionStart; model.AltS(); PutText(textbox, model.LinesArray(), model.SelectionStart);
}

It works! The tests all run, Alt+S inserts the section tags, and the irritating beep is gone. But there s a pretty obvious problem with that code. Note that it sends the AltS message to the TextModel. This code makes it clear, if it wasn t already, that we have some confusion between the human factors gestures, Enter and Alt+S, and the model s logical behavior, InsertParagraphTag and InsertSectionTags.

We might choose not to worry about it. The new menu approach, carried forward, will probably help us provide isolation between the keystroke gestures and the actions. The menu item names will be the actions, which is good enough for now. For now I ll change the code to send InsertSectionTags, which makes more sense to me. This might not be good enough for all time. If this application were going to be internationalized, for example, the menu items might change. We ll definitely not worry about that for now: we don t have a story for it.




Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net