The Button Control


When you think of a button, you are probably thinking of a rectangular button that can be clicked to perform some task. However, the .NET Framework provides a class derived from ControlSystem.Windows.Forms.ButtonBase — that implements the basic functionality needed in button controls, so any programmer can derive from this class and create his or her custom button controls.

The System.Windows.Forms namespace provides three controls that derive from ButtonBaseButton, CheckBox, and RadioButton. This section focuses on the Button control (which is the standard, well-known rectangular button), and I'll cover the other two later in this chapter.

The Button control exists on just about any Windows dialog you can think of. A button is primarily used to perform three kinds of tasks:

  • To close a dialog with a state (for example, OK and Cancel buttons)

  • To perform an action on data entered on a dialog (for example clicking Search after entering some search criteria)

  • To open another dialog or application (for example, Help buttons)

Working with the button control is very straightforward. It usually consists of adding the control to your form and double-clicking it to add the code to the Click event, which will probably be enough for most applications you work on.

Let's look at some of the commonly used properties and events of the control. This will give you an idea what can be done with it. After that, you create a small example that demonstrates some of the basic properties and events of a button.

Button Properties

The following table lists the most commonly used properties of the Button class, even if technically they are defined in the ButtonBase base class. Only the most commonly used properties are explained here. Please refer to the .NET Framework SDK documentation for a complete listing.

Name

Description

FlatStyle

The style of the button can be changed with this property. If you set the style to Popup, the button will appear flat until the user moves the mouse pointer over it. When that happens, the button pops up to a 3-D look.

Enabled

I'll mention this here even though it is derived from Control, because it's a very important property for a button. Setting the Enabled property to false means that the button becomes grayed out and nothing happens when you click it.

Image

Allows you to specify an image (bitmap, icon, and so on) that will be displayed on the button.

ImageAlign

With this property, you can set where the image on the button appears.

Button Events

By far the most used event of a button is the Click event. This event happens whenever a user clicks the button, which means pressing the left mouse button and releasing it while the pointer is over the button. This means that if you left-click the button and then draw the mouse away from the button before releasing it, the Click event will not be raised. Also, the Click event is raised when the button has focus and the user presses the Enter key. If you have a button on a form, you should always handle this event.

In the following Try It Out, you create a dialog with three buttons. Two of the buttons will change the language used from English to Danish and back. (Feel free to use whatever language you prefer.) The last button closes the dialog.

Try It Out – ButtonTest

image from book

Follow these steps to create a small Windows application that uses three buttons to change the text in the caption of the dialog:

  1. Create a new Windows application called ButtonTest in the directory C:\BegVCSharp\ Chapter14.

  2. Pin the Toolbox down by clicking the pin icon next to the x in the top-right corner of the window, and double-click the Button control three times. Then move the buttons and resize the form as shown in Figure 14-6.

    image from book Figure 14-6

  3. Right-click a button and select Properties. Then change the name of each button as indicated in the picture above by selecting the (Name) edit field in the Properties window and typing the relevant text.

  4. Change the Text property of each button to be the same as the name, but omit the button prefix for the Text property value.

  5. You want to display a flag in front of the text to make it clear what you are talking about. Select the English button and find the Image property. Click (...) to the right of it to bring up a dialog where you can add images to the resource file of the form. Click the Import button and browse to the icons. The icons you want to display are included in the project ButtonTest you can download from the Wrox homepage. Select the icon flguk.ico and flgDen.ico.

  6. Select FLGUK and click OK. Then select buttonDanish, click the (. . .) on the Image property and choose FLGDK before clicking OK.

  7. You notice at this point that the button text and icon are placed on top of each other, so you need to change the alignment of the icon. For both the English and Danish buttons, change the ImageAlign property to MiddleLeft.

  8. At this point, you may want to adjust the width of the buttons so that the text doesn't start right where the images end. Do this by selecting each of the buttons and pulling the notch on the right edge of the button.

  9. Finally, click on the form and change the Text property to Do you speak English?

image from book

That's it for the user interface of your dialog. You should now have something that looks like Figure 14-7.

image from book Figure 14-7

Now you are ready to add the event handlers to the dialog. Double-click the English button. This will take you directly to the event handler for the control's default event — the Click event is the default event for the button and so that is the handler created.

Adding the Event Handlers

Double-click the English button and add the following code to the event handler:

private void buttonEnglish_Click(object sender, EventArgs e) { this.Text = "Do you speak English?"; } 

When Visual Studio creates a method to handle such an event, the method name is a concatenation of the name of the control, followed by an underscore and the name of the event that is handled.

For the Click event, the first parameter, object sender, will hold the control that was clicked. In this example, this will always be the control indicated by the name of the method, but in other cases many controls may use the same method to handle an event, and in those cases you can find out exactly which control is calling by checking this value. The text box example later in this chapter demonstrates how to use a single method for multiple controls. The other parameter, System.EventArgs e, holds information about what actually happened. In this case, you won't need any of that information.

Return to the design view and double-click the Danish button, and you will be taken to the event handler for that button. Here is the code:

private void buttonDanish_Click(object sender, EventArgs e) { this.Text = "Taler du dansk?"; }

This method is identical to btnEnglish_Click, except that the text is in Danish. Finally, you add the event handler for the OK button in the same way as you've done twice now. The code is a little different though:

private void buttonOK_Click(object sender, EventArgs e) { Application.Exit(); }

With this you exit the application and, with it, this first example. Compile it, run it and press a few of the buttons. You will get output similar to what is displayed in Figure 14-8.

image from book
Figure 14-8




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

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