2. Design the form 1. Open the project that's in the C:\C#\Chapter 18\CustomerInvoices directory. In addition to the Cus

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

2. Design the form 1. Open the project that's in the C:\C#\Chapter 18\CustomerInvoices directory. In addition to the Cus

Post by answerhappygod »

2 Design The Form 1 Open The Project That S In The C C Chapter 18 Customerinvoices Directory In Addition To The Cus 1
2 Design The Form 1 Open The Project That S In The C C Chapter 18 Customerinvoices Directory In Addition To The Cus 1 (479.12 KiB) Viewed 33 times
2 Design The Form 1 Open The Project That S In The C C Chapter 18 Customerinvoices Directory In Addition To The Cus 2
2 Design The Form 1 Open The Project That S In The C C Chapter 18 Customerinvoices Directory In Addition To The Cus 2 (276.66 KiB) Viewed 33 times
2 Design The Form 1 Open The Project That S In The C C Chapter 18 Customerinvoices Directory In Addition To The Cus 3
2 Design The Form 1 Open The Project That S In The C C Chapter 18 Customerinvoices Directory In Addition To The Cus 3 (556.09 KiB) Viewed 33 times
CustomerDB.cs
2 Design The Form 1 Open The Project That S In The C C Chapter 18 Customerinvoices Directory In Addition To The Cus 4
2 Design The Form 1 Open The Project That S In The C C Chapter 18 Customerinvoices Directory In Addition To The Cus 4 (17.73 KiB) Viewed 33 times
2 Design The Form 1 Open The Project That S In The C C Chapter 18 Customerinvoices Directory In Addition To The Cus 5
2 Design The Form 1 Open The Project That S In The C C Chapter 18 Customerinvoices Directory In Addition To The Cus 5 (185.37 KiB) Viewed 33 times
2 Design The Form 1 Open The Project That S In The C C Chapter 18 Customerinvoices Directory In Addition To The Cus 6
2 Design The Form 1 Open The Project That S In The C C Chapter 18 Customerinvoices Directory In Addition To The Cus 6 (711.67 KiB) Viewed 33 times
2 Design The Form 1 Open The Project That S In The C C Chapter 18 Customerinvoices Directory In Addition To The Cus 7
2 Design The Form 1 Open The Project That S In The C C Chapter 18 Customerinvoices Directory In Addition To The Cus 7 (184.32 KiB) Viewed 33 times
program.cs
2. Design the form 1. Open the project that's in the C:\C#\Chapter 18\CustomerInvoices directory. In addition to the Customer Invoice form, this project contains the business and database classes needed by the application. Add a ListView control to the Customer Invoice form, and set the View property of this control to Details. 3. Use the smart tag menu for the ListView control to display the ColumnHeader Collection Editor. Then, define the column headings for this control so they appear like the last three shown in figure 18-12. (You'll add the first column later.) Add code to display the invoice data 4. Open the Invoice and InvoiceDB classes and review the code that they contain. In particular, notice that the GetInvoices() method in the InvoiceDB class gets invoices from a text file named Invoices.txt and returns them in a List<Invoice> object. 5. Add an event handler for the Load event of the form. Then, use the GetInvoices() method to get the list of invoices, and store this list in a variable. 6. Define a query expression that returns all the invoices from the invoice list and sorts them by invoice total in descending sequence. Include a select clause in this query expression that selects entire invoices. 7. Use a foreach statement to execute the query and load the results into the List View control. 8. Run the application to see how it works. Make any necessary corrections, and then end the application. 9. Modify the select clause in the query expression so the query returns only the fields needed by the form. Then, run the application again to be sure it still works.
Enhance the application to include customer information 10. Open the Customer and CustomerDB classes and review the code they contain. Note that the GetCustomers() method in the CustomerDB class gets customers from a text file named Customers 18.txt and returns them in a List<Customer> object. 11. Add another column at the beginning of the List View control for displaying the customer name. (To get this column to display before the other columns, you'll need to set its Display Index property to 0.) Then, add a statement to the
Load event handler of the form that uses the GetCustomers() method of the CustomerDB class to get a list of customers, and store the list in a variable. 12. Modify the query expression so it joins the data in the customer list with the data in the invoice list, so it sorts the results by invoice total within customer name, and so only the fields that are needed by the form are returned by the query. 13. Modify the foreach statement so it adds the customer name to the List View control, but don't worry about not repeating the customer name. 14. Run the application to make sure it works correctly. 15. If you're going to continue with the next exercise, leave the solution open. Otherwise, close the solution. Exercise 18-2 Use a method-based query In this exercise, you'll modify the solution to exercise 18-1 so it uses a method- based query instead of a query expression. 1. If it's not already open, open your solution for exercise 18-1 and display the code for the form. 2. Comment out the query expression in the event handler for the Load event of the form. Then, code a method-based query to replace the query expression. As you do that, you may want to add one method at a time, testing as you go. Note: If you use i as the name of the parameter for the lambda expressions in the Join() method, it will conflict with the variable named i that's used by the foreach statement. You can fix that by using a different name for the param- eter or variable. 3. When you have the query working, close the solution.
1 ETE 2 3 Eusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 4 00 00 N P 5 6 7 namespace CustomerInvoices 8 9 10 5 references public class Customer { 1 reference public int CustomerID { get; set; } 1 reference public string Name { get; set; } 11 12 13 14 15
3 4 5 6 7 8 9 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace CustomerInvoices O references public static class CustomerDB 0 1 2. 3 4. private const string Dir = @"C:\C#\Files\"; private const string Path = Dir + "Customers18.txt"; O references public static List<Customer> GetCustomers() 5 -6 7 List<Customer> customers = new List<Customer>(); 9 StreamReader textIn = new StreamReader( new FileStream(Path, FileMode. Open, FileAccess. Read)); while (textIn.Peek() != -1) 1 2. 3 4 5 6 7 8 9 0 1 2 3 string row = textIn.ReadLine(); string[] columns = row.Split('|'); Customer customer = new Customer(); customer.CustomerID = Convert.ToInt32(columns[0]); customer.Name = columns[1]; customers.Add(customer); textIn.Close(); return customers; 14 } 5 6

1 2 3 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 4 COW ON UW N E 5 다. 10 11 12 namespace CustomerInvoices { 5 references public class Invoice { 1 reference public int InvoiceID { get; set; } 1 reference public int CustomerID { get; set; } 1 reference public DateTime InvoiceDate { get; set; } 1 reference public decimal ProductTotal { get; set; } 1 reference public decimal SalesTax { get; set; } 1 reference public decimal Shipping { get; set; } 1 reference public decimal InvoiceTotal { get; set; } 13 14 15 16 1 17 18 19 20
2 3 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; 5 NM in NO 0 6 8 namespace CustomerInvoices 10 11 12 13 14 O references public static class InvoiceDB { private const string Dir = @"C:\C#\Files\"; private const string Path = Dir + "Invoices.txt"; O references public static List<Invoice> GetInvoices { List<Invoice> invoices = new List<Invoice>(); StreamReader textIn = new StreamReader new FileStream(Path, FileMode. Open, FileAccess. Read)); while (textIn.Peek() != -1) 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 string row = textIn.ReadLine(); string[] columns = row.Split('|'); Invoice invoice = new Invoice(); invoice. InvoiceID = Convert.ToInt32(columns[@]); invoice. CustomerID = Convert.ToInt32 columns[1]); invoice. InvoiceDate = Convert.ToDateTime(columns[2]); invoice. ProductTotal = Convert.ToDecimal(columns[3]); invoice.SalesTax = Convert.ToDecimal(columns[4]); invoice. Shipping = Convert.ToDecimal(columns[5]); invoice. InvoiceTotal = Convert.ToDecimal(columns[6]); invoices.Add(invoice); textIn.Close(); return invoices;
1 2 3 Qusing System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; 4 N m n N 00 5 6 7 namespace CustomerInvoices 8 O references static class Program 9 10 11 12 13 T I 14 15 16 17 18 19 20 /// <summary> The main entry point for the application. /// </summary [STAThread] O references static void Main() { Application. SetHighDpiMode (HighDpiMode.SystemAware); Application. EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(); 21 22 23 24
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply