Monday, March 28, 2005

Developing Windows Applications - II

Did you go through previous article in a careful manner? Didn't you miss anything there? Yes! We didn't talk about toolbar and status bar in our previous article. Here we will have a look into toolbar and status bar plus validation controls and other tips about windows forms.

Toolbar is another important aspect in Windows application. It's a rare instance when an application doesn't have toolbar with it. ToolBar contains the collection of ToolBarButton. It's the collection of ToolBarButton that makes visual representation of toolbar. To add a toolbar to your application go to View ' Toolbox. Select ToolBar control from toolbox and drop it on the form. This action makes a blank strip at top edge of your form. That is the toolbar. Now you got a question. How to add buttons to this blank strip? Simple; Right click on this strip or now onwards a toolbar. Select Buttons property and you will get another window something like given below:


Now you are free to add as many toolbar buttons as you want. Give proper values to Name, Tag, Text and ToolTipText property. Notice Style and ImageIndex properties. These are two crucial properties for toolbar. Style property is used to set the button of proper type. Whether you want toggle button in toolbar or should it be a separator. ImageIndex will be useful to assign a proper image to button of toolbar.

To have images in our toolbar we have to use another control, ImageList. ImageList provides methods to manage a collection of Images objects. ImageList is typically used by other controls, such as the ListView, TreeView, or ToolBar. You can add bitmaps, icons files to the ImageList, and the other controls are able to use the images as they require. To add ImageList to your form drag it from toolbox to your form. As it is not a visual component, it will sit in component tray. To assign images to ImageList, select its Images property. This property will let you visually add images to ImageList. Once you are done, come back to property window for toolbar. Pull down the drop down menu of ImageList property and select the appropriate value. You should have it as the name you gave to your ImageList component.

Now we come to our original topic. We were supposed to add images to our toolbar buttons. Here are the steps to do that:
1. We should have ImageList in our Form. We do have now!
2. Add images into this ImageList component. We have done that!
3. Assign ImageList property of toolbar as the ImageList component we have added. Done!
4. Click on Buttons property of toolbar and now you will have appropriate values in ImageIndex property as shown below:


Once your toolbar is visually ready, double click on this. You will get an empty event handler, where you will have to identify that which was the clicked button. That we have to do based on its index in following manner:

private voidtoolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)

switch (toolBar.Buttons.IndexOf(e.Button))
case 0:
//Your code goes here

Status Bar
Status bar is used to inform user about certain kind of activities or some static data such as current date etc. To add status bar to your form, drag and drop StatusBar control from toolbox to your form. It will be bottom docked to your form by default. StatusBar has a property panels that is collection of StatusBarPanel.This makes visual representation of status bar and we have to assign text values to these panels. To get your status bar up, follow given below steps:
1. Drag a status bar to your form.
2. Go to property window and selects Panels property.
3. Add panels to your status bar in a similar manner as you added toolbar buttons to toolbar.
4. As a final step, give some proper value to each panel when your form loads.

One little issue is here. To show your panels make sure you have ShowPanels property is set to true. By default this is set to false, I dont know why. You can set values of panels as given below:
statusBar.Panels[0].Text = DateTime.Now.ToShortDateString();
statusBar.Panels[1].Icon = SystemIcons.Information;

At this point we have decorated our application to look good enough. Following is the descriptions about some Form properties through which you can make you application behave better too.

This is the property, when you want to accomplish some task when user hits Enter key. You will have all buttons those are part of your form listed in this property. Select one button from the list, you would like to act when user hits Enter key.

Button selected under this property works when users hits Esc key.

You can track the keyboard on KeyDown, KeyPress and KeyUp events. But these events will be fired only when your form or particular control connected to these events is having focus.If any other control on your form is having focus and you are tracking a key on form level, you will never be notified. Should you ever get into situation like this? You set KeyPreview property as true and now you can track entire keyboard at form level. Form will be the first control to receive all the messages coming from keyboard. Once Form does the processing, then only message will be passed to other controls.

Do you ever get into situation where you have to retain a minimum size of form? User should not be able to resize the form once she reaches the given points? In such situation, use this property. Assign it the minimum size of form that you would like to retain.

Have you ever dreamt to make your form semi-transparent? Then your dream has come true. By default this property is set at 100%. That means your form will be opaque. Just play with this property to adjust opacity of your form.One caution: In property window this is treated in percentage. If you happen to set it through code part, you have to set it in following manner:
//To Set opacity at 50%
this.Opacity = 0.5;

While covering these aspects of windows application, I happen not to cover some simple controls like TextBox, Label etc. I leave these controls and other windows controls on you to explore further. You will find your journey really exciting to explore these controls. In next article we will see how can we validate user input?

No comments: