【问题标题】:Populated ListView being treated as empty?填充的 ListView 被视为空?
【发布时间】:2014-05-09 00:00:55
【问题描述】:

我有一个确实填充的 ListView 控件。问题是它被视为空的。当指定我知道存在的项目的索引时,我收到“ArgumentOutOfRangeException”错误。

public static string folderToUpdate(string folderChanged)
    {
        Main m = new Main(); // This is the class which this method is in,
                             // as a side matter I don't even know why I have to create
                             // an instance of the class I'm ACTUALLY IN
                             // (but I get a compile time error if I don't).

        string folder1 = m.lstFoldersToSync.Items[0].Text;
        string folder2 = m.lstFoldersToSync.Items[1].Text;

        string folderToUpdate;

        // If the folder changed is folder1 then the folder to update must be folder2.
        // If the folder changed is folder2 then the folder to update must be folder1.
        if (folderChanged == folder1)
        {
            folderToUpdate = folder2;
        }
        else
        {
            folderToUpdate = folder1;
        }

        return folderToUpdate;
    }

错误出现在我声明和定义“folder1”的第 8 行。

一些澄清:我在发布之前进行了广泛的搜索。没有其他问题是完全重复的。有许多类似的问题,但解决方案总是被证明是 empty 列表问题或“Off-by-one 错误”,这些都不是我遇到的问题。我再次强调 Listview 已填充(在表单加载时)。

那会是什么?这个问题会不会和Main m = new Main();有关?

任何帮助将不胜感激。谢谢。

PS:我认为除了第 3、8 和 9 行之外的所有代码都几乎无关紧要(我可能错了)。

编辑:我通过使用包含“lstFoldersToSync”控件内容的静态字段列表解决了这个问题,然后访问它(而不是尝试直接访问控件的内容)。

新的工作代码:

    private static List<string> foldersToSync = new List<string>(); // This will be populated with the items in "lstFoldersToSync" control on "Main" form.

    public static string folderToUpdate(string folderChanged)
    {
       // Main m = new Main();

        string folder1 = foldersToSync[0];
        string folder2 = foldersToSync[1];

        string folderToUpdate;

        // If the folder changed is folder1 then the folder to update must be folder2.
        // If the folder changed is folder2 then the folder to update must be folder1.
        if (folderChanged == folder1)
        {
            folderToUpdate = folder2;
        }
        else
        {
            folderToUpdate = folder1;
        }

        return folderToUpdate;
    }

非常感谢所有帮助我的人。

【问题讨论】:

  • 你是故意使用静态函数吗?
  • 从哪里调用代码?一些事件?粘贴更多代码...
  • @attila 是的,它是静态的,因为我需要从另一个类访问它(而且我不喜欢实例化,我知道它不是最好的)。
  • @ZdravkoDanev 是一个辅助方法,用于确定需要更新哪个文件夹才能匹配另一个(但我认为它无关紧要)。
  • @GrantWinney "非静态字段方法或属性需要对象引用"

标签: c# .net


【解决方案1】:

是的,您的问题出在m = new Main() 部分。您需要以某种方式获取对您的表单的引用,而不是创建一个新表单。

要获取表单上选定的文件夹,最佳做法是在表单上创建一个属性。

【讨论】:

  • 您能详细说明一下吗?也许提供一些代码?谢谢。
  • 该方法是否存在于主窗体中?将其更改为不是 static 并从第 8 行和第 9 行中删除 m.
  • 这肯定会解决问题,除非我需要它是静态的,以便我可以从另一个类访问它(请参阅我对 attila 的回复)。
  • 不,它不应该是静态的,要从另一个类访问它,您需要对表单进行引用。你可以像她最后的回复一样:stackoverflow.com/questions/1000847/…
【解决方案2】:

因为它是静态的,所以您需要将对表单/窗口或控件的引用传递给该方法。

我会考虑重写该方法,这样您就不必传递对任何 UI 元素的引用。

public static string folderToUpdate(string folderChanged, List<string> foldersToSync)
{
    string folder1 = foldersToSync[0];
    string folder2 = foldersToSync[1];

    // Keep the rest the same
    ...
    ...
}

然后您可以这样称呼它,从您的ListView 发送文件夹名称列表:

yourClass.folderToUpdate("SomeFolderName",
                         lstFoldersToSync.Items.Select(i => i.Text);

【讨论】:

  • 那么“m”会在哪里实例化呢?
  • 但是我不能从另一个班级写“lstFoldersToSync”(如果我没有明确说明我在做什么)。我得到“名称'lstFoldersToSync'在当前上下文中不存在”它不存在,因为“lstFoldersToSync”在“Main”类中,我从不同调用“folderToUpdate”类。
  • 我该怎么做(“以某种方式获得你的 ListBox 对该类的引用”)?
  • 我有两个类,一个是“Main”,一个是“other”。在“Main”中我有“folderToUpdate”方法,在“other”中我有一个我想调用“folderToUpdate”的方法
  • 问题是对“folderToUpdate”的调用来自不同类中的方法,这就是为什么我将“folderToUpdate”方法设为静态,首先导致问题。
【解决方案3】:

我通过使用包含“lstFoldersToSync”控件内容的静态字段列表解决了这个问题,然后访问它(而不是尝试直接访问控件的内容)。

新的工作代码:

private static List<string> foldersToSync = new List<string>(); // This will be populated with the items in "lstFoldersToSync" control on "Main" form.

public static string folderToUpdate(string folderChanged)
{
   // Main m = new Main();

    string folder1 = foldersToSync[0];
    string folder2 = foldersToSync[1];

    string folderToUpdate;

    // If the folder changed is folder1 then the folder to update must be folder2.
    // If the folder changed is folder2 then the folder to update must be folder1.
    if (folderChanged == folder1)
    {
        folderToUpdate = folder2;
    }
    else
    {
        folderToUpdate = folder1;
    }

    return folderToUpdate;
}

非常感谢所有帮助我的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    相关资源
    最近更新 更多