Chapter 10: Find a Bug, Write a Test


Last time out, I found a bug. Our rules of engagement require us to write a test, first to show that the bug exists and later to verify that we have fixed it, but we don t know how to write that test! We get by with a little help from our friends .

Oh No, There s a Bug!

In the previous chapter, I found a defect in the program: when insertion of a new set of paragraph tags takes you below the bottom of the window, the text doesn t scroll up to keep the cursor on the screen. You have to keep clicking on the scroll bars to bring the line back up.

It took me only a little while to find and fix the defect. Here s the code, from the XMLNotepad class, with the fix. That s what we re going to work on today:

 void XMLKeyDownHandler(object objSender, KeyEventArgs kea) { 
model.SetLines(textbox.Lines);
model.SelectionStart = textbox.SelectionStart;
if (kea.KeyCode == Keys.Enter) {
model.Enter();
kea.Handled = true;
}
if (kea.KeyCode == Keys.L && kea.Modifiers == Keys.Control) {
String[] lines = textbox.Lines;
foreach ( String s in lines) {
Console.WriteLine(s);
}
Console.WriteLine("model");
//Console.WriteLine(model.Text);
kea.Handled = true;
}
if (kea.KeyCode == Keys.S && kea.Modifiers == Keys.Control) {
textbox.SelectionLength = 0;
kea.Handled = true;
}
textbox.Lines = model.LinesArray();
textbox.SelectionStart = model.SelectionStart;
textbox.ScrollToCaret(); // keep cursor on screen. no test.
}

It turns out that when you set the text into a TextBox and then set the cursor, it doesn t force the cursor onto the screen. You have to call the ScrollToCaret method to get that to happen. Finding that method and sending the message was the fix, but only the beginning of my pain.




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

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