gpt4 book ai didi

arrays - Visual Basic 6 运行时错误 381 "Invalid Property Array Index"

转载 作者:行者123 更新时间:2023-12-04 23:40:50 29 4
gpt4 key购买 nike

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

我有一个列表框,我想在其中显示 radio 的名称,然后当我单击该名称时,我希望它将数据放入一些文本框中,这很简单,我知道但我什至不完全了解 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

最佳答案

您的 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

关于arrays - Visual Basic 6 运行时错误 381 "Invalid Property Array Index",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13602584/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com