Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

As requested I've included the bugging exercises for the chapter. The program should also have a pick again button as well. You Do It Adding

As requested I've included the bugging exercises for the chapter. The program should also have a pick again button as well.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

You Do It Adding Labels to a Form and Changing Their Properties In the next steps, you begin to create an application for Bailey's Bed and Breakfast. See Figure 12-9.) The main Form allows the user to select one of two suites and discover the amenities and price associated with each choice. You will start by placing two Labels on a Form and setting several of their properties. (continues Adding Color to a Form en images in the next steps represent a tyoical Visual Sion, your scr on of Visual Studio you are using and the options selected during your r (continued) environment. Based on the lected during your installation, your screen Open Microsoft Visual Studio, and then select New, Project, and Windows e botom of the New Project window, click in the Name text box, and replace the default name with BedAndBreakfast. Make sure that the Location tield contains the folder where you want to store the project. See Figure 12.9 eld . ecent NET Framework 452 Sen , - Visual C wroom formi Agen- Windows Forms App selected Shared Proiert Other Project Types Stared Project Sampies Project Name entered ot tnsing mhat you are Icoiing for? Open visasl Studho Insaler Location selected Unit Test App Universal Click OK to create project 121 Lockion Solubiot 5okudion Name Create new solution Create directory for on Or Cancel Figure 12-9 The New Project window for the BedAndBreakfast program Click OK. The design screen opens. The blank Form in the center of the screen has an empty title bar. Click the Form. The lower-right corner of the screen contains a Properties window that lists the Form's properties. (If you do not see the Properties window, you can click View on the menu bar and then click Properties Window.) In the Properties list, click the Name property and change the Name of the Form to BaileysForm. Click the Text property and change it to Bailey's Bed and Breakfast. 2. (continues) CHAPTER 12 Using Controls a From the Toolbox at the left of the screen, drag a Label onto t Change the Name of labe11 to welcomeLabel and change the Tos to Welcome to Bailey's. Drag and resize the Label so it is close to tn position of the Labe1 in Figure 12-10 o the Form 556 H you do not see the Toolbox, click the Toolbox tab at the left side of the screen and n screen by clicking the pushoin. Alternatively, you can select View from the man bar Toolbox. s' Bailey's Bed and Welcome to Bailey's Figure 12-10 A Label placed on the Form in the BedAndBreakfast project 4. Locate the Font property in the Properties list. Currently, it lists the default font as Microsoft Sans Serif, 7.8 pt. Notice the ellipsis (three dots) at the right of the Font property name. (You might have to click in the Property to see the button.) Click the ellipsis to display the Font dialog box. Make selections to change the font to Microsoft Sans Serif, 18 point, and Bold. Click OK After you enlarge the Font for the Label, the Font appears larger. Drag the edge of the Form so the new, enlarged Label fits 5. Drag a second Label onto the Form beneath the first one, and then set ts Name property to ratelabel and its Text property to Check our rates. Change the Font for the new Label to Microsoft Sans Serif, 16 point Regular, and reposition the second Labe1 so it is approximately centered under the first one. (continues Adding Color to a Form save the project, click Debug on Debugging, or use the shor on the menu har 6. Save the project, clio F or use the shortcut keys listed in the menu. the menu bar r, and click Start Without he Form appears. ppear 557 2Bailey's Bed and Breakfast Welcome to Bailey's Check our rates Figure 12-11 The BedAndBreakfast Form with two Labels 7. Dismiss the Form by clicking the Close button in its upper-right corner. Examining the Code Generated by the IDE n the next steps, you examine the code generated by the IDE for the following reasons You will recognize many of the C# statements from what you have already learned in this book. To gain an understanding of the types of statements created by the IDE To lose any intimidation you might have about the code that is generated. (continues) 1. In the Solution Explorer at the right side of the screen Form1.Designer.cs. Scroll down if necessary until you see the that contains Windows Form designer generated code, and eve exDandat see only a few statements, you can drag the bottom and sid code window to expose more of the code, or you can sc should be able to view statements that assign valuesto components that you dragged into the Form, similar to the she in Figure 12-12. The numbers used in some of your statemewts shown those seen in Figure 1212 depending on the exact plaentofry from soexand it. If you can expand it. If y borders of the the properties Labels private void InitializeComponent) this. welcomeLabelnew System.Windows. Forms. Labe this.rateLabel- new System.Windows. Forms. LabelO this.SuspendLayoutO O: // welcomeLabel this.welcomeLabel.AutoSize true; this.welcomeLabel.Location new System.Drawing. Point (88, 100) this.welcomeLabe l.Marginnew System.Windows . Forms. Padding(5, 0, 5, 0) this.welcomeLabel.Name"welcomeLabel" this.welcomeLabel.Size new System.Drawing.Size(300, 36); this.welcomeLabel.TabIndex0 this.welcomeLabel Text "Welcome to Bailey 's"; // rateLabel this.rateLabel.Autosize true this.ratelabe1.Font new System.Drawing.Font ("Microsoft Sans Serif" 16.2F, System. Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte) (0))): this.rateLabel.Locationnew System.Drawing.Point(144, 185); this.rateLabel. Name "rateLabel"; this.rateLabel.Size new System.Drawing.Size(181, 29): this.rateLabel.TabIndex1 this.rateLabe1.Text-"Check our rates" Figure 12-12 Part of the Formi.Designer.cs code (continue CHAPTER 12 u Using Controls Using Other Controls If you examine the Visual Studio IDE or search through the Visual Studio documentation, you will find many other Controls that are not covered in this c the menu bar and click Add New Item, you can add extra Forms, Files, Controls, and other hapter. If you click Projects project that adds new controls of your own. Still a 80 elements to your project. (In the next You Do It" section, you create a pr Forms that appear after selections are made from a primary Form.)N will be developed in the future, and you might even design nevw w controls and contairier ls will contain properties and methods and your solid foundation in C# will prepare to use new controls effectively You Do It Adding CheckBoxes to a Form In the next steps, you add two CheckBoxes to the BedAndBreakfast Form. These controls allow the user to select an available room and view information about it 1. Open the BedAndBreakfast project in Visual Studio, if it is not still open on your screen. 2. In the Design view of the BedAndBreakfast project in the Visual Studio IDE, drag a CheckBox onto the Form below the Check our rates Label. (See Figure 12-35 for its approximate placement.) Change the Text property of the CheckBox to Belle Aire Suite. Change the Name of the property to belleAireCheckBox. Drag a second CheckBox onto the Form beneath the first one. Change its Text property to Lincoln Room and its Name to lincolnCheckBox. Change the Font property for both CheckBoxes to Microsoft Sans Serif Bold 14 (continues) (continued) Dalley* Bed and Dreakfast Welcome to Bailey's Check our rates a Belle Aire Suite Lincoln Room The BedAndBreakfast Form with two Figure 12-35 CheckBoxes 3. Next, you will create two new Forms: one that appears when the user selects the BelleAire CheckBox and one that appears when the user selects the Lincoln CheckBox. Click Project on the menu bar, and then click Add New Item. In the Add New ltem window, click Windows Form. In the Name text box at the bottom of the window, type BelleAireForm. See Figure 12-36 (continues) (continued And New It DedhndBreaifast Type: Visasl Carem A blank Windows Form Sort ty Delault Visual Ctem Code Oata General O Interface Add New Item window EWindows FamC tem p web Windows Form Windows Forms User ComtretVisual Ce terms Name of Form Graphics Componem ClassVis Citems + Online UHer contrel visal C ten Add buttorn C " items About Rox ADONE Data -Viwal(2 Items BelleAireformcs Ad Cance Figure 12-36 The Add New Item window 4. Click the Add button. A new Form is added to the project, and its Name and Text (tle bar) properties contain BelleAireform. Save the project (and continue to do so periodically) 5. Change the BackColor property of the Form to Yellow to match the color of BaileysForm. 6. Drag a Label onto the Form. Change the Name of the Label to bel1eAireDescriptionLabel. Change the Text property of the Label to contain the following: The BelleAire suite has two bedrooms, two baths, and a private balcony. Click the arrow on the text property to type the long label message on two lines. Drag a second Label onto the Form, name it belleAirePricelabel, and type the price as the Text property: $199 per night. Adjust the sizes and positions of the Labels to resemble Figure 12-37 (continues) Using Other Controls BelleAireForm (continued) wo baths, and a private balcodroms 583 $199.99 per night Figure 12-37 The BelleAireForm with two Labels 1. Click the Forml.cs[Design] tab at the top of the Designer screern to view the original Bailey's Bed and Breakfast Form. Double-click the BelleAire Suite CheckBox. The program code (the method shell for the default event of a CheckBox) appears in the IDE main window. Rename the belleAireCheckBox CheckedChanged) method to start with an uppercase etter, and within the method, add an if statement that determines whether the BelleAire CheckBox is checked. If it is checked, create a new instance of BelleAireForm and display it. private void BelleAireCheckBox_CheckedChanged Cobject sender, EventArgs e) if (belleAfreCheckBox. Checked) BelleAireForm belleAireForm new BelleAireFormO; belleAireForm.ShowDialogO; When a new Form (or other windows class) is instantiated, it is not visible by default. ShowDialogO shows the window and disables all other windows in application. The user must dismiss the new Form before proceeding. A secondary window that takes control of a program is a modal window; the user must deal with this window before proceeding. (continues) CHAPTER 12U PTER 12 Using Controls continued 8. Save and then execute the program by selecting Debug from the menu bar and then select Start Without Debugging. The main edereanlast fors appears. Click the BelleAire Suite CheckBox. The Dismiss the Form. Click the Lincoln Room CheckBox 84 . Nothing happens because you have not yet written event code for this Che kBox. When you uncheck and then check the BelleAire Suite CheckBox again, the BelleAire form reappears. Dismiss the BelleAire Form. 9. In the IDE adjust the size, position. or font of any controls if you think your adjustments would improve the program's appearance, and then test the program again 10. When you dismiss the BelleAire Form, the BelleAire CheckBox on the Bed and Breakfast Form remains checked. To see it appear as unchecked after its Form is dismissed, dismiss the program's main form (Bailey's Bed and Breakfast) to end the program. Then, add a third statement within the if block in the CheckedChanged) message as follows belleAireCheckBox.Checked false; That way, whenever the CheckedChanged method executes because the belleAireCheckBox was checked, it will become unchecked. Save the project, and then execute it again. When you select the BelleAire CheckBox, view the Form, and dismiss it, the CheckBox appears unchecked and is ready to check again. Dismiss the BedAndBreakfast Form 11. Click Project on the menu bar, and then click Add New Item. Click Windows Form, and enter its Name: LincolnForm. Click the Add button, and when the new Form appears, its Name and Text properties will have been set to LincolnForm. Change the Text property to Lincoln Room. Then add two Labels to the Form, and provide appropriate Name properties for them Change the Text on the first Label to Return to the 1850s in this lovely room with private bath. The second should be $110 per night. Change the Form's BackColor property to White. See Figure 12-38. continues) Using Other Controls Lincoln Room continued) Return to the 1850s n this ovely room with privnte bath 585 S110 per ngh e1238 The LincolnForm 12. From the Toolbox, drag a Picture Z and click the ellipsis. A dia Box onto the Form. Select its Image property alog box allows you to select a resource. Click Local resource, and then click the Import button to browse for an image. Find the AbeLincoln file in the Chapter.12 folder of your downloadable student files, and double-click it. (Alternately, you can import another image you prefer.) After the mage appears in the Select Resource dialog box, click OK. Part of the selected ima ge appears in the PictureBox. Change the SizeMode property to Zoom box so that the picture is fully visible and everything looks attractive on the Form. See Figure 12-39 Adjust the size of the Form and the sizes and positions of the labels and picture Lincoln Room Return to the 1850s in this lovely room with private bath. $110 per night. Figure 12-39 The LincoInForm with dn Lmage in a PictureBox The AbeLincoln file was cbtained at continued www.clipartkid.com. You can mages to use in your own applications. You also shoud search the wet should search the Web for free cho art and similar phrases. 13. In the Solution Explorer, double-click Forml.cs or click the Fo cs[Design] tab at the top of the design screen. On the BedAnde Form, double-click the Lincoln Room method to start with an uppercase letter. Then add the following to the LincolnCheckBox_CheckedChanged 0 method: CheckBox, and rename the default au ast private void LincolnCheckBox_CheckedChanged Cobject sender, EventArgs e) if (lincolnCheckBox.Checked) LincolnForm 1incolnForm new LincolnForm lincolnForm. ShowDialogO 1 incolnCheckBox.checked= false; 14. Save the project and then execute it. When the BedAndBreakfast Form appears click either CheckBox-the appropriate informational Form appears. Close it and then click the other CheckBox. Again, the appropriate Form appears 15. Close all forms. Adding RadioButtons to a Form Next you add more Controls to the BedAndBreakfast Form. You generally use RadioButtons when a user must select from mutually exclusive options. 1. In the Design view of the main Form in the BedAndBreakfast project, add a Button near the bottom of the Form. Change the Button's Name propertyto mealButton and the Button's Text to Click for meal options. Adjust the size of the Button so that its text is fully visible. 2. From the menu bar, select Project, click Add New ltem, and click Windows Form. Name the Form BreakfastoptionForm, and click Add. On the new Form, make the following changes: . Set the Form's BackColor to Yellow. Drag a Label onto the Form. Name it appropriately, and set its Text to Select your breakfast option. . continues 2 Other Controls three RadioButtons onto the Form. Set their re properties to Drag rties to Continental, Full, and Deluxe Select an appropriate font for Drag a Label onto the Form, and then set its Text to Price: and its Hame O Contit ton (continued) r dioButton, fullRadioButton, and deluxeR ir respective Text uxe. Set their respective Names to oDe ton, and delue to 58 the F for the ton aux e Strrespe the Labe1 and RadioBut uxeRadioButtor to pricelabeI, Make the Font property a little larger than for the Form Con abe1 components, and also make it Bold. Figure 12-40 for approximate placement of all these Controis than for the other Breakfastption.. Select your breakfast option O Continental O Deluse ricc: Figure 12-40 Developing the BreakfastoptionForm named BreakfastOptionForm LoadO. Within this method, you can type statements that execute each time the Form is created. Add the following statements within the BreakfastoptionForm class, which declare three constants representing prices for different breakfast options. Within the BreakfastOptionForm LoadO method, set the pricelabel Text property to the lowest price by default when the Form loads public partial class BreakfastoptionForm : Form 3. Double-click the title bar of the BreakfastoptionForm to generate a method private const double CONT_ BREAKFAST PRICE 6.00; private const double FULL BREAKFAST-PRICE= 9.9 private const double DELUXE BREAKFAST PRICE 16.50; (continues) CHAPTER 12 Using Controls public BreakfastOptionForm) cortnued 588 InitializeComponent): private void Breakfastoptionform Load (object sender EventArgs e) priceLabel.Text "Price:" CONT _BREAKFAST_PRICE. ToStringC"C") 4. Return to the Design view for the BreakfastoptionForm, and double-click the contRadioButton. When you see the generated CheckedChangedO method rename it to start with an uppercase letter, and add a statement that sets pricelabel to the continental breakfast price when the user makes that selection private void ContRadioButton_CheckedChanged Cobject sender, EventArgs e) priceLabel.Text - "Price:" CONT_BREAKFAST PRICE .ToStringC"C 5. Return to the Design view for the BreakfastoptionForm, double-click the ful1RadioButton, rename the generated method, and add a statement that sets the pricelabel to the full breakfast price when the user makes that selection private void FullRadioButton_CheckedChanged Cobject sender, EventArgs e) priceLabel.Text "Price: "+ FULL BREAKFAST PRICE. ToStringC"C"); 6. Return to the Design view for the BreakfastoptionForm, double-click the deluxeRadioButton, rename the default method, and add a statement that sets the priceLabel to the deluxe breakfast price when the user makes that selecton: private void DeluxeRadioButton CheckedChanged (object sender, EventArgs e) priceLabel.Text-"Price: "+ DELUXE BREAKFAST PRICE.ToString("C") (continues Using, Otther Cantrols Form. Doubl generated, renacte user clicks the Button; tion Explorer, double click the Formi.cs fle to vex e-click the meal Button Button. When the click ble tton Wfile t uvoid McalButton Click (object sender cenerat lowing code so that the Breakfastoptionform s rename its Click) method to start with an uppe ded the original method is click) method is 589 astoptionForm is loaded when a case letter, and and EventArgs e) .c1i eutt akfastoptionForm breakfastform new BreakfastoptionFormO: kfastForm.ShowDialogO beakfastasb Savet the BelleAire Suite and Lincom Room CheckBoxes ved Then cick the project and execute it. When the BedAndBreakfast Form appea still work correctly, displaying their informa the Click for meal options Button. By default, the Continental breakfast option is chosen, as shown in Figure 12-41. Test the other radio buttons Dismiss the Forms, return to the IDE, and adjust the sizes and positions of any components as needed, and then test the program again tion Form when Dismiss all the Forms, and close Visual Studio Breakfastop... Selact your breakfast option O Continental O Full O Deluxe Price: $6.00 Figure 12-41 The BreakfastOptionForm with Continental breakfast RadioButton selected 12. Create a project named Lets MakeADeal.In this are assigned randomly to be hidden behind three "doors" that you can in Buttons. For example, the prizes might be a new car, a big-screen TV, and a live goat The player chooses a Button, and then one of the two other prizes is revealed; the one revealed is never the most desirable prize. The user then has the option of changing the original selection to the remaining unseen choice. For example, consider theseto game scenarios: game, three prizes of varying value Suppose that the most valuable prize is randomly assigned to the first button. Ii the user chooses the first button, reveal either of the other two prizes, and ask the user if he wants to change his selection. Suppose that the most valuable prize is assigned to the first button, but the user chooses the second button. Reveal the third prize so that the most valuable prizes location is still hidden, and then ask the user whether he wants to change his selection. . . After the user has chosen to retain his original selection or make a change, reveal wht he has won

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions