comobobox选择项目索引不变

时间:2018-06-18 09:38:00

标签: vb.net indexing combobox

我用三个combobox1,2,3创建了一个简单的forme。 combobox1应该将combobox3返回到索引-1,但在为combobox3选择项目后,更改组合框1不会将组合框3更改为索引-1,谢谢。

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For i = 0 To 3
        ComboBox1.Items.Add(i.ToString)
        ComboBox2.Items.Add(i.ToString)
        ComboBox3.Items.Add(i.ToString)
    Next
    ComboBox2.Enabled = False
    ComboBox3.Enabled = False
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    ComboBox2.SelectedIndex() = -1
    ComboBox3.SelectedIndex() = -1
    ComboBox2.Enabled() = True
    ComboBox3.Enabled() = False
End Sub

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
    ComboBox3.Enabled() = True
    ComboBox3.Items.Clear()
    ComboBox3.Items.Add("0")
End Sub

结束班

1 个答案:

答案 0 :(得分:0)

实际上,它确实将Selectedindex更改为-1。如果您已正确调试并实际查看了SelectedIndex,那么您已经看到了这种情况。您基于用户界面进行了假设,但这只是显示Text。即使它将Text重置为-1,它也无法清除控件的SelectedIndex。这有点奇怪,但它就是它。

您也可以清除Text或者首先执行您应该做的事情,然后将DropDownStyle设置为DropDownList。如果你这样做,那么控件将永远不会显示除SelectedItem之外的文本,这是你希望ComboBox在大多数情况下要做的事情。除非您特别希望用户能够输入任意文本,否则请不要将DropDownStyle设置为默认DropDown

编辑:

实际上,我刚试过这段代码并且工作正常,即使每个DropDownStyle设置为DropDown

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For i = 0 To 3
        ComboBox1.Items.Add(i.ToString)
        ComboBox2.Items.Add(i.ToString)
        ComboBox3.Items.Add(i.ToString)
    Next
    ComboBox2.Enabled = False
    ComboBox3.Enabled = False
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    ComboBox2.SelectedIndex = -1
    ComboBox2.Enabled = True
End Sub

Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
    ComboBox3.SelectedIndex = -1
    ComboBox3.Enabled = (ComboBox2.SelectedIndex <> -1)
End Sub

无论如何,在[{1}}的事件处理程序中引用ComboBox3并没有任何理由。