Team LiB
Previous Section Next Section

Some Points About the Code

The code that I wrote for the examples follows some simple guidelines. I describe those guidelines here so you will know what to expect and where to find certain code sections.

Designating Events

I like to know what is happening in my code and I also like to be able to control it. There is quite a bit of functionality in the IDE that allows you to automatically handle events or assign event handlers using the IDE. As far as VB .NET goes, you can get an event handler for a certain control by double-clicking the control. The IDE will generate the event shell for you similar to VB 6.0. This consists of the event handler procedure name followed by the event it handles. It looks like this:


Private Sub cmdClose_Click(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) Handles cmdClose.Click

The C# IDE does much the same thing, except that it creates a delegate for you and assigns the delegate within the "Windows Form Designer generated code" section.

I do not use this method of programming for events for the following reasons:

  • The IDE will choose the name of my event handler. I can change it later, but in the case of C# I need to dig through the "Windows Form Designer generated code" section to find the delegate assignment.

  • Using VB, you cannot remove or change the event handler assignment at runtime.

You are also able to assign event handlers for C# using the IDE directly. You can do this by assigning handler names in the Properties screen of the IDE, as shown in Figure 1.

Click To expand
Figure 1: Assigning event handlers via the Properties screen

Although this is all well and good, it has two problems:

  • The delegate assignment is done in the "Windows Forms Designer generated code" section.

  • If you delete the control, the event handler code still remains.

Here is what I prefer to do. If I know at design time what I need, I assign all my event handlers in the constructor. This puts them all in one place, one right after the other.

For VB code, I use the AddHandler command to assign delegates to events. This allows me to assign multiple events to one handler, and it also allows me to use RemoveHandler later in the code to remove a delegate from handling an event.

Compartmentalizing Code

I like to use regions. I use a section at the top of each class to hold all the class local variables. I put all the mouse handling events in their own region. I like to put all class properties in their own region.

Using regions is a great way to partition your code and see only the section of code that you are working on.

Included Code

When I provide code in this book, I usually do not include the "Windows Form Designer generated code" region. I instead put a marker to note where this section belongs.

This section contains all the stuff the IDE puts in your code that concerns the controls on your form. It also has the Dispose method for both C# and VB. The constructor for VB is in this section as well, but the C# constructor is not.

Anyway, if you are typing in the project as I describe, the IDE will fill in this code for you. Including it is a waste of paper.

It is time to turn the page and start programming. I hope that you will find this book interesting and gain some insights that help you in your career.


Team LiB
Previous Section Next Section