【问题标题】:Visual Basic 6 Runtime Error 381 "Invalid Property Array Index"Visual Basic 6 运行时错误 381“无效的属性数组索引”
【发布时间】:2012-11-28 21:39:20
【问题描述】:

我是一个编程菜鸟,尤其是在 Visual Basic 中。我只使用 VB6,因为我必须在大学使用它,而且我完全被卡住了。

我有一个列表框,我想在其中显示一个收音机的名称,然后当我单击该名称时,我希望它把数据放入一些文本框中,我知道这很简单,但我什至不完全了解 VB6语法所以我完全卡住了我已经问过我的老师,但他并没有真正的帮助。

这是我单击调试时突出显示的行:

x = radCatList.ItemData(radCatList.ListIndex)

这是整个表单的代码,同样非常简单,我几乎不知道我在做什么这个项目大部分是复制和粘贴工作:

Option Explicit

Private Sub Form_Load()
Dim r As radioRec
Dim radioChan As Integer
Dim x As Integer

x = 1
radioChan = FreeFile
Open radioFile For Random As radioChan Len = radioLen
Get radioChan, x, r
Do While Not EOF(radioChan)
    radCatList.AddItem r.rModel
    radCatList.ItemData(radCatList.NewIndex) = x
    x = x + 1
    Get radioChan, x, r
Loop
Close radioChan
End Sub

Private Sub radCatList_Click()
Dim r As radioRec
Dim radioChan As Integer
Dim x As Integer

radCatList.Clear

x = radCatList.ItemData(radCatList.ListIndex)
radioChan = FreeFile
Open radioFile For Random As radioChan Len = radioLen
Get radioChan, x, r
channelTxt = r.rLicense
licenseTxt = r.rLicense
rangeTxt = r.rRange
stockTxt.Text = r.rStock
Close radioChan
End Sub

【问题讨论】:

  • 当您必须首先为您使用的工具道歉时,它说明了很多关于社区的信息。
  • 他们还在教VB6???告诉他们在第三个千年加入其他所有人!

标签: arrays vb6


【解决方案1】:

您的列表索引可能是 -1,因为尚未选择任何列表项?

看看下面的代码

'1 form with
'    1 listbox : name=List1
Option Explicit

Private Sub Form_Load()
  Dim intIndex As Integer
  For intIndex = 0 To 10
    List1.AddItem CStr(intIndex)
    List1.ItemData(intIndex) = intIndex * intIndex
  Next intIndex
  ShowData List1.ListIndex
End Sub

Private Sub Form_Resize()
  List1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub

Private Sub List1_Click()
  ShowData List1.ListIndex
End Sub

Private Sub ShowData(intIndex As Integer)
  Dim strShow As String
  strShow = "Index:" & CStr(intIndex)
  If intIndex > -1 Then
    strShow = strShow & " Data:" & CStr(List1.ItemData(intIndex))
  End If
  Caption = strShow
End Sub

所以您只需检查 listindex 是否不是 -1

【讨论】:

  • 感谢您的帮助,但是在纠缠了我的老师之后,似乎我完全是个智障,我所要做的就是在选项中显式声明一个 int 来替换我列表中的 x代码,然后主要的罪魁祸首是(我不知道我怎么没弄清楚,因为我之前认为这是问题所在)行:radCatList.Clear
猜你喜欢
  • 2022-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多