Click on the control on the form, then set the properties in the Properties window.
Implement an event handler.
The two ways to create an event handler are as follows:
Go to the Properties window, click on the lightning button to open the events, then fill in a name or double-click next to the event to let Visual Studio 2005 create the name.
Double-click on the control to create the default handler with a name provided by Visual Studio 2005.
A method calling itself (such as calling MethodA( )
from within the body of
MethodA( )
).
XML documentation comments are preceded with three slashes, and are used to generate XML documents that can be used to document the application.
Click the ShowAllFiles button on the Solution Explorer and
examine the <FormName>.Designer
file.
Create a Windows application that displays the word “Hello” in a label, and has a button that changes the display to “Goodbye.”
See the source code solution Chapter18Exercise1 on the web site for this book. Figure A-5 is a picture of the form.
Here is the event handler for the button:
private void button1_Click( object sender, EventArgs e ) { label1.Text = "Goodbye"; }
Create a Windows application that presents an order form that looks like Figure A-6.
This figure represents an order form that that lets the user enter information through various controls such as buttons, checkboxes, radio buttons, DateTimePicker, and so forth. You don’t need to write the back-end for the ordering system, but you can do the following:
Simulate saving to the shopping cart with a message, and reset the form when the “Add to Shopping Cart” button is clicked.
Set the minimum delivery date to be two days from now, and let the user select later dates.
For one example solution, please see the source code solution Chapter18Exercise2 available on the web site for this book.
To make this work the way I wanted, I added a bit of code to support the UI (though not to support actually recording the data). That code follows:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Chapter18Exercise2 { public partial class OrderForm : Form { public OrderForm( ) { InitializeComponent( ); ResetValues( ); ResetDeliveryDate( ); } // When you click the Add button we reset for next choice // and we do the work of adding the current order to the cart private void btnAdd_Click( object sender, EventArgs e ) { ResetValues( ); SaveToShoppingCart( ); } // set all the controls to their starting values private void ResetValues( ) { this.ddlBooks.SelectedIndex = -1; this.ddlBooks.Text = "Please pick a book"; this.numCopies.Value = 1; this.rbHardcover.Checked = false; this.rbLibrary.Checked = false; this.rbPaperback.Checked = true; this.cbConfirm.Checked = false; this.cbGiftWrap.Checked = false; this.cbRush.Checked = false; } // broken out in case we need to set this separately private void ResetDeliveryDate( ) { // minimum delivery date is 2 days from now this.deliveryDate.MinDate = DateTime.Now + new TimeSpan( 2, 0, 0, 0 ); // initialize the control to the MinDate this.deliveryDate.Value = this.deliveryDate.MinDate; } // handle work to save to shopping cart and update status private void SaveToShoppingCart( ) { // work here to record choices this.lblStatus.Text = "Saved to Shopping Cart."; } // hack to allow touching any control other than button // to turn off the status (to avoid confusion) private void UniversalEventHandler( object sender, EventArgs e ) { this.lblStatus.Text = string.Empty; } } // end class } // end namespace
Modify the first exercise by dragging a timer (found in the Components section of the Toolbox) onto the form and having the timer change the message from “Hello” to “Goodbye” and back once per second. Change the button to turn this behavior on and off. Use the Microsoft Help files to figure out how to use the timer to accomplish this exercise.
Create a new project named Chapter18Exercise3.
Set the form to the size of the form in Exercise 18-1 ( 196,128).
Optionally copy the two controls (label and button) from the first exercise (or drag on new ones).
Set the form’s text to “Hello Goodbye.”
Drag a timer onto the form; it will appear in the tray, as shown in Figure A-7.
Set the timer’s Interval property to 1000 and Enabled to false.
Double-click on the timer to create the timer1_Tick event handler that will fire every 1,000 milliseconds (every 1 second). It will alternate the text in the label by checking (and changing) the Boolean value isHello.
Change the button event handler to test if the timer is running (if so, its IsEnabled property is true) to start the timer and set the button’s text to stop, or to stop the timer and set the button’s text to start.
The source code for this example is presented in Example A-1.
Example A-1. Using a timer to switch the message every second
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Chapter18Exercise3 { public partial class Form1 : Form { bool isHello = true; public Form1( ) { InitializeComponent( ); } private void button1_Click( object sender, EventArgs e ) { if ( !timer1.Enabled ) { timer1.Start( ); this.button1.Text = "Stop"; } else { timer1.Stop( ); this.button1.Text = "Start"; } } private void timer1_Tick( object sender, EventArgs e ) { isHello = !isHello; if ( isHello ) label1.Text = "Hello"; else label1.Text = "Goodbye"; } } }