4

我是编程的菜鸟,尤其是在 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
4

1 回答 1

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

于 2012-11-28T10:30:45.183 回答