Windows Forms 2.0 Programming (Microsoft .NET Development Series)

After a component is dropped onto a form, you can use it just like any regular class. For example, imagine that you'd like users to be able to set an alarm in your application and be notified when it goes off. You can easily implement this functionality with a Timer component, from the System.Windows.Forms namespace.[1] When a Timer is dropped onto a form, the Windows Forms Designer generates the following code to InitializeComponent:

[1] Several timers are available from the .NET Framework. When and how to use them are discussed at http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/default.aspx (http://tinysells.com/15).

// AlarmForm.Designer.cs partial class AlarmForm { ... /// <summary> /// Required designer variable. /// </summary> System.ComponentModel.IContainer components = null; Timer timer; ... void InitializeComponent() { ... this.components = new System.ComponentModel.Container(); this.timer = new Timer(this.components); ... } } // AlarmForm.cs partial class AlarmForm : Form { public AlarmForm() { InitializeComponent(); } }

When a new component is created, it's placed in a containermanaged list with the other components on the form. This allows forms to keep track of hosted components and to provide automatic resource management.

Because the Windows Forms Designer takes care of these issues behind the scenes, you need only write the code to use the component, as with a normal class. For a timer component, this might involve setting its Enabled and Interval properties and handling its Tick event:

// AlarmForm.cs partial class AlarmForm : Form { public AlarmForm() { InitializeComponent(); // Configure the Timer component this.timer.Enabled = true; this.timer.Interval = 1000; this.timer.Tick += this.timer_Tick; } void timer_Tick(object sender, EventArgs e) {...} }

The real beauty of using components is their integration with the Windows Forms Designer, which allows you to configure them declaratively. Making sure the desired component is selected, in this case the Timer, you set properties and register event handlers using the Properties window, as shown in Figure 9.3.

Figure 9.3. Declaratively Configuring a Component with the Properties Window

As properties are set and events are registered, the Windows Forms Designer generates the necessary code to reflect your configuration requirements, and, as you have probably come to expect by now, the result looks remarkably similar to what you would write yourself:

// AlarmForm.Designer.cs partial class AlarmForm { ... void InitializeComponent() { ... // timer this.timer.Enabled = true; this.timer.Interval = 1000; this.timer.Tick += this.timer_Tick; ... } } // AlarmForm.cs partial class AlarmForm : Form { public AlarmForm() { InitializeComponent(); } void timer_Tick(object sender, EventArgs e) {...} }

In our alarm example, the Windows Forms Designer has generated most of the Timerrelated code for us, so we implement the rest of the alarm functionality for our form:

// AlarmForm.cs partial class AlarmForm : Form { ... DateTime alarm = DateTime.MaxValue; // No alarm void setAlarmButton_Click(object sender, EventArgs e) { this.alarm = this.dateTimePicker.Value; } void timer_Tick(object sender, EventArgs e) { // Check to see whether we're within 1 second of the alarm double seconds = (DateTime.Now this.alarm).TotalSeconds; if( (seconds >= 0) && (seconds <= 1) ) { this.alarm = DateTime.MaxValue; // Show alarm only once MessageBox.Show("Wake Up!"); } } }

Here, we use DateTimePicker to allow users to specify an alarm time. When the timer goes off every second, we check to see whether we're within one second of the alarm time. If we are, we turn off the alarm and notify the user, as shown in Figure 9.4.

Figure 9.4. The Alarm Code Responding to the Timer Component's Tick Event

To help you write less code in a wide variety of scenarios, Windows Forms implements a multitude of intrinsic components. Appendix D: Component and Control Survey outlines the standard Windows Forms components and refers you to locations in the book where they are covered in more detail.

Категории