JJES
JJES

Reputation: 41

Visual Basic 6 Runtime Error 381 "Invalid Property Array Index"

I am a noob to programming especially in Visual Basic. I am only using VB6 because I have to use it for college and I am totally stuck.

I have a listbox in which I want to display a name of a radio and then when I click the name I want it to put data into some textboxes, it is simple I know but I don't even fully know the VB6 syntax so I am totally stuck I have asked my teacher but he is not really any help.

This is the line that is highlited when I click debug:

x = radCatList.ItemData(radCatList.ListIndex)

This is code for the enitre form, again it is very simple and I barely know what I am doing most of this project is a copy and paste job:

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

Upvotes: 4

Views: 11680

Answers (1)

Hrqls
Hrqls

Reputation: 2951

your listindex is probably -1 as no listitem is selected yet ?

have look at the following code

'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

so all you have to add is a check if the listindex isn't -1

Upvotes: 4

Related Questions