Question
Basics Chapter 9 Ex 6 In this exercise, you will modify the Warren School application from Chapter 8s Apply the Concepts lesson. Open the Warren
Basics Chapter 9 Ex 6 In this exercise,
you will modify the Warren School application from Chapter 8s Apply the Concepts lesson.
Open the Warren Solution.sln file contained in the VB2017\Chap09\Warren Solution folder. The frmMain_FormClosing procedure should save the sales amounts stored in the intCandy array to a sequential access file named candySales.txt. Code the procedure. The frmMain_Load procedure should fill the array with the sales amounts from the candySales.txt file. (Hint: If the file does not exist, you do not need to display a message to the user.) The frmMain_Load procedure should also display the array amounts in the appropriate label controls. Create an independent Sub procedure named DisplayArray to handle this task. Then, modify the btnAdd_Click procedure so it uses the DisplayArray procedure. Save the solution and then start the application. Type 7 in the Sold box and then click the Add to total button. The number 7 appears in the Choco Bar box. Now, click Kit Kat in the Candy list box, type 10 in the Sold box, and then click the Add to total button. The number 10 appears in the Kit Kat box. Click the Exit button. Open the candySales.txt file. The file contains the numbers 7, 0, 10, 0, and 0. Close the candySales.txt file. Start the application again. The candy boxes on the form reflect the amounts stored in the file: 7, 0, 10, 0, and 0. Click Kit Kat in the Candy box, type 20 in the Sold box, and then click the Add to total button. The number 30 appears in the Kit Kat box. Click the Exit button. Open the candySales.txt file. The file now contains the numbers 7, 0, 30, 0, and 0. Close the candySales.txt file. Start the application again. The candy boxes on the form reflect the amounts stored in the file: 7, 0, 30, 0, and 0. On your own, continue testing the application. When you are finished testing, click the Exit button.
My code
Name: Warren Project ' Purpose: Displays the amount sold for each candy type. ' Programmer: on
Option Explicit On Option Strict On Option Infer Off
Public Class frmMain Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load ' Fills the list box with values and then selects the first value.
lstCandy.Items.Add("Choco Bar") lstCandy.Items.Add("Choco Bar-Peanuts") lstCandy.Items.Add("Kit Kat") lstCandy.Items.Add("Peanut Butter Cups") lstCandy.Items.Add("Take 5 Bar") lstCandy.SelectedIndex = 0 End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click ' Adds the amount sold to the appropriate total.
' Declare array and variable. Static intCandy(4) As Integer Dim intSold As Integer
Integer.TryParse(txtSold.Text, intSold)
' Update array value. intCandy(lstCandy.SelectedIndex) += intSold
' Display array values. lblChocoBar.Text = intCandy(0).ToString lblChocoBarPeanuts.Text = intCandy(1).ToString lblKitKat.Text = intCandy(2).ToString lblPeanutButCups.Text = intCandy(3).ToString lblTake5Bar.Text = intCandy(4).ToString
txtSold.Focus() End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click Me.Close() End Sub
Private Sub txtSold_Enter(sender As Object, e As EventArgs) Handles txtSold.Enter txtSold.SelectAll() End Sub
Private Sub txtSold_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtSold.KeyPress ' Accept only numbers, the hyphen, and the Backspace.
If (e.KeyChar "9") AndAlso e.KeyChar "-" AndAlso e.KeyChar ControlChars.Back Then e.Handled = True End If End Sub
Private Sub ClearOutput(sender As Object, e As EventArgs) Handles txtSold.TextChanged, lstCandy.SelectedIndexChanged lblChocoBar.Text = String.Empty lblChocoBarPeanuts.Text = String.Empty lblKitKat.Text = String.Empty lblPeanutButCups.Text = String.Empty lblTake5Bar.Text = String.Empty End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub End Class
Warren School Chocolate Fund Raiser Chco Cand IstCand Sold Add to total Exit Choco Bar: Choco Bar-Peanuts Kit Kat: Peanut Butter Cup: Take 5 BarStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started