|
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
namespace FileFind
{
/// <summary>
/// User interface to display a list of folders and disk drives to be excluded
/// when a search is performed. The information is stored in ConfigInfo.
/// </summary>
public partial class IgnoreFolders : Form
{
private ConfigInfo configInfo = new ConfigInfo();
private bool isDirty = false; //determines if ConfigInfo class is to be updated
//when this form closes
private const string ILLEGALCHARACTERFOUND = "Selection characters (asterisk and question mark) are not allowed";
private Point formLocation = new Point(0, 0);
private bool formLocationSet = false;
public Point FormLocation
{
get { return formLocation; }
set { formLocation = value; formLocationSet = true; }
}
public IgnoreFolders()
{
InitializeComponent();
this.Font = configInfo.UseFont;
if (formLocationSet)
this.Location = formLocation;
}
void AddNewFolder(string newIgnore)
{
string str = newIgnore.Replace('\\', '/');
int index = listBox1.FindStringExact(str, -1); // Search starting from index -1:
if (index == -1)
{
listBox1.Items.Add(str);
isDirty = true;
}
} //ends void AddNewFolder(...
private void IgnoreFolders_Load(object sender, EventArgs e)
{
if (formLocationSet)
this.Location = formLocation;
this.Text = configInfo.ProductName + " - Exclude disk(s) or folder(s)";
msgLabel.Text = @"Examples: disk D:\ folder FolderName specific folder C:\Program Files";
//load the current ignore folders array
listBox1.BeginUpdate(); // Shutdown the painting of the ListBox
foreach (string str in configInfo.IgnoreFolders)
{ listBox1.Items.Add(str); }
listBox1.EndUpdate(); // Allow the ListBox to repaint and display the remaining items
textBox1.Select();
}
private void addButton_Click(object sender, EventArgs e)
{ //Add a new disk or folder
string newIgnore = textBox1.Text.Trim();
if (IllegalCharactersFound(newIgnore))
msgLabel.Text = ILLEGALCHARACTERFOUND;
else
{
AddNewFolder(newIgnore);
textBox1.Text =
msgLabel.Text = "";
}
textBox1.Focus();
}
private void deleteButton_Click(object sender, EventArgs e)
{ //Delete selected folders
if (-1 == listBox1.SelectedIndex)
return;
ArrayList remove_folders = new ArrayList();
listBox1.BeginUpdate(); // Shutdown the painting of the ListBox as items are deleted
for (int i = 0; i < listBox1.SelectedItems.Count; ++i)
remove_folders.Add(listBox1.SelectedItems[i].ToString());
//listBox1.Items.RemoveAt(listBox1.SelectedItems);
if (remove_folders.Count != 0)
isDirty = true;
foreach (string str in remove_folders)
listBox1.Items.Remove(str);
listBox1.EndUpdate(); // Allow the ListBox to repaint and display the remaining items
listBox1.Invalidate();
textBox1.Focus();
}
private void cancelButton_Click(object sender, EventArgs e)
{ //Cancel all changes
isDirty = false;
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void exitButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
private void defaultsButton_Click(object sender, EventArgs e)
{ //reset ignored folders to defaults
listBox1.Items.Clear();
ArrayList ignoreFolders = new ArrayList();
ignoreFolders = configInfo.DefaultFolders;
foreach (string str in ignoreFolders)
listBox1.Items.Add(str);
isDirty = true;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((char)Keys.Enter == e.KeyChar)
{ //treat as Add button
string newIgnore = textBox1.Text.Trim();
if (IllegalCharactersFound(newIgnore))
msgLabel.Text = ILLEGALCHARACTERFOUND;
else
{
AddNewFolder(textBox1.Text);
textBox1.Text =
msgLabel.Text = "";
e.Handled = true;
}
textBox1.Focus();
}
}
/// <summary>
/// Checks string for illegal characters
/// </summary>
/// <param name="newIgnore">string to check</param>
/// <returns>true if illegal character was found</returns>
private bool IllegalCharactersFound(string checkString)
{
if ((checkString.IndexOf("*") > -1) || (checkString.IndexOf("?") > -1))
return true;
return false;
}
private void IgnoreForm_Closing(object sender, FormClosingEventArgs e)
{
if (DialogResult.Cancel != this.DialogResult)
{
this.DialogResult = DialogResult.OK;
if (isDirty)
{
ArrayList saveExcludes = new ArrayList();
for (int i = 0; i < listBox1.Items.Count; ++i)
saveExcludes.Add(listBox1.Items[i].ToString());
configInfo.IgnoreFolders = saveExcludes;
}
}
}
}
}
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.