组合框返回-1对于SelectedIndex

时间:2015-03-04 17:40:43

标签: vb.net visual-studio-2010

我正在尝试选择一个组合框的选定索引。这工作得很好,然后突然它开始返回-1无论选择什么项目

我的代码是:

表单代码

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

参考模块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

当选择lev组合框项目“Pulsar”时,这会使组合框Man完全填满。然后,我希望我的用户单击按钮以生成一些标签和内容。代码是这样的:

按钮点击代码

 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只是检查所有方框是否已填写。我将表单中的信息传递给以下函数:

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

lev.selectedindex参数到达此函数时,它读取-1。即使用户已经选择了3个项目中的任何一个。任何人都可以解释为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

我刚试过你的代码。我得到了相同的结果(lev.SelectedIndex中的-1),这是因为当我击中Man或Units Combobox时,使用tab跳过控件我运行LevListAdd()然后清除Lev- ComboBox因为Form1.Lev.Items.Clear()

你应该考虑一下你的电话Man_SelectedIndexChanged_1,或者只是使用这样的话:

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

而不是

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

您应该将来自manunit组合框的电话分开。

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