【问题标题】:Visual basic ComboBox.SelectedIndexVisual basic ComboBox.SelectedIndex
【发布时间】:2017-09-18 11:23:20
【问题描述】:

我开发了一个 vb.net 应用程序,但我遇到了组合框问题。

我知道我的组合框上的选定项目何时更改:

Private Sub ComboBoxSite_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxSite.SelectedIndexChanged
    If (ComboBoxSite.SelectedIndex <> 0) Then 'If it is not the default value
        Console.WriteLine("ActionListenerIndex = {0}", ComboBoxSite.SelectedIndex) 'To debug
        RequestAccesv2(0)
    End If
End Sub

还有 RequestAccessv2() 函数

Private Sub RequestAccesv2(taille As Integer)
    initBoxesLocation() 'A function that clear/refill 4 comboBoxes
    Console.WriteLine("SELECTED INDEX SITE : {0}", ComboBoxSite.SelectedIndex)
        Select Case taille
            Case 0 ..... 'Some database treatment

End Sub

输出上有结果,当调用第二个函数时,我没有相同的 selectedIndex :

ActionListenerIndex = 2
SELECTED INDEX SITE : -1 'Does it means thas nothing is selected ?

您是否已经/解决了这个问题?

问候, 法比恩

【问题讨论】:

  • initBoxesLocation 的代码是什么?您似乎以某种方式更改了该函数中的 SelectedIndex 。请edit您的问题添加该代码
  • 如果您“清除/重新填充组合框”,则所选项目将被删除,SelectedIndex 重置回-1
  • 实际上第一项在索引 0 处。所以当您从第二个索引更改为第一个索引时,If (ComboBoxSite.SelectedIndex &lt;&gt; 0) Then 不会通过。这是故意的吗?

标签: vb.net combobox selecteditem selectedindex


【解决方案1】:

数据索引是非负的。索引 -1 表示没有选择。如果要查找何时选择了有效索引,请检查 0 或更大。

Private Sub ComboBoxSite_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxSite.SelectedIndexChanged
    If (ComboBoxSite.SelectedIndex >= 0) Then 'If it is not the default value
        Console.WriteLine("ActionListenerIndex = {0}", ComboBoxSite.SelectedIndex) 'To debug
        RequestAccesv2(0)
    End If
End Sub

参见 MSDN:https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex(v=vs.110).aspx

ComboBox.SelectedIndex 属性

资产价值
类型:System.Int32 当前选定项的从零开始的索引。
如果没有选择任何项目,则返回负一 (-1) 值。

现在,您可能想忽略第一个值,然后使用ComboBoxSite.SelectedIndex &gt;= 1。但是,如果用户选择了第二个,那么第一个,你还想忽略它吗?

【讨论】:

    【解决方案2】:

    当没有选择任何项目时,将返回-1。这是通常检查的内容:

    Private Sub ComboBoxSite_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxSite.SelectedIndexChanged
        If (ComboBoxSite.SelectedIndex <> -1) Then ' If something is selected
            Console.WriteLine("ActionListenerIndex = {0}", ComboBoxSite.SelectedIndex) 'To debug
            RequestAccesv2(0)
        End If
    End Sub
    

    如果您在第一个插槽中有一个不应该被选中的值,那么您可以检查以确保它是 >= 1:

    Private Sub ComboBoxSite_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxSite.SelectedIndexChanged
        If (ComboBoxSite.SelectedIndex >= 1) Then ' If it is not the default value at index 0 (zero), and something is selected
            Console.WriteLine("ActionListenerIndex = {0}", ComboBoxSite.SelectedIndex) 'To debug
            RequestAccesv2(0)
        End If
    End Sub
    

    【讨论】:

      【解决方案3】:

      感谢您的回答!

      事实上,史蒂夫和一个朋友问题出在函数 initBoxesLocation 上。 在这个功能中,我清除了 4 个组合框,然后在每个组合框上添加了 1 个项目。

      我真的不明白问题出在哪里。

      编辑:是的,当然,一旦我的组合框被重新归档,我就没有再次选择一个项目,所以出现了问题。

      Private Sub initBoxesLocation()
          Console.WriteLine("initialisation entete")
          initBoxEnteteSite()
          initBoxEnteteBuilding()
          initBoxEnteteModule()
          initBoxEnteteRoom()
      End Sub
      

      我拆分了 initBoxesLocation() 函数,根据组合框的变化调用一个或另一个重置函数,实际上我不需要全部调用它们。

      现在可以了!

      向法比安致敬

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-01
        • 1970-01-01
        • 2014-11-29
        • 2014-07-31
        • 2015-01-09
        • 2019-05-23
        • 2014-06-06
        相关资源
        最近更新 更多