Create Command Controls
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| Exam 70-526 Preparation Guide: Create and configure command controls on a Windows Form |
Contents |
A command control is one that requires something to happen when you click on it. In Visual Basic versions before the arrival of .NET, the command control was a button. Although other controls can require things to happen when you click on them, it is only the button that must have something happen when you click on it or why else use one.
The Button control
The Button control allows the user to click it to perform an action. When the button is clicked, it gives a visual cue that something is happening.
Inheritance hierarchy
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ButtonBase
System.Windows.Forms.Button
Useful properties
-
AutoEllipsis- Indicates whether the ellipsis character (...) appears at the right edge of the control. This denotes that the control text extends beyond the specified length of the control.
-
-
DialogResult- Value that is returned to the parent form when the button is clicked. This is normally set toDialogResult.None, howeverDialogResult.OKorDialogResult.Cancelare useful values (see below).
-
-
FlatAppearance- Defines how the button looks and behaves whenFlatStyleis set toFlat. Implements theFlatButtonAppearanceclass with the following properties:
-
-
BorderColor- Gets or sets the colour of the border around the button. -
BorderSize- Gets or sets a value that specifies the size, in pixels, of the border around the button. -
MouseDownBackColor- Gets or sets the colour of the client area of the button when the mouse is pressed within the bounds of the control. -
MouseOverBackColor- Gets or sets the colour of the client area of the button when the mouse pointer is within the bounds of the control.
-
-
FlatStyle- Indicates the flat style appearance of the button control. Takes a value from theFlatStyleenumeration:
-
-
Flat- The control appears flat until the mouse pointer moves over it, at which point it becomes highlighted. -
Popup- A control appears flat until the mouse pointer moves over it, at which point it appears three-dimensional. -
Standard- The control appears three-dimensional (default). -
System- The appearance of the control is determined by the user's operating system.
-
-
Image- Indicates the image that is displayed on the button control.
-
-
ImageAlign- Indicates the alignment of the image on the button control. Takes a value from theContentAlignmentenumeration:
-
-
BottomCenter- Content is vertically aligned at the bottom, and horizontally aligned at the center. -
BottomLeft- Content is vertically aligned at the bottom, and horizontally aligned on the left. -
BottomRight- Content is vertically aligned at the bottom, and horizontally aligned on the right. -
MiddleCenter- Content is vertically aligned in the middle, and horizontally aligned at the center (default). -
MiddleLeft- Content is vertically aligned in the middle, and horizontally aligned on the left. -
MiddleRight- Content is vertically aligned in the middle, and horizontally aligned on the right. -
TopCenter- Content is vertically aligned at the top, and horizontally aligned at the center. -
TopLeft- Content is vertically aligned at the top, and horizontally aligned on the left. -
TopRight- Content is vertically aligned at the top, and horizontally aligned on the right.
-
-
Text- Value of the text that appears on the button control.
-
-
TextAlign- Indicates the alignment of the text on the button control. Takes a value from theContentAlignmentenumeration (see above).
-
-
TextImageRelation- Indicates the position of text and image relative to each other. Takes a value from theTextImageRelationenumeration:
-
-
ImageAboveText- Specifies that the image is displayed vertically above the text of a control. -
ImageBeforeText- Specifies that the image is displayed horizontally before the text of a control. -
Overlay- Specifies that the image and text share the same space on a control (default). -
TextAboveImage- Specifies that the text is displayed vertically above the image of a control. -
TextBeforeImage- Specifies that the text is displayed horizontally before the image of a control.
-
The Click event
The Click event responds to either the left mouse button being clicked whilst hovering over the button, or the ENTER key being pressed when the button has focus. This event passes an EventArgs parameter to its event handler, so it only indicates that a click has occurred. An event handler for the event can be added in the following way.
In the Designer Window, double-click on the button that you wish to handle the Click event for. The IDE causes two things to happen in the code behind the form. First of all, a subscription to the event is created in the InitializeComponent() method:
this.button1.Click += new System.EventHandler(this.button1_Click);
The second thing that happens is that the event handler itself is added:
private void button1_Click(object sender, EventArgs e) { this.Text = "Hello"; }
The method name that is created by the IDE is a concatenation of the name of the control, an underscore and the name of the event that is being handled. The EventArgs parameter holds information about what occurred.
Events are described in more depth in Create event handlers for Windows Forms and controls.
Determining clicks
Another event that often needs to be responded to is the MouseDown event. This event occurs whenever any of the mouse buttons are clicked. To determine which button has been pressed the MouseEventArgs parameter can be interrogated.
The MouseEventArgs class has the following Public properties:
-
Button- Gets which mouse button was pressed. Takes a value from theMouseButtonsenumeration:
-
-
Left- The left mouse button was pressed. -
Middle- The middle mouse button was pressed. -
None- No mouse button was pressed. -
Right- The right mouse button was pressed. -
XButton1- The first XButton was pressed. -
XButton2- The second XButton was pressed.
-
Note: - With Windows 2000, Microsoft introduced support for the Microsoft IntelliMouse Explorer, which is a mouse with five buttons. The two new mouse buttons (XBUTTON1 and XBUTTON2) provide backward/forward navigation.
-
Clicks- Gets the number of times the mouse button was pressed and released.
-
-
Delta- Gets a signed count of the number of detents the mouse wheel has rotated. A detent is one notch of the mouse wheel.
-
-
Location- Gets the location of the mouse during the generating mouse event.
-
-
X- Gets the x-coordinate of the mouse during the generating mouse event.
-
-
Y- Gets the y-coordinate of the mouse during the generating mouse event.
-
Determining which Button was pressed
Using the values exposed in the Button property, the button that was pressed can be determined.
- Select the button in the Design window.
- In the properties window, click on the lightning bolt, to bring up the events window.
- Double click in the cell next to the
MouseDownevent. The IDE will generate the skeleton code to handle this event. - In the event handler, add the following code:
private void button1_MouseDown(object sender, MouseEventArgs e) { string eventString = null; switch (e.Button) { case MouseButtons.Left: eventString = "L"; break; case MouseButtons.Right: eventString = "R"; break; case MouseButtons.Middle: eventString = "M"; break; case MouseButtons.XButton1: eventString = "X1"; break; case MouseButtons.XButton2: eventString = "X2"; break; case MouseButtons.None: default: break; } }
Handling DialogResult
The DialogResult property value that is returned to the parent form when the button is clicked can be used to determine when a form is closing and how it had been closed. This can be seen in action with a demonstration:
- Open a new windows application. Drag a button and a label onto the form.
- Set the text of the button to "Click Me" and the text of the label to "".
- Add a second form to the application named NameFrm.
- Set up the second form as per the following.
- Make the text box
Public. (Keeps things simple, but this is a bad practice). - Set the
DialogResultproperty of theOKbutton toOK. - Set the
DialogResultproperty of the Cancel button toCancel. - Add the following code to the button event handler of the first form.
private void button1_Click(object sender, EventArgs e) { NameFrm aName = new NameFrm(); if (aName.ShowDialog() == DialogResult.OK) { this.label1.Text = "Hello " + aName.textBox1.Text; } }
The label only changes if the OK button is pressed. - Run the application.
MSDN references
|


