Exemplo n.º 1
0
        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            // Clear previous entries
            ClearBlocks();
            definitionsComboBox.Items.Clear();

            // Check to see if input is empty or invalid
            if (inputTextBox.Text == null || inputTextBox.Text == "")
            {
                return;
            }

            // Webreqest to UrbanDictionary to find all similar definition entries
            DefinitionList list = Lookup.LookupDefinitions(inputTextBox.Text);

            // Final check to see if the results were null
            if (list == null || list.definitions == null || list.definitions.Count == 0)
            {
                definitionsComboBox.IsEnabled = false;
                return;
            }

            // Add results to the ComboBox
            foreach (Definition def in list.definitions)
            {
                definitionsComboBox.Items.Add(def);
            }

            // Make sure the ComboBox is enabled
            definitionsComboBox.IsEnabled = true;
            // Set the current selected ComboBox option to the most apporopriate
            definitionsComboBox.SelectedValue = DefinitionHandler.FindBestDefinition(list, inputTextBox.Text) ?? definitionsComboBox.Items[0] ?? null;
        }
Exemplo n.º 2
0
        public static Definition FindBestDefinition(DefinitionList list, string searchTerm)
        {
            foreach (Definition definition in list.definitions)
            {
                if (definition.word == searchTerm)
                {
                    return(definition);
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        // Main method for looking up a string on UrbanDictionary
        public static DefinitionList LookupDefinitions(string searchTerm)
        {
            // Initial response from the website
            string response = null;

            try
            {
                // Simple webrequest
                using (var request = new WebClient())
                {
                    response = request.DownloadString("http://api.urbandictionary.com/v0/define?term=" + "\"" + searchTerm + "\"");
                }
            }

            // If webrequest fails or returns nothing
            catch (Exception e)
            {
                System.Windows.MessageBox.Show(e.ToString(), "There was an error contacting a dictionary", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error, System.Windows.MessageBoxResult.OK);
            }

            // Check that the response is valid and not empty
            if (response == null || response == "")
            {
                System.Windows.MessageBox.Show("Could not connect to a dictionary", "Error!", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error, System.Windows.MessageBoxResult.OK);
            }

            // Convert response to an actual object
            DefinitionList list = ConvertToDefinitionList(response);

            // Check if there were at least one definition returned
            if (list.definitions.Count == 0)
            {
                System.Windows.MessageBox.Show("No definitions were found!", "Error!", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error, System.Windows.MessageBoxResult.OK);
            }

            return(list);
        }