'Program: Ch08HandsOn 'Programmer: Joseph Schuman 'Date: October 2013 'Description: Look up the price for bulk coffee. ' based upon quantity and type. ' Uses a structure and arrays, and prints a report ' of the transactions from the array. 'Folder: Ch08HandsOn Public Class BulkCoffeeForm 'Declare structure and module-level variables Structure CoffeeSale Dim TypeString As String Dim QuantityString As String Dim PriceDecimal As Decimal End Structure Private TransactionCoffeeSale(20) As CoffeeSale Private NumberTransactionsInteger As Integer Private PriceDecimal(,) As Decimal = {{2.6D, 2.9D, 3.25D}, {4.9D, 5.6D, 6.1D}, {8.75D, 9.75D, 11.25D}} Private SelectedButtonString As String Private Sub ExitButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitButton.Click 'Terminate the project. Dim ResponseDialogResult As DialogResult ResponseDialogResult = MessageBox.Show("Print the report?", "Terminate the Application", MessageBoxButtons.YesNo, MessageBoxIcon.Question) If ResponseDialogResult = Windows.Forms.DialogResult.Yes Then PrintButton_Click(sender, e) End If Me.Close() End Sub Private Sub ClearButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ClearButton.Click 'Remove the selection from the list and clear the price 'Select first radio button QuarterPoundRadioButton.Checked = True 'Clear coffee type selection CoffeeTypeComboBox.SelectedIndex = -1 PriceTextBox.Text = "" End Sub Private Sub FindPriceButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles FindPriceButton.Click 'Lookup the price using the quantity and type Dim RowInteger, ColumnInteger As Integer Dim SalePriceDecimal As Decimal 'Allow only 20 transactions If NumberTransactionsInteger < 20 Then ColumnInteger = CoffeeTypeComboBox.SelectedIndex If ColumnInteger <> -1 Then 'Coffee selection made, determine quantity selected. Select Case SelectedButtonString Case "QuarterPoundRadioButton" RowInteger = 0 TransactionCoffeeSale(NumberTransactionsInteger).QuantityString = "Quarter Pound" Case "HalfPoundRadioButton" RowInteger = 1 TransactionCoffeeSale(NumberTransactionsInteger).QuantityString = "Half Pound" Case "FullPoundRadioButton" RowInteger = 2 TransactionCoffeeSale(NumberTransactionsInteger).QuantityString = "Full Pound" Case Else 'No selection made; use quarter pound. RowInteger = 0 TransactionCoffeeSale(NumberTransactionsInteger).QuantityString = "Quarter Pound" End Select 'Retrieve price of selection. SalePriceDecimal = PriceDecimal(RowInteger, ColumnInteger) PriceTextBox.Text = SalePriceDecimal.ToString("C") 'Save this transaction. TransactionCoffeeSale(NumberTransactionsInteger).TypeString = CoffeeTypeComboBox.Text TransactionCoffeeSale(NumberTransactionsInteger).PriceDecimal = SalePriceDecimal NumberTransactionsInteger += 1 Else MessageBox.Show("Select the coffee type.", "Selection Incomplete", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If Else MessageBox.Show("Sorry, only 20 transactions allowed.") End If End Sub Private Sub PrintButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PrintButton.Click 'Print the report using Print Preview. PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog() End Sub Private Sub QuarterPoundRadioButton_CheckedChanged( ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles FullPoundRadioButton.CheckedChanged, HalfPoundRadioButton.CheckedChanged, QuarterPoundRadioButton.CheckedChanged 'Save the name of the selected radio button. 'This procedure is executed each time any radio button is selected. SelectedButtonString = CType(sender, RadioButton).Name End Sub Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 'Handles print and print previews. Dim PrintFont As New Font("Arial", 12) Dim HeadingFont As New Font("Arial", 14, FontStyle.Bold) Dim LineHeightSingle As Single = PrintFont.GetHeight + 2 Dim Column1HorizontalLocationSingle As Single = e.MarginBounds.Left Dim VerticalPrintLocationSingle As Single = e.MarginBounds.Top Dim Column2HorizontalLocationSingle As Single = 300 Dim Column3HorizontalLocationSingle As Single Dim PrintLineString As String Dim FontSizeF As New SizeF Dim FormattedPriceString As String 'Set up and display heading lines. PrintLineString = "R 'n R Coffee Sales" e.Graphics.DrawString(PrintLineString, HeadingFont, Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle) PrintLineString = " by Joseph Schuman" VerticalPrintLocationSingle += LineHeightSingle e.Graphics.DrawString(PrintLineString, PrintFont, Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle) VerticalPrintLocationSingle += LineHeightSingle * 2 'Loop through transactions. For Each IndividualCoffeeSale As CoffeeSale In TransactionCoffeeSale 'Don't print if blank. If IndividualCoffeeSale.QuantityString <> "" Then 'Set up a line. 'Quantity. e.Graphics.DrawString(IndividualCoffeeSale.QuantityString, PrintFont, Brushes.Black, Column1HorizontalLocationSingle, VerticalPrintLocationSingle) 'Type. e.Graphics.DrawString(IndividualCoffeeSale.TypeString, PrintFont, Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle) 'Right-align the price. FormattedPriceString = FormatNumber(IndividualCoffeeSale.PriceDecimal) 'Measure string in the font FontSizeF = e.Graphics.MeasureString(FormattedPriceString, PrintFont) 'Subtract width of string from column position. Column3HorizontalLocationSingle = 550 - FontSizeF.Width e.Graphics.DrawString(FormattedPriceString, PrintFont, Brushes.Black, Column3HorizontalLocationSingle, VerticalPrintLocationSingle) 'Increment the Y position for the next line; Double space. VerticalPrintLocationSingle += LineHeightSingle * 2 End If Next End Sub End Class 'Program: Ch08HandsOn 'Programmer: Joseph Schuman 'Date: October 2013 'Description: Look up the price for bulk coffee. ' based upon quantity and type. ' Uses a structure and arrays, and prints a report ' of the transactions from the array. 'Folder: Ch08HandsOn Public Class BulkCoffeeForm 'Declare structure and module-level variables Structure CoffeeSale Dim TypeString As String Dim QuantityString As String Dim PriceDecimal As Decimal End Structure Private TransactionCoffeeSale(20) As CoffeeSale Private NumberTransactionsInteger As Integer Private PriceDecimal(,) As Decimal = {{2.6D, 2.9D, 3.25D}, {4.9D, 5.6D, 6.1D}, {8.75D, 9.75D, 11.25D}} Private SelectedButtonString As String Private Sub ExitButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitButton.Click 'Terminate the project. Dim ResponseDialogResult As DialogResult ResponseDialogResult = MessageBox.Show("Print the report?", "Terminate the Application", MessageBoxButtons.YesNo, MessageBoxIcon.Question) If ResponseDialogResult = Windows.Forms.DialogResult.Yes Then PrintButton_Click(sender, e) End If Me.Close() End Sub Private Sub ClearButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ClearButton.Click 'Remove the selection from the list and clear the price 'Select first radio button QuarterPoundRadioButton.Checked = True 'Clear coffee type selection CoffeeTypeComboBox.SelectedIndex = -1 PriceTextBox.Text = "" End Sub Private Sub FindPriceButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles FindPriceButton.Click 'Lookup the price using the quantity and type Dim RowInteger, ColumnInteger As Integer Dim SalePriceDecimal As Decimal 'Allow only 20 transactions If NumberTransactionsInteger < 20 Then ColumnInteger = CoffeeTypeComboBox.SelectedIndex If ColumnInteger <> -1 Then 'Coffee selection made, determine quantity selected. Select Case SelectedButtonString Case "QuarterPoundRadioButton" RowInteger = 0 TransactionCoffeeSale(NumberTransactionsInteger).QuantityString = "Quarter Pound" Case "HalfPoundRadioButton" RowInteger = 1 TransactionCoffeeSale(NumberTransactionsInteger).QuantityString = "Half Pound" Case "FullPoundRadioButton" RowInteger = 2 TransactionCoffeeSale(NumberTransactionsInteger).QuantityString = "Full Pound" Case Else 'No selection made; use quarter pound. RowInteger = 0 TransactionCoffeeSale(NumberTransactionsInteger).QuantityString = "Quarter Pound" End Select 'Retrieve price of selection. SalePriceDecimal = PriceDecimal(RowInteger, ColumnInteger) PriceTextBox.Text = SalePriceDecimal.ToString("C") 'Save this transaction. TransactionCoffeeSale(NumberTransactionsInteger).TypeString = CoffeeTypeComboBox.Text TransactionCoffeeSale(NumberTransactionsInteger).PriceDecimal = SalePriceDecimal NumberTransactionsInteger += 1 Else MessageBox.Show("Select the coffee type.", "Selection Incomplete", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If Else MessageBox.Show("Sorry, only 20 transactions allowed.") End If End Sub Private Sub PrintButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PrintButton.Click 'Print the report using Print Preview. PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog() End Sub Private Sub QuarterPoundRadioButton_CheckedChanged( ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles FullPoundRadioButton.CheckedChanged, HalfPoundRadioButton.CheckedChanged, QuarterPoundRadioButton.CheckedChanged 'Save the name of the selected radio button. 'This procedure is executed each time any radio button is selected. SelectedButtonString = CType(sender, RadioButton).Name End Sub Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 'Handles print and print previews. Dim PrintFont As New Font("Arial", 12) Dim HeadingFont As New Font("Arial", 14, FontStyle.Bold) Dim LineHeightSingle As Single = PrintFont.GetHeight + 2 Dim Column1HorizontalLocationSingle As Single = e.MarginBounds.Left Dim VerticalPrintLocationSingle As Single = e.MarginBounds.Top Dim Column2HorizontalLocationSingle As Single = 300 Dim Column3HorizontalLocationSingle As Single Dim PrintLineString As String Dim FontSizeF As New SizeF Dim FormattedPriceString As String 'Set up and display heading lines. PrintLineString = "R 'n R Coffee Sales" e.Graphics.DrawString(PrintLineString, HeadingFont, Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle) PrintLineString = " by Joseph Schuman" VerticalPrintLocationSingle += LineHeightSingle e.Graphics.DrawString(PrintLineString, PrintFont, Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle) VerticalPrintLocationSingle += LineHeightSingle * 2 'Loop through transactions. For Each IndividualCoffeeSale As CoffeeSale In TransactionCoffeeSale 'Don't print if blank. If IndividualCoffeeSale.QuantityString <> "" Then 'Set up a line. 'Quantity. e.Graphics.DrawString(IndividualCoffeeSale.QuantityString, PrintFont, Brushes.Black, Column1HorizontalLocationSingle, VerticalPrintLocationSingle) 'Type. e.Graphics.DrawString(IndividualCoffeeSale.TypeString, PrintFont, Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle) 'Right-align the price. FormattedPriceString = FormatNumber(IndividualCoffeeSale.PriceDecimal) 'Measure string in the font FontSizeF = e.Graphics.MeasureString(FormattedPriceString, PrintFont) 'Subtract width of string from column position. Column3HorizontalLocationSingle = 550 - FontSizeF.Width e.Graphics.DrawString(FormattedPriceString, PrintFont, Brushes.Black, Column3HorizontalLocationSingle, VerticalPrintLocationSingle) 'Increment the Y position for the next line; Double space. VerticalPrintLocationSingle += LineHeightSingle * 2 End If Next End Sub End Class 'Program: Ch08HandsOn 'Programmer: Joseph Schuman 'Date: October 2013 'Description: Look up the price for bulk coffee. ' based upon quantity and type. ' Uses a structure and arrays, and prints a report ' of the transactions from the array. 'Folder: Ch08HandsOn Public Class BulkCoffeeForm 'Declare structure and module-level variables Structure CoffeeSale Dim TypeString As String Dim QuantityString As String Dim PriceDecimal As Decimal End Structure Private TransactionCoffeeSale(20) As CoffeeSale Private NumberTransactionsInteger As Integer Private PriceDecimal(,) As Decimal = {{2.6D, 2.9D, 3.25D}, {4.9D, 5.6D, 6.1D}, {8.75D, 9.75D, 11.25D}} Private SelectedButtonString As String Private Sub ExitButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitButton.Click 'Terminate the project. Dim ResponseDialogResult As DialogResult ResponseDialogResult = MessageBox.Show("Print the report?", "Terminate the Application", MessageBoxButtons.YesNo, MessageBoxIcon.Question) If ResponseDialogResult = Windows.Forms.DialogResult.Yes Then PrintButton_Click(sender, e) End If Me.Close() End Sub Private Sub ClearButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ClearButton.Click 'Remove the selection from the list and clear the price 'Select first radio button QuarterPoundRadioButton.Checked = True 'Clear coffee type selection CoffeeTypeComboBox.SelectedIndex = -1 PriceTextBox.Text = "" End Sub Private Sub FindPriceButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles FindPriceButton.Click 'Lookup the price using the quantity and type Dim RowInteger, ColumnInteger As Integer Dim SalePriceDecimal As Decimal 'Allow only 20 transactions If NumberTransactionsInteger < 20 Then ColumnInteger = CoffeeTypeComboBox.SelectedIndex If ColumnInteger <> -1 Then 'Coffee selection made, determine quantity selected. Select Case SelectedButtonString Case "QuarterPoundRadioButton" RowInteger = 0 TransactionCoffeeSale(NumberTransactionsInteger).QuantityString = "Quarter Pound" Case "HalfPoundRadioButton" RowInteger = 1 TransactionCoffeeSale(NumberTransactionsInteger).QuantityString = "Half Pound" Case "FullPoundRadioButton" RowInteger = 2 TransactionCoffeeSale(NumberTransactionsInteger).QuantityString = "Full Pound" Case Else 'No selection made; use quarter pound. RowInteger = 0 TransactionCoffeeSale(NumberTransactionsInteger).QuantityString = "Quarter Pound" End Select 'Retrieve price of selection. SalePriceDecimal = PriceDecimal(RowInteger, ColumnInteger) PriceTextBox.Text = SalePriceDecimal.ToString("C") 'Save this transaction. TransactionCoffeeSale(NumberTransactionsInteger).TypeString = CoffeeTypeComboBox.Text TransactionCoffeeSale(NumberTransactionsInteger).PriceDecimal = SalePriceDecimal NumberTransactionsInteger += 1 Else MessageBox.Show("Select the coffee type.", "Selection Incomplete", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If Else MessageBox.Show("Sorry, only 20 transactions allowed.") End If End Sub Private Sub PrintButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PrintButton.Click 'Print the report using Print Preview. PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog() End Sub Private Sub QuarterPoundRadioButton_CheckedChanged( ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles FullPoundRadioButton.CheckedChanged, HalfPoundRadioButton.CheckedChanged, QuarterPoundRadioButton.CheckedChanged 'Save the name of the selected radio button. 'This procedure is executed each time any radio button is selected. SelectedButtonString = CType(sender, RadioButton).Name End Sub Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 'Handles print and print previews. Dim PrintFont As New Font("Arial", 12) Dim HeadingFont As New Font("Arial", 14, FontStyle.Bold) Dim LineHeightSingle As Single = PrintFont.GetHeight + 2 Dim Column1HorizontalLocationSingle As Single = e.MarginBounds.Left Dim VerticalPrintLocationSingle As Single = e.MarginBounds.Top Dim Column2HorizontalLocationSingle As Single = 300 Dim Column3HorizontalLocationSingle As Single Dim PrintLineString As String Dim FontSizeF As New SizeF Dim FormattedPriceString As String 'Set up and display heading lines. PrintLineString = "R 'n R Coffee Sales" e.Graphics.DrawString(PrintLineString, HeadingFont, Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle) PrintLineString = " by Joseph Schuman" VerticalPrintLocationSingle += LineHeightSingle e.Graphics.DrawString(PrintLineString, PrintFont, Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle) VerticalPrintLocationSingle += LineHeightSingle * 2 'Loop through transactions. For Each IndividualCoffeeSale As CoffeeSale In TransactionCoffeeSale 'Don't print if blank. If IndividualCoffeeSale.QuantityString <> "" Then 'Set up a line. 'Quantity. e.Graphics.DrawString(IndividualCoffeeSale.QuantityString, PrintFont, Brushes.Black, Column1HorizontalLocationSingle, VerticalPrintLocationSingle) 'Type. e.Graphics.DrawString(IndividualCoffeeSale.TypeString, PrintFont, Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle) 'Right-align the price. FormattedPriceString = FormatNumber(IndividualCoffeeSale.PriceDecimal) 'Measure string in the font FontSizeF = e.Graphics.MeasureString(FormattedPriceString, PrintFont) 'Subtract width of string from column position. Column3HorizontalLocationSingle = 550 - FontSizeF.Width e.Graphics.DrawString(FormattedPriceString, PrintFont, Brushes.Black, Column3HorizontalLocationSingle, VerticalPrintLocationSingle) 'Increment the Y position for the next line; Double space. VerticalPrintLocationSingle += LineHeightSingle * 2 End If Next End Sub Private Sub PrintPreviewDialog1_Load(sender As Object, e As EventArgs) Handles PrintPreviewDialog1.Load End Sub End Class