Section A.18. Chapter 18: Creating Windows Applications


A.18. Chapter 18: Creating Windows Applications

A.18.1. Quiz



Solution to Question 181 .

Click on the control on the form, then set the properties in the Properties window.



Solution to Question 182 .

Implement an event handler.



Solution to Question 183 .

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.



Solution to Question 184 .

A method calling itself (such as calling MethodA( ) from within the body of MethodA( ) ).



Solution to Question 185 .

XML documentation comments are preceded with three slashes , and are used to generate XML documents that can be used to document the application.



Solution to Question 186 .

Click the ShowAllFiles button on the Solution Explorer and examine the <FormName>.Designer file.

A.18.2. Exercises



Solution to Exercise 18-1 .

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"; } 

Figure A-5. Exercise 18-1: the form


Solution to Exercise 18-2 .

Create a Windows application that presents an order form that looks like Figure A-6.

Figure A-6. Exercise 18-2: order form

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 



Solution to Exercise 18-3 .

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.

  1. Create a new project named Chapter18Exercise3 .

  2. Set the form to the size of the form in Exercise 18-1 ( 196,128).

  3. Optionally copy the two controls (label and button) from the first exercise (or drag on new ones).

  4. Set the form's text to "Hello Goodbye."

  5. Drag a timer onto the form; it will appear in the tray, as shown in Figure A-7.

  6. Set the timer's Interval property to 1000 and Enabled to false.

  7. 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.

  8. 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.

Figure A-7. Adding the timer

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";       }    } } 



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

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