Visual Basic ComboBox.SelectedIndex

时间:2017-04-21 12:42:22

标签: vb.net combobox selecteditem selectedindex

我开发了一个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 ?

你已经解决了这个问题吗?

此致 法比安

3 个答案:

答案 0 :(得分:0)

数据索引是非负数。索引-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(-1)。

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

答案 1 :(得分:0)

如果未选择任何项目,将返回-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

如果您在第一个广告位中有一个不应该被选中的值,那么您可以检查以确保它是&gt; = 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

答案 2 :(得分:0)

感谢您的回答!

Steve和A Friend确实问题来自函数initBoxesLocation。 在这个功能中我清理了4个组合框,然后我在每个组合中添加了1个项目。

我没有真正理解问题出在哪里。

编辑:是的,当然,一旦我的组合框重新复制,我没有再次选择一个项目,所以存在问题。

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:paddingLeft="@dimen/paddinfleftchatpage_"
 android:paddingRight="@dimen/padding_right_chat_page"
 android:layout_height="match_parent">
 <LinearLayout
    android:id="@+id/SenderLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/SenderMesssage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/layout_marginTop_chat_page"
        android:layout_weight="6"
        android:background="@drawable/rounded_corner"
        android:padding="@dimen/text_view_padding"
        android:text="Android charting application xml ui design tutorial 
 with example. Android charting application xml ui design tutorial with 
example. Android charting application xml ui design tutorial with example. 
Android charting application xml ui design tutorial with example."
        android:textColor="#000" />



   </LinearLayout>
    <LinearLayout
    android:id="@+id/time"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:id="@+id/timeOfmessage"
        android:text="5:25 am"/>
    </LinearLayout>
  </LinearLayout>

我拆分initBoxesLocation()函数,通过调用一个或另一个复位函数,取决于组合框的改变,我实际上不需要全部调用它们。

现在它有效!

关心Fabien