如何将组合框项目转换为值并进行计算

时间:2018-11-28 05:22:50

标签: vb.net

我想将组合框中的所选项目转换成数字并计算出来。

当我尝试启动我的vb.net程序时,我收到了这样的消息。

  

从字符串“ <20”到类型'Integer'的转换无效

非常感谢您的帮助。

这是我的代码:

Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click

    Dim LHR As Integer
    Dim TipeRetak As Integer
    Dim LbRetak As Integer
    Dim LuKer As Integer
    Dim Alur As Integer
    Dim Tambal As Integer
    Dim Kasar As Integer
    Dim amblas As Integer

    Select Case ComboLHR.SelectedIndex
        Case "< 20 "
            LHR = 0
        Case "20 - 50"
            LHR = 1
        Case "50 - 200"
            LHR = 2
        Case "200 - 500"
            LHR = 3
        Case "500 - 2000"
            LHR = 4
        Case "2000 - 5000"
            LHR = 5
        Case "5000 - 20000"
            LHR = 6
        Case "20000 - 50000"
            LHR = 7
        Case "> 50000"
            LHR = 8
    End Select

    Select Case ComboTipeRetak.SelectedIndex
        Case "Buaya"
            TipeRetak = 5
        Case "Acak"
            TipeRetak = 4
        Case "Melintang"
            TipeRetak = 3
        Case "Memanjang"
            TipeRetak = 1
        Case "Tidak Ada"
            TipeRetak = 1
    End Select

    Select Case ComboLebarRetak.SelectedIndex
        Case "> 2 mm"
            LbRetak = 3
        Case "1 - 2 mm"
            LbRetak = 2
        Case "< 1 mm"
            LbRetak = 1
        Case "Tidak Ada"
            LbRetak = 0
    End Select

    Select Case ComboLuasKerusakan.SelectedIndex
        Case "> 30%"
            LuKer = 3
        Case "10 - 30%"
            LuKer = 2
        Case "< 10%"
            LuKer = 1
        Case "0"
            LuKer = 0
    End Select

    Select Case ComboKedalamanAlur.SelectedIndex
        Case "> 20 mm"
            Alur = 7
        Case "11 - 20 mm"
            Alur = 5
        Case "6 - 10 mm"
            Alur = 3
        Case "0 - 5 mm"
            Alur = 1
        Case "Tidak Ada"
            Alur = 0
    End Select

    Select Case ComboTambal.SelectedIndex
        Case ">30 %"
            Tambal = 3
        Case "20 - 30 %"
            Tambal = 2
        Case "10 - 20%"
            Tambal = 1
        Case "< 10%"
            Tambal = 0
    End Select

    Select Case ComboKekasaran.SelectedIndex
        Case "Desintegration"
            Kasar = 4
        Case "Pelepasan Butir"
            Kasar = 3
        Case "Rough(Hungry)"
            Kasar = 2
        Case "Fatty"
            Kasar = 1
        Case "Close Texture"
            Kasar = 0
    End Select

    Select Case ComboAmblas.SelectedIndex
        Case "> 5/100 m"
            amblas = 4
        Case "2 - 5/100 m"
            amblas = 2
        Case "0 - 2/100 m"
            amblas = 1
        Case "Tidak Ada"
            amblas = 0
    End Select

    Dim comand As New MySqlCommand("INSERT INTO `tb_bnkt`(`nomor`, `Nama`, `kondisi prioritas`) VALUES (@nomor,@NamaRuas,@kondisi)", Connector)
    comand.Parameters.Add("@nomor", MySqlDbType.VarChar).Value = TextNomor.Text
    comand.Parameters.Add("@NamaRuas", MySqlDbType.VarChar).Value = ComboNamaRuas.Text
    comand.Parameters.Add("@kondisi", MySqlDbType.VarChar).Value = 17 - (Val(LHR + TipeRetak + LbRetak + LuKer + Alur + Tambal + Kasar + amblas))

    If comand.ExecuteNonQuery() = 1 Then
        MessageBox.Show("Data disimpan")
        Loading()
        TextNomor.Clear()
        ComboNamaRuas.Text = String.Empty
        ComboLHR.Text = String.Empty
        ComboTipeRetak.Text = String.Empty
        ComboLebarRetak.Text = String.Empty
        ComboLuasKerusakan.Text = String.Empty
        ComboKedalamanAlur.Text = String.Empty
        ComboTambal.Text = String.Empty
        ComboKekasaran.Text = String.Empty
        ComboAmblas.Text = String.Empty
    Else
        MessageBox.Show("Error")
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

通过简单的类和数据绑定,您可以稍微简化代码,并摆脱当前的问题。

Public Class MyItem
    Public ReadOnly Name As String
    Public ReadOnly Value As Integer

    Public Sub New(name As String, value As Integer)
        Me.Name = name
        Me.Value = value
    End Sub
End Class

' Then in constructor create collection of values and bind it ot the combobox

Dim LHRValues As New List(Of MyItem) From
{
    New MyItem("< 20 ", 0),
    New MyItem("20 - 50", 1),
    New MyItem("50 - 200", 2),
    New MyItem("200 - 500", 3),
    New MyItem("500 - 2000", 4),
    New MyItem("2000 - 5000", 5),
    New MyItem("5000 - 20000", 6),
    New MyItem("20000 - 50000", 7),
    New MyItem("> 50000", 8)
}

ComboLHR.DisplayMember = "Name" ' Property Name will be used as a text
ComboLHR.ValueMember = "Value" ' Property Value will be used as a value
ComboLHR.DataSource = LHRValues

' Then in the code where you need selected value

Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
    Dim selectedLHR As Integer = DirectCast(ComboLHR.SelectedValue, Integer)
    ' Other selected values
End Sub

您只需要将ComboLHR.SelectedValue强制转换为期望的类型(Integer),因为SelectedValue的类型为object。希望您已将Option Strict设置为On