コード例 #1
0
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string material = cmbMaterial.Text;

            lbQuotes.Items.Clear();

            try
            {
                //check if file exists
                if (File.Exists(@"quotes.json"))
                {
                    //set initialJson to the text within the file
                    var initialJson = File.ReadAllText(@"quotes.json");

                    //parses file contents to a JArray named "array"
                    var array = JArray.Parse(initialJson);

                    foreach (JObject quote in array)
                    {
                        //create DeskQuote object from this record in the array
                        DeskQuote dq = quote.ToObject <DeskQuote>();

                        //if it has the selected surface material, format a string and add to list box
                        if (dq.QuotedDesk.SurfaceMaterial == material)
                        {
                            string formattedString = "Date: " + dq.QuoteDate;
                            formattedString += " Name: " + dq.SelectedCustomerName;
                            formattedString += " Width: " + dq.QuotedDesk.Width;
                            formattedString += " Depth: " + dq.QuotedDesk.Depth;
                            formattedString += " # of Drawers: " + dq.QuotedDesk.CountDrawer;
                            formattedString += " Material: " + dq.QuotedDesk.SurfaceMaterial;
                            formattedString += " Build Time: " + dq.SelectedBuildOption + " days";
                            formattedString += " Quote: $" + dq.QuotedFinalCost;
                            lbQuotes.Items.Add(formattedString);
                        }
                    }
                }
                else
                {
                    //if the file does nto exist, show the message box
                    MessageBox.Show(@"You have no quotes saved!");
                }
            }
            catch (Exception ex)
            {
                //if there is an exception, show the error message
                MessageBox.Show(@"Failed to find quotes!" + "\n" + ex.Message);
            }
        }