| 5 Handy Hints for Writing .NET Programs Posted by: Xav in Untagged on Jul 01, 2008 |
Well well well. Anyone can write a program. Well, maybe not everyone. But it isn't that difficult. However, not everyone can write a good program. There are a few pieces of advice when writing programs, that any decent programmer should bear in mind. Here's a brainstorm:
Note: code references are to .NET, but techniques can be applied across languages.
1. Keep it responsive
A program can do something amazing, but it's no good if it just freezes up. A user who has to wait for a program to unfreeze is not a happy user, and bear in mind that different computers have different levels of processing power, so it is important to allow for this.
A good way to ensure responsiveness is to use Application.DoEvents() in .NET, like so:


You can also use multithreading, but in either case, you also need to:
2. Tell the user what's going on
So the user clicks on "Go". The application might not freeze (like above), but the user needs to know if the program is doing something - otherwise, they might not know an action is being performed.
A StatusBar is very useful - the bar that sits at the bottom of the screen. In its simplest form, a simple label that sits in the bottom-left corner of the screen, that tells the user what's happening. You could create a method like this:

Then, to use the code, just simply call the method, passing it a string parameter:

Another useful option is to use a ProgressBar. This is the bar that can be filled to show the ongoing progress of an action. To use it, add one from the Toolbox, then set the Maximum and Minimum values in the Properties Window. To set the value in code, simply change the property:

Assuming the Maximum property is set to 100, the progress bar will be set to just over half full.
3. Stick with what they know
This is an easy one - don't confuse the user. Make use of well-known programs, such as Office. For example, always use Ctrl-S for saving, Ctrl-P for printing and Ctrl-F for finding. This way, a user does not need to learn a new set of shortcuts. On menus, you can easily set this using the Properties Window.
Also, copy other programs' layouts. The more you familiarise yourself with the 'common' program layouts, the more familiar your programs will seem to users. Learn that the menu goes at the top, the status bar goes at the bottom. Buttons are always aligned, text boxes are generally the same length. Important actions such as deleting and closing should always be confirmed with a simple dialog, perhaps in the form's FormClosing event. To cancel the closing of a form, use:

The event argument 'e' contains data about the FormClosing event. The Cancel property is a boolean value - simply set it to true to stop the form from closing. A good idea is to use MessageBox.Show(), passing in the four standard parameters - the text, the title, and two enumerations describing which buttons and icon to display.
4. Test it, test it, test it again
Bugs, bugs, bugs, Every program is vulnerable to bugs, and they need to be found and eliminated ASAP. Run the program, making things extreme. Try your best to cause an error (known as an Exception), then swoop in and work out what went wrong. Then, fix the error. If you need to trap errors when the worst happens, use a try/catch block, like so:

If possible, get friends and family to test the program for you, or if you're a pro, hire a software tester. Errors don't go down well... but if they do, then they need to be able to contact you for...
5. Help!
Whether your program is complex or not, users are bound to need to contact you in some way. Always include a Help menu on your menu strip at the top, preferably with a link to a website/email. You can make it load the page automatically, using a WebBrowser control. Users can request assistance, alert you of bugs and even donate - so you get paid for all your hard work. :)
Note: code references are to .NET, but techniques can be applied across languages.
1. Keep it responsive
A program can do something amazing, but it's no good if it just freezes up. A user who has to wait for a program to unfreeze is not a happy user, and bear in mind that different computers have different levels of processing power, so it is important to allow for this.
A good way to ensure responsiveness is to use Application.DoEvents() in .NET, like so:

- Application.DoEvents();
Application.DoEvents();

- Application.DoEvents()
Application.DoEvents()
You can also use multithreading, but in either case, you also need to:
2. Tell the user what's going on
So the user clicks on "Go". The application might not freeze (like above), but the user needs to know if the program is doing something - otherwise, they might not know an action is being performed.
A StatusBar is very useful - the bar that sits at the bottom of the screen. In its simplest form, a simple label that sits in the bottom-left corner of the screen, that tells the user what's happening. You could create a method like this:

- private void Status(string Msg)
- {
- statusLabel.Text = Msg;
- }
private void Status(string Msg)
{
statusLabel.Text = Msg;
}
{
statusLabel.Text = Msg;
}
Then, to use the code, just simply call the method, passing it a string parameter:

- Status("Getting data...");
Status("Getting data...");
Another useful option is to use a ProgressBar. This is the bar that can be filled to show the ongoing progress of an action. To use it, add one from the Toolbox, then set the Maximum and Minimum values in the Properties Window. To set the value in code, simply change the property:

- progressBar1.Value = 56;
progressBar1.Value = 56;
Assuming the Maximum property is set to 100, the progress bar will be set to just over half full.
3. Stick with what they know
This is an easy one - don't confuse the user. Make use of well-known programs, such as Office. For example, always use Ctrl-S for saving, Ctrl-P for printing and Ctrl-F for finding. This way, a user does not need to learn a new set of shortcuts. On menus, you can easily set this using the Properties Window.
Also, copy other programs' layouts. The more you familiarise yourself with the 'common' program layouts, the more familiar your programs will seem to users. Learn that the menu goes at the top, the status bar goes at the bottom. Buttons are always aligned, text boxes are generally the same length. Important actions such as deleting and closing should always be confirmed with a simple dialog, perhaps in the form's FormClosing event. To cancel the closing of a form, use:

- e.Cancel = true;
e.Cancel = true;
The event argument 'e' contains data about the FormClosing event. The Cancel property is a boolean value - simply set it to true to stop the form from closing. A good idea is to use MessageBox.Show(), passing in the four standard parameters - the text, the title, and two enumerations describing which buttons and icon to display.
4. Test it, test it, test it again
Bugs, bugs, bugs, Every program is vulnerable to bugs, and they need to be found and eliminated ASAP. Run the program, making things extreme. Try your best to cause an error (known as an Exception), then swoop in and work out what went wrong. Then, fix the error. If you need to trap errors when the worst happens, use a try/catch block, like so:

- try
- {
- //Code goes here.
- }
- catch (Exception exc)
- {
- //If exception occurs in try section, code execution redirects here. The 'exc' variable contains data about the exception.
- }
- finally
- {
- //This code ALWAYS runs, regardless of whether or not an error occurs.
- }
try
{
//Code goes here.
}
catch (Exception exc)
{
//If exception occurs in try section, code execution redirects here. The 'exc' variable contains data about the exception.
}
finally
{
//This code ALWAYS runs, regardless of whether or not an error occurs.
}
{
//Code goes here.
}
catch (Exception exc)
{
//If exception occurs in try section, code execution redirects here. The 'exc' variable contains data about the exception.
}
finally
{
//This code ALWAYS runs, regardless of whether or not an error occurs.
}
If possible, get friends and family to test the program for you, or if you're a pro, hire a software tester. Errors don't go down well... but if they do, then they need to be able to contact you for...
5. Help!
Whether your program is complex or not, users are bound to need to contact you in some way. Always include a Help menu on your menu strip at the top, preferably with a link to a website/email. You can make it load the page automatically, using a WebBrowser control. Users can request assistance, alert you of bugs and even donate - so you get paid for all your hard work. :)
Set as favorite
Bookmark
Email This
Hits: 588
Trackback(0)
Write comment
