简体   繁体   English

vb.net 组合框与另一个组合框相连

[英]vb.net combobox connected with another combobox

so i need help in this following code:所以我需要以下代码的帮助:

 Private Sub cbxkelas_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxkelas.SelectedIndexChanged
    Try
        Dim kel As String
        kel = cbxkelas.SelectedText.FirstOrDefault()
        If kel = "2" Or "4" Or "6" Then
            cbxsemes.SelectedItem = "Genap"
        Else
            cbxsemes.SelectedItem = "Ganjil"
        End If
        cbxsemes.Items.Clear()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

im trying to get a cbxsemex(combobox) value depending on the choice made at cbxkelas(combobox) first character but when i choose "1psi2" at cbxkelas it still show "Genap" in cbxsemes.我试图根据在 cbxkelas(combobox) 第一个字符上所做的选择来获取 cbxsemex(combobox) 值,但是当我在 cbxkelas 上选择“1psi2”时,它仍然在 cbxsemes 中显示“Genap”。

You have Option Strict set to OFF.您将Option Strict设置为 OFF。
This settings allows a syntax like the one in this line此设置允许使用类似于此行中的语法

 If kel = "2" Or "4" Or "6" Then

to pass through but in your context it makes no sense because the line is evaluated as通过但在您的上下文中它没有意义,因为该行被评估为

 If (kel = "2") Or ("4" Or "6") Then

and, with Option Strict set to OFF, an automatic conversion from the strings "4" and "6" to booleans happens.并且,在 Option Strict 设置为 OFF 的情况下,会发生从字符串“4”和“6”到布尔值的自动转换。 Of course this conversion gives back true as result (if the string was "0" you will get false)当然,此转换返回 true 作为结果(如果字符串为“0”,您将得到 false)

You code should be你的代码应该是

 If kel = "2" Or kel = "4" Or kel = "6" Then

So, my recommendation is to change Option Strict to ON.因此,我的建议是将 Option Strict 更改为 ON。 Probably, after this, your program will fail to compile correctly but it will also highlight a lot of other places where you should look carefully if you are really getting the result expected.可能在此之后,您的程序将无法正确编译,但它也会突出显示许多其他地方,如果您真的得到预期的结果,您应该仔细查看。

Still it is not clear why you remove all items from the same combobox that you have just set the selected item.仍然不清楚为什么要从刚刚设置所选项目的同一个组合框中删除所有项目。

您的情况必须更改为

If kel = "2" Or kel = "4" Or kel = "6" Then

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM