0

这是我的课程,在每个文件选择后将文件添加到我的列表中,然后从更新我的 ListBox 并将文件添加到我的 ListBox 的主窗体引发事件中。

当我选择 2 个文件时,我可以看到(使用调试器)add method添加第一个文件和我的列表已更新,但在第二个文件通过 add 方法后,list.count 仍然为 1。

public class ListboxFile
{
    public delegate void OnFileAdd(string file);
    public event OnFileAdd OnFileAddEvent;
    private static List<string> _files;

    public ListboxFile()
    {
        _files = new List<string>();
    }

    public 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 List<string> list
    {
        get { return _files; }
    }
}

从主窗体(添加文件按钮单击):

private void btnAddfiles_Click(object sender, EventArgs e)
{
    #region file filter
    string fileToAdd = string.Empty;
    System.IO.Stream stream;
    OpenFileDialog thisDialog = new OpenFileDialog();
    thisDialog.InitialDirectory = (lastPath.Length > 0 ? lastPath : "c:\\");
    thisDialog.Filter = "(*.snoop, *.pcap, *.cap, *.net, *.pcapng, *.5vw, *.bfr, *.erf, *.tr1)" +
        "|*.snoop; *.pcap; *.cap; *.net; *.pcapng; *.5vw; *.bfr; *.erf; *.tr1|" + "All files (*.*)|*.*";
    thisDialog.FilterIndex = 1;
    thisDialog.RestoreDirectory = false;
    thisDialog.Multiselect = true;
    thisDialog.Title = "Please Select Source File";
    #endregion

    if (thisDialog.ShowDialog() == DialogResult.OK)
    {
        if (thisDialog.FileNames.Length > 0)
        {
            lastPath = Path.GetDirectoryName(thisDialog.FileNames[0]);
        }

        BackgroundWorker backgroundWorker = new BackgroundWorker();
        backgroundWorker.WorkerReportsProgress = true;
        backgroundWorker.DoWork +=
        (s3, e3) =>
        {
            foreach (String file in thisDialog.FileNames)
            {
                try
                {
                    if ((stream = thisDialog.OpenFile()) != null)
                    {
                        int numberOfFiles = thisDialog.SafeFileNames.Length;
                        using (stream)
                        {
                            ListboxFile lbf = new ListboxFile();
                            lbf.OnFileAddEvent += lbf_OnFileAddEvent;
                            lbf.checkFile(file);
                            lastPath = Path.GetDirectoryName(thisDialog.FileNames[0]);
                        }
                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        };

        backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
            (s3, e3) =>
            {

            });

        backgroundWorker.RunWorkerAsync();
    }
}

private void lbf_OnFileAddEvent(string file)
{
    if (InvokeRequired)
    {
        this.Invoke((MethodInvoker)delegate
        {                    
            listBoxFiles.Items.Add(file);
            , listBoxFiles.Items.Count.ToString("#,##0")));
            if (listBoxFiles.Items.Count != 0)
                listBoxFiles.SetSelected(listBoxFiles.Items.Count - 1, true);
        });
    }
    else
    {
        listBoxFiles.Items.Add(file);
        if (listBoxFiles.Items.Count != 0)
            listBoxFiles.SetSelected(listBoxFiles.Items.Count - 1, true);
    }
}
4

2 回答 2

2

您在 foreach 循环中声明并初始化 ListBoxFile 的实例。
在每个循环中,您都会重新初始化实例,因此您会丢失先前的添加

快速修复,将 ListboxFile 实例的声明和初始化移到循环之外(也是对事件的订阅)

.....
ListboxFile lbf = new ListboxFile();
lbf.OnFileAddEvent += lbf_OnFileAddEvent;
foreach (String file in thisDialog.FileNames)
{
  ..... 

顺便说一句,你打电话lbf.checkFile(file);,你的意思是lbf.Add(file)对的吗?

于 2013-05-23T20:00:09.130 回答
1

您正在调用lbf.checkFile(file);,但我在您的类定义中没有看到该方法。也许在那个方法里面你没有开火OnFileAddEvent

于 2013-05-23T20:03:04.663 回答