Handling the Enter


All that glorious emergent design comes later. Here s what happened in our first couple of hours.

We had some starting code for handling events, which we moved into our XMLNotepad class from the Petzold prototype discussed previously. Here s our handler:

 void XMLKeyDownHandler(object objSender, KeyEventArgs kea) { 
if (kea.KeyCode == Keys.P && kea.Modifiers == Keys.Control) { txtbox.Text += "controlP";
kea.Handled = true;
}
if (kea.KeyCode == Keys.L && kea.Modifiers == Keys.Control) {
String[] lines = txtbox.Lines;
foreach ( String s in lines) {
Console.WriteLine(s);
}
kea.Handled = true;
}
}
Lesson  

The highlighted if statement referring to Keys.P is the code that handles Ctrl+P for our prototype. All it does is add the string controlP to the text, when you type Ctrl+P. You probably remember that from Chapter 1, XML Notepad Stories. As dumb as it looks, I d like to remind us again that this is a good example of a well- chosen spike. (A spike is what we call a simple experiment aimed at learning how to do something. Think of driving a big spike through a board.) The other ifstatement is one we put in to display the TextBox contents on the console, for fun and for debugging.

This simple code does at least four valuable things in just a few lines:

  1. It teaches us how to hook a KeyDown event.

  2. It teaches us how to distinguish a Ctrl+P from a regular P.

  3. It teaches us how to programmatically add text to the TextBox.

  4. It teaches us how to tell Microsoft Windows that we have handled the key ”or at least we thought it did.

When we re trying to learn how something works, we don t fiddle around trying to get something important implemented. Instead, we take little tiny bites, trying to learn the most while typing the least. Since we don t know what we re doing, by definition, the less code we write, the less we ll confuse ourselves . And we re easily confused when we re trying something we ve never done before. Maybe you are, as well.

So, based on that spike, the handling of Enter for our story gave us these simple tasks :

  1. Write an Enter KeyDown handler just like the Ctrl+P one.

  2. Figure out what line has the cursor.

  3. Create a new line array with all the lines up to that one, followed by a blank line, followed by a line with <P></P>, followed by all the remaining lines.

  4. Set the cursor in between those P tags.

It seemed easy enough. We wrote the handler in the obvious way, referring to kea.KeyCode==Keys.Enter. No problem. Only thing was, we needed the code to get the CursorLine. I had written some experimental code to figure out which line had the cursor, and I was going to extract that into a method, but Chet had a better idea: Just write the CursorLine method to return a constant. That will tell us if the code works. So we had the handler, and a CursorLine method, like this:

 if (kea.KeyCode == Keys.Enter) { 
String[] lines = txtbox.Lines;
int cursorLine = CursorLine();
String[] newlines = new String[lines.Length+2];
for(int i = 0; i <= cursorLine; i++) {
newlines[i] = lines[i];
}
newlines[cursorLine+1] = "";
newlines[cursorLine+2] = "<P></P>";
for (int i = cursorLine+1; i < lines.Length; i++) {
newlines[i+2] = lines[i];
}
txtbox.Lines = newlines;
kea.Handled = true;
}
...
private int CursorLine() { return 3; }

This code is pretty straightforward. We get the lines from the TextBox and get the cursor line, by cheating for now. We copy the lines from the beginning up through the cursor line, add our two new lines, and copy the rest of the lines. Then we slam the lines back into the TextBox and report that we handled the event.

As I look at that code now, it doesn t express our intention very well. It would have been better to write it expressing intention because it had a few bugs in it before we got it right: various off-by-one errors. We ll give some examples of that later on, but that s not the core of tonight s tale.

(Note during editing: Looking at this code much later, as I edit the book, I realize that there are surely ways that ArrayList or Array class could have helped with this. We didn t know it at the time. Let s watch to see whether we write better code later and whether this code ever gets cleaned up.)




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