What s to Do?


The Enter key is handled in the KeyDownHandler code in the XMLNotepad class. When we started, it looked like this:

 public void XMLKeyDownHandler(object objSender, KeyEventArgs kea) { 
model.SetLines(textbox.Lines);
model.SelectionStart = textbox.SelectionStart;
if (kea.KeyCode == Keys.Enter) {
TypeEnter();
kea.Handled = true;
}
PutText(textbox, model.LinesArray(), model.SelectionStart);
}
public void TypeEnter() {
model.Enter();
}

Our concern, as new C# Microsoft .NET programmers, was that we needed to figure out how to get two different behaviors from typing Enter, depending on whether the Shift key was down. As implemented, Enter inserts a paragraph tag, as you probably remember. So does Shift+Enter, Alt+Enter, Ctrl+Shift+Alt+Function+Enter, and so on. With a little research, we found that the KeyEventArgs (kea) has another property, Modifiers, which can be tested for values like Keys.Shift. Just the thing:

 public void XMLKeyDownHandler(object objSender, KeyEventArgs kea) { 
model.SetLines(textbox.Lines);
model.SelectionStart = textbox.SelectionStart;
if (kea.KeyCode == Keys.Enter && kea.Modifiers == Keys.None ) {
TypeEnter();
kea.Handled = true;
}
PutText(textbox, model.LinesArray(), model.SelectionStart);
}

This should change the GUI so that paragraph tags are inserted only when you type Enter with no other keys down. And it worked. This was what we wanted to learn: how to separate out the two similar keystrokes. So we added the code for our new Shift+Enter:

 public void XMLKeyDownHandler(object objSender, KeyEventArgs kea) { 
model.SetLines(textbox.Lines);
model.SelectionStart = textbox.SelectionStart;
if (kea.KeyCode == Keys.Enter && kea.Modifiers == Keys.None) {
TypeEnter();
kea.Handled = true;
}
else if (kea.KeyCode == Keys.Enter && kea.Modifiers == Keys.Shift) {
model.InsertReturn();
kea.Handled = true;
} PutText(textbox, model.LinesArray(), model.SelectionStart);
}

This worked fine, just like we had hoped! However, in writing the new code, we noticed that odd little method TypeEnter(). It seems useless. We think we wrote it originally as some attempt to break out the human factors action (typing Shift+Enter) from what the system does with that action (send Enter() to the model). We definitely don t like it now, so we removed it, putting the code back in line:

 public void XMLKeyDownHandler(object objSender, KeyEventArgs kea) { 
model.SetLines(textbox.Lines);
model.SelectionStart = textbox.SelectionStart;
if (kea.KeyCode == Keys.Enter && kea.Modifiers == Keys.None) {
model.Enter();
kea.Handled = true;
}
else if (kea.KeyCode == Keys.Enter && kea.Modifiers == Keys.Shift) {
model.InsertReturn();
kea.Handled = true;
}
PutText(textbox, model.LinesArray(), model.SelectionStart);
}

Now this is certainly a questionable change. Perhaps the broken-out version is better, but we didn t think so. It didn t communicate much more about what was going on, and it increased the general complexity. Your mileage may vary. On your code, use your judgment.




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