Petzold Notepad Version 1


Petzold s book includes a number of interesting yet simple form examples, showing how forms are displayed and closed, how to change some text, and so on. I worked through a couple of those just for fun. Petzold talks about the Application object, about events, and so on. Those concepts were familiar to me and probably are to you as well. And in the spirit of learning something about my real topic, which is an XML editor, I moved on to experiment with Petzold s example that builds a clone of the famous Windows Notepad application. (This was quite a jump ”it s on page 847 of his book ”but it s still a simple program.) And here it is, as I typed it into a new solution and project:

 using System; 
using System.Drawing;
using System.Windows.Forms;

namespace Notepad1
{
class NotepadCloneNoMenu : Form
{
protected TextBox txtbox;

public static void Main()
{
Application.Run(new NotepadCloneNoMenu());
}

public NotepadCloneNoMenu() {
Text = "Notepad Clone No Menu";

txtbox = new TextBox();
txtbox.Parent = this;
txtbox.Dock = DockStyle.Fill;
txtbox.BorderStyle = BorderStyle.None;
txtbox.Multiline = true;
txtbox.ScrollBars = ScrollBars.Both;
txtbox.AcceptsTab = true;
}
}
}

That s the program as Petzold provided it. In his book, he goes on to do things with the registry and then with menus . That s not my interest at the moment. I m more interested in learning how to make a simple action at the keyboard insert XML characters . The action I have in mind is typing a control character. I ll experiment with that by creating a new class, XMLNotepad, that inherits from the NotepadCloneNoMenu class:

 using System; 
using System.Drawing;
using System.Windows.Forms;

namespace Notepad1
{
class XMLNotepad : NotepadCloneNoMenu
{
[STAThread]
static void Main(string[] args)
{
Application.Run(new XMLNotepad());
}

public XMLNotepad() {
Text = "XML Notepad";
}
}
}

To make this work, I fumbled around in Visual Studio a fair amount, trying to learn about solutions and projects. And I had to remove the Main() method from the NotepadCloneNoMenu class to put it in mine. I m trying to spare you the painful report of every mistake I made so that we can get down to the real work. The end result is a program with the same functionality as the original, except with a new Text property set into the top bar of the window. This is enough to show me that my subclass is up and running.

Lesson  

There s great value to reading the books and examples written by other people. I prefer books, in general, because they tend to hang together better than the separate articles we can find on the Web. Books are kind of expensive sometimes, but ask yourself what you d pay for a good idea when you need one. Fifty bucks or so might be a bargain. On the other hand, when it gets down to learning how to do something more specific, you may not have a book that talks about that subject and it s very likely out there on the Web somewhere. See this book s Bibliography for a list of some of the resources I found along the way.

There s some art to selecting spikes [2] to do, especially when working from a book. I skipped all over Petzold s book, trying things I didn t know, but always trying to focus on things that led me in the direction I was heading in: an editor for XML. When the book started to lead me off into menus, I could have continued , but it seemed like time to start adapting the ideas in the books to my own program. In general, I think it pays off to put things into the context of the problem we re trying to solve, as soon as we reasonably can.

I could have started modifying the original Petzold program instead of creating my own subclass. I chose to create the subclass for a few reasons. First, it allowed me to preserve Petzold s running code without change. I felt safer in doing that, in part because I haven t set up any code management software yet. Second, I frankly wanted to try subclassing in C#, just to get a feeling for it. Third, Petzold, in his later examples, uses subclasses based on this original class, so I assumed that it was a good idea, without a clear understanding of why. Later on in the book, you ll see that I reverse that decision and get rid of the Petzold class.

Lesson  

And here s a lesson about the previous lesson. As we go forward in this book, you ll see again and again that we try things. Some of them work and we keep them forever. Some of them work for a while and then get changed. Some don t work at all, and of course we don t keep them at all. I learned long ago that I make mistakes in designing things; I d encounter trouble in programming when the design didn t bear up under the weight of reality. For a long time, I assumed that I needed to do more and better design. That s no longer my view. My view today is that it s better to start delivering useful software with a simple, clear design and to evolve the design as I go forward.

If you re like me, at first you ll fear that evolving the design will be inefficient or that you might reach some corner you can t get out of. This book is exploring what will happen if we do very little design up front and instead lots of design all the time . Please note that distinction: I m not saying no to design. I m saying to sb design all the time.

[2] Spike is an Extreme Programming term meaning experiment. We use the word because we think of a spike as a quick, almost brute-force experiment aimed at learning just one thing. Think of driving a big nail through a board.




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