Beispiel #1
0
        /// <summary>
        /// One click on the right "Fill" button fills the lbWordSearch listbox with our entire resource "WordList.txt" file.
        /// Depending on what radio button is checked (Rhyme, Scrabble, Morph Word, or Morph Chain), the lbWordSearch list box will be filled with the designated function correlated to the word in the textboxes.
        /// If none of the radio buttons are checked, print out error message box.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFill2_Click(object sender, EventArgs e)
        {
            if (!(rbRhyme.Checked || rbScrabble.Checked || rbMorphWord.Checked || rbMorphChain.Checked))
            {
                string msgStr = String.Format("Option not selected.");
                MessageBox.Show(msgStr);
            }

            // Finds words that rhyme with the word inside the textbox.
            // Used try/catch to detect error from user input.
            else if (rbRhyme.Checked)
            {
                lbWordSearch.BeginUpdate();
                lbWordSearch.Items.Clear();
                List <string> rhymeWords = word.RhymeWord(txtUser.Text);
                try
                {
                    if (rhymeWords.Count == 0 || string.IsNullOrWhiteSpace(txtUser.Text))
                    {
                        throw new Exception();
                    }
                    labelError.Visible = false;
                    foreach (string words in rhymeWords)
                    {
                        lbWordSearch.Items.Add(words);
                    }
                }
                catch (Exception)
                {
                    labelError.Text    = "Invalid Input";
                    labelError.Visible = true;
                }
                // If no words are found, print out message
                lbWordSearch.EndUpdate();
            }

            // Finds words that make up of letters inside the textbox.
            // Used try/catch to detect error from user input.
            else if (rbScrabble.Checked)
            {
                lbWordSearch.BeginUpdate();
                lbWordSearch.Items.Clear();
                List <string> wordScrabble = word.ScrabbleWord(txtUser.Text);
                try
                {
                    if (wordScrabble.Count == 0 || wordScrabble.Contains("") || string.IsNullOrWhiteSpace(txtUser.Text))
                    {
                        throw new Exception();
                    }
                    labelError.Visible = false;
                    foreach (string scrabble in wordScrabble)
                    {
                        if (scrabble != wordScrabble[0])
                        {
                            lbWordSearch.Items.Add(scrabble);
                        }
                    }
                }
                catch (Exception)
                {
                    labelError.Text    = "Invalid Input";
                    labelError.Visible = true;
                }
                // If no words are found, print out message
                lbWordSearch.EndUpdate();
            }

            // Find morph words of the word inside the textbox.
            // Used try/catch to detect error from user input.
            else if (rbMorphWord.Checked)
            {
                lbWordSearch.BeginUpdate();
                lbWordSearch.Items.Clear();
                string[] wordMorph = word.MorphWord(txtUser.Text);
                try
                {
                    // If no words are found, print out message
                    if (wordMorph.Length == 0 || string.IsNullOrEmpty(txtUser.Text))
                    {
                        throw new Exception();
                    }
                    labelError.Visible = false;
                    foreach (string morph in wordMorph)
                    {
                        lbWordSearch.Items.Add(morph);
                    }
                }
                catch (Exception)
                {
                    labelError.Text    = "Invalid Input";
                    labelError.Visible = true;
                }
                lbWordSearch.EndUpdate();
            }

            // Finds morph chain of start and end word with maximum chain length.
            // Used try/catch to detect error from user input.
            else if (rbMorphChain.Checked)
            {
                lbWordSearch.BeginUpdate();
                lbWordSearch.Items.Clear();
                try
                {
                    int           maxLength = Int32.Parse(tbMax.Text);
                    List <string> wordChain = word.MorphChain(txtUser.Text, tbEnd.Text, maxLength);
                    if (wordChain.Count == 0)
                    {
                        labelError.Text    = "No Solution";
                        labelError.Visible = true;
                    }
                    if (string.IsNullOrEmpty(txtUser.Text))
                    {
                        throw new Exception();
                    }
                    labelError.Visible = false;
                    foreach (string chain in wordChain)
                    {
                        lbWordSearch.Items.Add(chain);
                    }
                }
                catch
                {
                    labelError.Text    = "Invalid Input";
                    labelError.Visible = true;
                }
                lbWordSearch.EndUpdate();
            }
        }