2017-04-21 109 views
1

我开发了一个vb.net应用程序,并且遇到了组合框的麻烦。Visual Basic ComboBox.SelectedIndex

我有这样的知道什么时候在我的组合框中选择的项目被改变:

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 ? 

你已经有/解决了这个问题?

问候, 法比安斯基

+0

_initBoxesLocation_的代码是什么?看起来,你以某种方式改变了该函数中的SelectedIndex。请编辑您的问题并添加代码 – Steve

+3

如果您“清除/重新填充组合框”,则选定的项目将被删除,并且“SelectedIndex”重置为“-1” –

+0

实际上,第一项是在索引0处。因此,如果(ComboBoxSite.SelectedIndex <> 0)Then'不会从第二个索引更改为第一个时通过。这是打算? – djv

回答

0

感谢您的回答!

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

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

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

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

我分裂initBoxesLocation()函数,通过调用改变一个或取决于组合框的其他复位功能,我需要的其实不是打电话给他们。

现在它的工作!

Regards Fabien

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)的值。

现在,您可能需要忽略第一个值,然后使用ComboBoxSite.SelectedIndex >= 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 

如果你在第一个插槽,不应该选择一个值,那么你可以检查,以确保它是> = 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