I want to separate the code inside my Add button click event
from the code in order to make my code looks more arranged.
this is my code before these changes (i am using BackgroundWorker in order to avoid my GUI to freeze because every file that I am choosing need to open process and check if this is file OK before add this file into my Listbox):
private void btnAddfiles_Click(object sender, EventArgs e)
{
System.IO.Stream stream;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileSelected();
if (openFileDialog1.FileNames.Length > 0)
lastPath = Path.GetDirectoryName(openFileDialog1.FileNames[0]);
ListboxFile lbf = new ListboxFile();
lbf.OnFileAddEvent += lbf_OnFileAddEvent;
BackgroundWorker backgroundWorker = new BackgroundWorker();
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.DoWork +=
(s3, e3) =>
{
foreach (String file in openFileDialog1.FileNames)
{
try
{
if ((stream = openFileDialog1.OpenFile()) != null)
{
int numberOfFiles = openFileDialog1.SafeFileNames.Length;
using (stream)
{
lbf.checkFile(file);
lastPath = Path.GetDirectoryName(openFileDialog1.FileNames[0]);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
};
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
(s3, e3) =>
{
//update my gui
});
backgroundWorker.RunWorkerAsync();
}
}
and this is after the changes:
private void btnAddfiles_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileSelected();
}
}
private void fileSelected()
{
if (openFileDialog1.FileNames.Length > 0)
lastPath = Path.GetDirectoryName(openFileDialog1.FileNames[0]);
ListboxFile lbf = new ListboxFile();
lbf.OnFileAddEvent += lbf_OnFileAddEvent;
BackgroundWorker backgroundWorker = new BackgroundWorker();
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.DoWork +=
(s3, e3) =>
{
foreach (String file in openFileDialog1.FileNames)
{
System.IO.Stream stream;
try
{
if ((stream = openFileDialog1.OpenFile()) != null)
{
int numberOfFiles = openFileDialog1.SafeFileNames.Length;
using (stream)
{
lbf.checkFile(file);
lastPath = Path.GetDirectoryName(openFileDialog1.FileNames[0]);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
};
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
(s3, e3) =>
{
//update my gui
});
backgroundWorker.RunWorkerAsync();
}
So, what I did is necessary ? Maybe something else was better ?
public class ListboxFile
{
public delegate void OnFileAdd(string file);
public event OnFileAdd OnFileAddEvent;
private static List<string> _files;
public ListboxFile()
{
_files = new List<string>();
}
private void add(string file)
{
_files.Add(file);
OnFileAddEvent(file);
}
public void remove(string file)
{
if (_files.Contains(file))
{
_files.Remove(file);
}
}
public void clear()
{
_files.Clear();
}
public void checkFile(string file)
{
if ((new Editcap()).isLibpcapFormat(file))
{
add(file);
}
}
public List<string> list
{
get { return _files; }
}
}
Editcap.isLibpcapFormat
. Also looks like you might want to make it static. \$\endgroup\$