Click here to Skip to main content
15,883,705 members
Articles / Programming Languages / C#

Source Code Super Search

Rate me:
Please Sign up or sign in to vote.
4.71/5 (22 votes)
10 Mar 2009CPOL8 min read 52.3K   1.5K   66  
A simple solution for searching source code directories
// BaileySoft Super Code Search
// Copyright (c) 2008, BSLTD
// nealbailey@hotmail.com - All rights reserved.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BSCodeGrep
{
    public partial class Form2 : Form
    {
        #region constructors

        public Form2()
        {
            InitializeComponent();
            m_uso = new IOWorker.SearchSettings();
            LoadUserSettings();
        }

        #endregion

        #region public properties

        public IOWorker.SearchSettings Uso
        {
            get { return m_uso; }
        }
        #endregion

        #region private methods

        private void LoadUserSettings()
        {
            _appSettings = new CAppSettings();
            _appSettings.Reload();

            _appSettings.SearchPaths.Reverse();
            _appSettings.SearchFilters.Reverse();
            _appSettings.SearchPatterns.Reverse();
            _appSettings.ReplacementStrings.Reverse();

            comboBox1.Items.AddRange(_appSettings.SearchPaths.ToArray());            
            comboBox2.Items.AddRange(_appSettings.SearchFilters.ToArray());
            comboBox3.Items.AddRange(_appSettings.SearchPatterns.ToArray());
            comboBox4.Items.AddRange(_appSettings.ReplacementStrings.ToArray());
        }

        private bool ValidateAndPersistControls()
        {
            bool isValid = false;

            if (comboBox1.Text != "" || comboBox2.Text != "" || comboBox3.Text != "")
            {

                if (!comboBox1.Items.Contains(comboBox1.Text) && !String.IsNullOrEmpty(comboBox1.Text))
                {
                    comboBox1.Items.Insert(0, comboBox1.Text);
                    _appSettings.SearchPaths.Add(comboBox1.Text);
                    if (comboBox1.Text.Substring(comboBox1.Text.Length - 1) == "\\")
                        comboBox1.Text = comboBox1.Text.Remove(comboBox1.Text.Length - 1);
                }

                if (!comboBox2.Items.Contains(comboBox2.Text) && !String.IsNullOrEmpty(comboBox2.Text))
                {
                    comboBox2.Items.Insert(0, comboBox2.Text);
                    _appSettings.SearchFilters.Add(comboBox2.Text);
                }

                if (!comboBox3.Items.Contains(comboBox3.Text) && !String.IsNullOrEmpty(comboBox3.Text))
                {
                    comboBox3.Items.Insert(0, comboBox3.Text);
                    _appSettings.SearchPatterns.Add(comboBox3.Text);
                }

                if (!comboBox4.Items.Contains(comboBox4.Text) && !String.IsNullOrEmpty(comboBox4.Text))
                {
                    comboBox4.Items.Insert(0, comboBox4.Text);
                    _appSettings.ReplacementStrings.Add(comboBox4.Text);
                }               
                
                _appSettings.Save();
                isValid = true;
            }    
            return isValid;
        }

        #endregion

        #region private events

        //Browse Folder Dialog Button 
        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
            folderBrowserDialog1.Description = "Starting Search Location";
            folderBrowserDialog1.ShowNewFolderButton = false;
            DialogResult result = folderBrowserDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {
                comboBox1.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        //Apply Button Clicked
        private void button2_Click(object sender, EventArgs e)
        {
            if (ValidateAndPersistControls())
            {
                string[] filters;

                if (comboBox2.Text.IndexOf(" ") > -1)
                    filters = comboBox2.Text.Split(' ');
                else
                    filters = new string[] { comboBox2.Text };

                m_uso.SearchPath = comboBox1.Text;
                m_uso.SearchFilters = filters;
                m_uso.SearchPattern = comboBox3.Text;
                m_uso.NamesOnly = false;
                m_uso.CaseInsensative = false;
                m_uso.Replacement = comboBox4.Text;
                m_uso.Scope = IOWorker.RegExSearchScope.SINGLE_LINE;

                if (checkBox1.Checked)
                    m_uso.CaseInsensative = true;

                if (checkBox2.Checked)
                    m_uso.NamesOnly = true;

                if (checkBox3.Checked)
                       m_uso.Scope = IOWorker.RegExSearchScope.MULTI_LINE;
            }
            this.Hide();
        }

        //Clear User Settings History
        private void button3_Click(object sender, EventArgs e)
        {
            _appSettings.SearchPaths.Clear();
            _appSettings.SearchFilters.Clear();
            _appSettings.SearchPatterns.Clear();
            _appSettings.ReplacementStrings.Clear();
            _appSettings.Save();

            comboBox1.Items.Clear();
            comboBox2.Items.Clear();
            comboBox3.Items.Clear();

            comboBox1.Text = "";
            comboBox2.Text = "";
            comboBox3.Text = "";
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                checkBox3.Checked = false;
                checkBox4.Checked = false;
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                checkBox4.Checked = false;
                checkBox2.Checked = false;
            }
        }
        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked)
            {
                checkBox2.Checked = false;
                checkBox3.Checked = false;
            }
        }      
        #endregion

        #region private fields

        private CAppSettings _appSettings = null;
        private IOWorker.SearchSettings m_uso;
        #endregion
       
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I'm a professional .NET software developer and proud military veteran. I've been in the software business for 20+ years now and if there's one lesson I have learned over the years, its that in this industry you have to be prepared to be humbled from time to time and never stop learning!

Comments and Discussions