【问题标题】:Populating folders in hierarchical listbox在分层列表框中填充文件夹
【发布时间】:2013-11-12 21:34:52
【问题描述】:

我正在用文件夹和文件填充分层列表框。 我用这些代码行填充我的列表框:

  For i As Integer = 1 To General.GetBlocksFolder.count
    If General.GetBlocksFolder.Item(i).Directory Then
       frmMain.BlockList.Append(General.GetBlocksFolder.Item(i).DisplayName, True)
       frmMain.BlockList.RowTag(i-1) = General.GetBlocksFolder.Item(i).GetSaveInfo(GetFolderItem(""))
    End if
  Next

General.GetBlocksFolder 是一个在我的系统上保存文件夹信息的对象。 BlockList 是在我的程序中显示“块”的列表。 这按预期工作,我看到了该列表中的文件夹。

然后,当我展开一行时,我使用以下代码:

  Dim ItemsAdded as integer
  Dim CurrentFolder As FolderItem = General.GetBlocksFolder.GetRelative(me.RowTag(row))

  For i As Integer = 1 To CurrentFolder.count
    ItemsAdded = ItemsAdded +1 
    If CurrentFolder.Item(i).Directory Then
      frmMain.BlockList.Append(CurrentFolder.Item(i).DisplayName, True)
      frmMain.BlockList.RowTag(i+ItemsAdded) = CurrentFolder.Item(i).GetSaveInfo(GetFolderItem(""))
    Else
      frmMain.BlockList.Append(CurrentFolder.Item(i).DisplayName)
    End if
  Next

这很好用,但是当我深入到 3 个级别时,我得到一个错误。 'CurrentRow' 上的 nilObjectException

有人知道这是什么魔法吗?

提前致谢 马蒂亚斯

【问题讨论】:

  • 很难说。 NilObjectException 发生在哪一行?我没有看到任何关于“CurrentRow”的信息。实际上 Nil 是什么?
  • 您的代码令人困惑。没有名为CurrentRow 的变量,ListBox 类也没有Append 方法。

标签: listbox hierarchical-data realbasic xojo


【解决方案1】:

您必须考虑 Item() 返回 nil。例如,如果文件在您处于循环中时被删除,或者当您遇到由于特殊类型或缺少权限而无法访问的项目时,就会出现这种情况。

因此,首先将 CurrentFolder.Item(i) 分配给一个变量,并在进一步使用它之前测试它是否为 nil,如下所示:

for i as integer = 1 to dir.count
  dim f as folderitem = dir.trueitem(i)
  if f = nil then continue // skip inaccessible items
  addrow ...
next

另外,您的代码中存在设计错误:

您应该使用 TrueItem() 函数,而不是 Item()。否则,您可能会在更深入时陷入无限循环,遇到符号链接或 Mac Finder 别名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多