首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ListBox中标记最后一项

在ListBox中标记最后一项
EN

Stack Overflow用户
提问于 2012-12-21 12:40:23
回答 1查看 212关注 0票数 0

在这段代码中,我将检查文件夹根中的所有文件,并且只将相关文件添加到我的Listbox中,我想要一个选项,它在每次迭代时标记我的Listbox中的最后一个项。

我认为应该使用下面的命令:ArgumentOutOfRangelistBoxFiles.SetSelected(listBoxFiles.Items.Count, true);,但是如果我每次将项目添加到列表框之后,就会收到listBoxFiles.SetSelected(listBoxFiles.Items.Count, true);错误。

代码语言:javascript
复制
private void btnAddDir_Click_1(object sender, EventArgs e)
{
    int totalCount = 0;
    int count = 0;
    string fileToAdd = string.Empty;
    List<string> filesList = new List<string>();
    BackgroundWorker backgroundWorker = null;
    DialogResult dialog = folderBrowserDialog1.ShowDialog();
    if (dialog == DialogResult.OK)
    {
        btnAddfiles.Enabled = false;
        btnAddDir.Enabled = false;
        btnPlay.Enabled = false;
        Editcap editcap = new Editcap();

        foreach (string file in SafeFileEnumerator.EnumerateFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories))
        {
            if (editcap.isWiresharkFormat(file))
            {
                filesList.Add(file);
                totalCount++;
            }
        }

        backgroundWorker = new BackgroundWorker();
        backgroundWorker.WorkerReportsProgress = true;
        backgroundWorker.DoWork +=
            (s1, e1) =>
            {
                foreach (string fileName in filesList)
                {
                    if (editcap.isWiresharkFormat(fileName))
                    {
                        if (editcap.isLibpcapFormat(fileName))
                        {
                            backgroundWorker.ReportProgress(0, fileName);
                            count++;
                        }
                        else if (!editcap.isLibpcapFormat(fileName))
                        {
                            fileToAdd = editcap.getNewFileName(fileName);
                            backgroundWorker.ReportProgress(0, fileToAdd);                                    
                            count++;
                        }

                        this.Invoke((MethodInvoker)delegate
                        {
                            labelStatus.Text = string.Format("Please wait..({0}/{1} files added)", count, totalCount);
                        });
                    }
                }
            };

        backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
        (s1, e1) =>
        {

        });

        backgroundWorker.ProgressChanged +=
         (s1, arguments) =>
         {
             listBoxFiles.Items.Add(arguments.UserState);
         };

        backgroundWorker.RunWorkerAsync();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-21 12:41:46

代码语言:javascript
复制
listBoxFiles.SetSelected(listBoxFiles.Items.Count - 1, true);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13990390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档