我是编程特别是在Visual Basic中的菜鸟.我只使用VB6,因为我必须将它用于大学,我完全陷入困境. 我有一个列表框,我想在其中显示一个收音机的名称然后当我点击我希望它将数据放入一些文
我有一个列表框,我想在其中显示一个收音机的名称然后当我点击我希望它将数据放入一些文本框的名称时,我知道这很简单,但我甚至不完全了解VB6语法所以我我完全陷入困境我问过我的老师,但他并没有任何帮助.
这是单击调试时高亮显示的行:
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你的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