Visual Basic 6运行时错误381“无效的属性数组索引”

时间:2012-11-28 10:10:17

标签: arrays vb6

我是编程的菜鸟,特别是在Visual Basic中。我只使用VB6,因为我必须将它用于大学,我完全陷入困境。

我有一个列表框,我想在其中显示收音机的名称,然后当我点击我希望它将数据放入某些文本框的名称时,我知道这很简单,但我甚至都不知道VB6的语法,所以我完全陷入困境,我问过我的老师但是他没有任何帮助。

这是单击debug时高亮显示的行:

x = radCatList.ItemData(radCatList.ListIndex)

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

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

1 个答案:

答案 0 :(得分:4)

你的listindex可能是-1,因为还没有选择listitem吗?

查看以下代码

'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