首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么ToolStripItem不能处理大数字?

为什么ToolStripItem不能处理大数字?
EN

Stack Overflow用户
提问于 2013-05-20 18:50:00
回答 1查看 469关注 0票数 0

我有一个递归函数来搜索文件夹。

代码语言:javascript
复制
    private int contFiles = 0;
    private List<string> GetFiles(string folder, string filter)
    {
        var files = new List<string>();
        Action<string> getFilesInDir = null;
        getFilesInDir = new Action<string>(dir =>
        {
            contFiles++;
            tslQuant.Text = contFiles.ToString(); //ToolStripItem
            try
            {
                // get all the files in this directory
                files.AddRange(Directory.GetFiles(dir, filter));                   
                // and recursively visit the directories
                foreach (var subdir in Directory.GetDirectories(dir))
                {
                    getFilesInDir(subdir);
                }
            }
            catch (UnauthorizedAccessException uae)
            {
                Console.WriteLine(uae.Message);
            }
        });
        getFilesInDir(folder);
        return files;
    }

该函数增加contFiles,并将该数字设置为ToolStripItem,但我总是得到"System.ArgumentOutOfRangeException“。

如何增加此值(最多5000)并在TSI?中显示

错误:

System.ArgumentOutOfRangeException是未处理的Message=“索引超出了范围。它必须是非负的并且小于集合的大小。参数名称:索引”。

System.Windows.Forms.ToolStripItemCollection.get_Item(Int32索引中的StackTrace:在System.Collections.ArrayList.get_Item中(Int32索引)在System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e中),在System.Windows.Forms.ToolStrip.WndProc(Message& m中的System.Windows.Forms.Control.WmPaint(Message& m),在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m中的System.Windows.Forms.Control.WmPaint(Message& m) )中的System.Windows.Forms.Control.WmPaint(Message& m),在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m中的System.Windows.Forms.Control.WmPaint(Message& m)中的Int16层),在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd中的System.Windows.Forms.Control.WmPaint(Message& m)中的Int16层(Message&m)中的Int16层),在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd中的System.Windows.Forms.Control.WmPaint(Message&m)中的(Message&m)中的Int16层(Message&m)中的Int16层(Message&m)中的Int16层),在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd中的System.Windows.Forms.Control.WmPaint(Message&m)中的(Message&m)中的Int16层(Message&m)中的(Message&m)中的Int16层),在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd中的System.Windows.Forms.Control.WmPaint(Message&m)中的(Message&m)中的(Message&m)

编辑

在阅读了整个程序的代码之后,我注意到Fun广子是在Do_Work中扩展的,所以我使用了

代码语言:javascript
复制
backgroundWorker2.ReportProgress((1));

去报到,一切都正常。

我不知道为什么,但不知怎么的,toolStripItem甚至可以在backgroundWorker、标签和其他控件中访问。

EN

回答 1

Stack Overflow用户

发布于 2013-05-20 19:08:42

您似乎从未将contFiles变量重置为零。如果你给GetFiles()打了几次电话,你可能会遇到麻烦。所以你可以在这里把它重新设置为零

代码语言:javascript
复制
    contFiles = 0;    // Reset variable to prevent overflow 
    var files = new List<string>();
    Action<string> getFilesInDir = null;
    getFilesInDir = new Action<string>(dir =>
    { ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16656308

复制
相关文章

相似问题

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