简体   繁体   中英

Combo Box Returning -1 For SelectedIndex

I'm trying to pick up a combo box's selected index. This was working absolutely fine, then all of a sudden it started returning -1 no matter what item is selected

My code is:

Form Code

Private Sub Man_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles Man.SelectedIndexChanged, Units.SelectedIndexChanged
        'Set Transducer Type
        Call References.LevListAdd()
    End Sub

References Module LevListAdd Sub

Public Sub LevListAdd()
        Form1.Lev.Items.Clear()
        If Form1.Man.Text = "Pulsar" Then
            With Form1.Lev.Items
                .Add("Ultra Range")
                .Add("IMP Range")
                .Add("Twin Range")
            End With
        End If
    End Sub

This fills the combo box lev fine when the Man combo box item "Pulsar" is selected. I then want my users to click a button to generate some labels and stuff. The code is as such:

Button Click Code

 Private Sub Generate_Click(sender As Object, e As EventArgs) Handles Generate.Click
        If CheckGenerate() = False Then Exit Sub
        Dim X = CheckGenerationType(Man.SelectedIndex, Lev.SelectedIndex, Level.Checked, Volume.Checked, ListBox1.SelectedIndex,
                                    Units.SelectedIndex)
        Call ParamPage(X)
    End Sub

CheckGenerate simply checks that all boxes have been filled in. I pass the informtion from the form to the following function:

Public Function CheckGenerationType(Man As Integer, Lev As Integer, Level As Boolean, Volume As Boolean, TankType As Integer,
                                    MeasurementUnit As Integer) As String
Dim M As Integer
Dim L As Integer
Dim CT As Integer
Dim TT As Integer
Dim Ms As Integer

M = Man
L = Lev
TT = TankType
Ms = MeasurementUnit

If Level = True Then
    CT = 0
ElseIf Volume = True Then
    CT = 1
End If

CheckGenerationType = CStr(M) + "." + CStr(L) + "." + CStr(CT) + "." + CStr(TT) + "." + CStr(Ms)

End Function

When the lev.selectedindex parameter arrives at this function, it reads -1. Even if the user has selected any of the 3 items. Can anyone explain why this is happening?

I've just tried your code. I get the same result (-1 in lev.SelectedIndex ) and this was because jumped using tab through the controls my when i'm hitting the Man or Units Combobox it runs the LevListAdd() and then clears the Lev-ComboBox because of Form1.Lev.Items.Clear() .

You should think about your call Man_SelectedIndexChanged_1 or maybe just use something like this:

Public Sub LevListAdd()
    If Form1.Man.Text = "Pulsar" Then
        Form1.Lev.Items.Clear()

instead of

Public Sub LevListAdd()
    Form1.Lev.Items.Clear()
    If Form1.Man.Text = "Pulsar" Then

And you should separate the calls from the man and unit ComboBoxes.

Private Sub Unit_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles Units.SelectedIndexChanged
    ' Do something
End Sub

Private Sub Man_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles Man.SelectedIndexChanged
    Form1.Lev.Items.Clear()

    Select Case Form1.Man.Text
        Case "Pulsar"
            With Form1.Lev.Items
                .Add("Ultra Range")
                .Add("IMP Range")
                .Add("Twin Range")
            End With

        Case "animals"
            With Form1.Lev.Items
                .Add("dogs")
                .Add("cats")
                .Add("birds")
            End With

    End Select
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM