Library sample chapters
Introduction to Windows Forms
- Introduction
- The Hello World Application
- Creating & Using an Event Handler
- Defining the Border Style
- Adding a Menu
- Handling Menu Events
- Menu Layout
- Context Menus
- Replacing, Cloning & Merging
- Sub-Menus
- Summary
Creating & Using an Event Handler
If you're familiar with Windows C development, you will understand the idea of handling messages as they arrive by the action of the WndProc switch statement. If you have been working with MFC, you will know that messages are routed through the message map of your application. Under .NET the message routing system is provided by delegates. Your application will define event handlers that are called through a delegate whenever a message arrives. Button clicks for example, can be handled by using a generic EventHandler delegate. Other types of events, such as mouse events or timer events, are serviced through specific handlers that have their own delegate signatures.
Adding a button to the Windows Forms application is simple. You can add a button member to the class, initialize it in the constructor, and place it in the controls collection.
When this is done, you can wire up an event that is trapped whenever the button is clicked. Events sent through the system are hooked to event handlers of a specific signature by a multicast delegate that is declared especially for this purpose. Your event handlers must all have the following method signature:
void EventMethodName(Object sender, EventArgs e);
Event handlers are added to the control's event sources with the += operator and removed with the -= operator, so events can be modified dynamically at runtime. Because events are multicast, you can add more than one handler at a time to the same event.
Listing 3.1.3 shows the modified file HWFButton.cs.
Listing 3.1.3 HWFButton.cs: Hello Windows Forms with a Simple Button Handler
// HWFButton.cs
namespace HelloWindowsFormsNamespace {
using System;
using System.Drawing;
using System.ComponentModel;
using System.WinForms;
public class HelloWindowsForms : System.WinForms.Form
{
//Label 1 displays the text message "Hello Windows Forms!"
private System.WinForms.Label label1;
//Adding a button member allows us to place a button on the panel.
private System.WinForms.Button button1;
//The constructor is where all initialization happens.
//Forms created with the designer have an InitializeComponent() method.
public HelloWindowsForms()
{
this.label1 = new System.WinForms.Label();
label1.Location = new System.Drawing.Point(8, 8);
label1.Text = "Hello Windows Forms!";
label1.Size = new System.Drawing.Size(408, 48);
label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 24f);
label1.TabIndex = 0;
label1.TextAlign = System.WinForms.HorizontalAlignment.Center;
this.button1=new System.WinForms.Button();
button1.Location=new System.Drawing.Point(8,58);
button1.Size = new System.Drawing.Size(408,25);
button1.Text = "Click Me!";
button1.TabIndex = 1;
button1.Click += new EventHandler(OnButton1Clicked);
this.Text = "Hello World";
this.MaximizeBox = false;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BorderStyle = System.WinForms.FormBorderStyle.FixedDialog;
this.MinimizeBox = false;
this.ClientSize = new System.Drawing.Size(426, 85);
this.Controls.Add(label1);
this.Controls.Add(button1);
}
void OnButton1Clicked(Object sender,EventArgs e)
{
if(this.label1.Text == "Hello Windows Forms!")
this.label1.Text = "You Clicked?";
else
this.label1.Text = "Hello Windows Forms!";
}
// This main function instantiates a new form and runs it.
public static void Main(string[] args)
{
Application.Run(new HelloWindowsForms());
}
}
} // end of namespace
Type in Listing 3.1.3 and build it with the build batch file. Running the sample will show a window that has the same message and a clickable button. The button handler simply swaps the text in the label control to verify that the handler worked.
|
button1.Size = new System.Drawing.Size(408,25); This longhand form is not always necessary. You could abbreviate the line above to Layout tools will always give you the longhand version because they never get tired of typing.button1.Size = new Size(408,25); Generally, if the object you're creating is in a namespace you declared you were using, you can use the shorthand. |
When you run the file, you will see the application shown in Figure 3.1.2.
Figure 3.1.2
Using a simple Click Handler.
Related articles
Related discussion
-
C# video Editing/rendering
by pkuchaliya (0 replies)
-
How to Fill DataSet with more records (around 1 lakh) in a faster way
by Jayaram P (0 replies)
-
Can't print on the network with MSADESS ??
by anatha1 (2 replies)
-
Very Urgent regarding deleting the images from a folder
by Nanosteps (6 replies)
-
DataGridViewComboBoxColumn not showing values
by sachinkalse (0 replies)
Actually all you have to do in place an ampersand before the letter you want underlined.
So if you want the "r" in "Print" underlined, enter in the code:
"P&rint"
That's it.
if you want to view the _ all the time for alt shortcuts you have to go to:
display properties->appearance->effects
and uncheck the "Hide underlined charactors for keyboard navigation until I press the ALT key" checkbox.
these instructions are for XP SP2 Pro build 2600
The "" on the short-cut character is visible only when "ALT" is pressed. Is it possible to display the "" programatically???
This thread is for discussions of Introduction to Windows Forms.