Esempio n. 1
0
        //Searches the files for the given word when button is clicked
        //Displays filename and result in a textbox
        private void btn_Search_Click(object sender, EventArgs e)
        {
            //When the search button is clicked, textbox will be cleared and new serach result displayed
            resultSearch.Text = string.Empty;

            SearchClass search = new SearchClass();

            if (word.Length > 0)
            {
                //Loop file/files to get filename and count of matches for the searched word
                for (int i = 0; i < SortedWords.Count; i++)
                {
                    var wordCount = search.MatchOnSearchedWord(SortedWords[i], word);
                    var fileName  = Path.GetFileNameWithoutExtension(readFiles[i].FileName);

                    resultSearch.Text += "\nThe searched word '" + word + "' was found " + wordCount + " times in File: " + fileName + " \r";
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(searchInputField.Text))
                {
                    MessageBox.Show("Please enter a valid input");
                    resultSearch.Text = string.Empty;
                }
                else
                {
                    MessageBox.Show("Please enter a valid input");
                    resultSearch.Text = string.Empty;
                }
            }
            //Cleares search textbox after each search
            searchInputField.Text = string.Empty;
            btn_Search.Enabled    = false;
        }
Esempio n. 2
0
        //Searches the files for the given word when button is clicked
        //Displays filename and result in a textbox
        private void BtnSearch_Click(object sender, EventArgs e)
        {
            //When the search button is clicked, textbox will be cleared and new serach result displayed
            resultSearch.Text = string.Empty;
            string resultString = string.Empty;

            SearchClass search = new SearchClass();

            Cursor.Current = Cursors.WaitCursor;
            if (searchInputField.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Please enter a valid input");
                searchInputField.Text = string.Empty;
                searchInputField.Focus();
                btn_Search.Enabled = false;
            }
            else if (word.Length > 0)
            {
                // List to sort for results
                List <SearchResult> resultList = new List <SearchResult>();

                //Loop file/files to get filename and count of matches for the searched word
                for (int i = 0; i < SortedWords.Count; i++)
                {
                    var wordCount = search.MatchOnSearchedWord(SortedWords[i], word);
                    var fileName  = Path.GetFileNameWithoutExtension(readFiles[i].FileName);

                    resultList.Add(new SearchResult(fileName, wordCount));
                }

                // Sort results using the standard CompareTo praxis
                resultList.Sort();

                // Iterate over the sorted results and write out
                foreach (var result in resultList)
                {
                    resultString += "\nThe searched word '" + word + "' was found " + result.MatchCount + " times in File: " + result.FileName + " \r";
                }
            }
            Cursor.Current    = Cursors.Default;
            resultSearch.Text = resultString;
            //Cleares search textbox after each search
            searchInputField.Text = string.Empty;
            btn_Search.Enabled    = false;
        }