コード例 #1
0
        private void btn_SearchSubmit_Click(object sender, EventArgs e)
        {
            BusinessObjects _businessObjects = new BusinessObjects();
            // Element reset to be performed each time, before a search is ran
            lbl_CatalogResultsFound.Text = null;           // Clear contents of search result label
            cbx_CatalogResultsList.Items.Clear();          // Clear old search results from results combo box
            cbx_CatalogResultsList.Text = "";              // Clear text from visible combobox area
            rbx_CatalogSearchResults.Clear();        // Clear search results each time a new search is submitted
            catalogItemCollection = null;                  // Clear inventory items from list

            // Verify that a query type was selected by the user
            if (cbx_CatalogQueryType.SelectedItem == null)
            {
                MessageBox.Show("You forgot to select a query type.  Please try again", "Invalid Query",
                                    MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }

            // SEARCH BY MANUFACTURER NAME - Chech to see if user entered a Manufacturer Name
            else if (cbx_CatalogQueryType.SelectedItem.ToString() == "Manufacturer")
            {
                // Generate a list of Inventory Items that match the Manufacturer Name entered by the user
                catalogItemCollection = _businessObjects.GetCatalogItemByManufacturer(tbx_CatalogQueryInput.Text);
            }

            // SEARCH BY ITEM NAME - Check to see if user entered an Item Name
            else if (cbx_CatalogQueryType.SelectedItem.ToString() == "Item Name")
            {
                // Generate a list of Catalog Items that match the Item Name entered by the user
                catalogItemCollection = (catalogItemCollection == null) ? new List<CatalogItem>() : catalogItemCollection;
                catalogItemCollection.Add(_businessObjects.GetCatalogItemByItemName(tbx_CatalogQueryInput.Text));
            }

            // SEARCH BY ITEM GUID - Check to see if user entered an ID Number
            else if (cbx_CatalogQueryType.SelectedItem.ToString() == "Catalog ID")
            {
                try
                {
                    // Generate a list of Catalog Items that match the Item ID entered by the user
                    catalogItemCollection = (catalogItemCollection == null) ? new List<CatalogItem>() : catalogItemCollection;
                    catalogItemCollection.Add(ApplicationObjects.GetCatalogItemByCatalogItemId(new Guid(tbx_CatalogQueryInput.Text)));
                }
                catch (Exception)
                {   // Catch if a non-Guid was entered
                    MessageBox.Show("You entered an invalid ID.  Please make sure that the ID contains 32 characters and 4 hyphens", "Invalid GUID",
                                    MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    rbx_CatalogSearchResults.AppendText("Input error - please try again");
                    return;
                }
            }

            // If nothing is populated into the searchDisplay box, we can assume that there were no search results
            else
            {
                lbl_CatalogResultsFound.Text = "No results found";
            }

            // Populate label that displays how many results were found
            lbl_CatalogResultsFound.Text = (catalogItemCollection.Count.ToString() + " result(s) found!");

            // This string list holds the results of an CatalogItem's ToItemDescription method, which actually
            // returns a list of individual line items, which each hold the item's attributes that will be
            // displayed on seperate lines of the text box
            List<string> itemDescriptions = (catalogItemCollection.Count > 0)
                                                    ? catalogItemCollection.FirstOrDefault().ToItemDescription()
                                                    : new List<string>();
            foreach (string lineItem in itemDescriptions)
            {   // Add each description line item to the text box
                rbx_CatalogSearchResults.AppendText(lineItem + Environment.NewLine);
            }

            // Populate the search result combobox with the Catalog Id numbers of the search results
            foreach (CatalogItem catalogItem in catalogItemCollection)
            {
                cbx_CatalogResultsList.Items.Add(catalogItem.CatalogItemId.ToString());
            }
            // Set the combobox to show the catalog number of the first search record
            if(cbx_CatalogResultsList.Items.Count > 0)
                cbx_CatalogResultsList.SelectedIndex = 0;
        }