Vb.net 在列表框中双击操作时,如何修复“从字符串“Num1”到类型“integer”的转换无效”

Vb.net 在列表框中双击操作时,如何修复“从字符串“Num1”到类型“integer”的转换无效”,vb.net,Vb.net,我对VB.Net还比较陌生,还是个学生,请容忍我。当我双击列表框中的一个操作ex:1+1=2时,我得到了一个从字符串到整型的转换错误。 我正在保存/从中提取的access数据库Calc->My Calc表的字段设置为短文本,因此我不明白如何得到此错误。其他一切都按照我的计划进行 我在输入端和输出端都尝试了几乎所有的转换 Option Explicit On Option Strict On Option Infer Off Imports System.Data.OleDb Public C

我对VB.Net还比较陌生,还是个学生,请容忍我。当我双击列表框中的一个操作ex:1+1=2时,我得到了一个从字符串到整型的转换错误。 我正在保存/从中提取的access数据库Calc->My Calc表的字段设置为短文本,因此我不明白如何得到此错误。其他一切都按照我的计划进行

我在输入端和输出端都尝试了几乎所有的转换

Option Explicit On
Option Strict On
Option Infer Off

Imports System.Data.OleDb

Public Class MainForm
    ' declare variables
    Dim count As Integer = 0

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        ' declare variables
        Dim result As Decimal = 0
        Dim addMath As MathOp = New MathOp()

        ' load text entries into class
        addMath.decNum1 = CDec(txtNum1.Text)
        addMath.decNum2 = CDec(txtNum2.Text)

        ' calculate and display addition of the numbers
        result = addMath.addNum()
        addMath.decResult = result

        txtResult.Text = addMath.decNum1 & " + " & addMath.decNum2 & " = " & addMath.decResult
        Me.btnSave.Focus()
    End Sub

    Private Sub btnSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSub.Click

        ' declare variables
        Dim result As Decimal = 0
        Dim subMath As MathOp = New MathOp()

        ' load text entries into class
        subMath.decNum1 = CDec(txtNum1.Text)
        subMath.decNum2 = CDec(txtNum2.Text)

        ' calculate and display the subtraction of the numbers
        result = subMath.subNum()
        subMath.decResult = result
        txtResult.Text = subMath.decNum1 & " - " & subMath.decNum2 & " = " & subMath.decResult
        Me.btnSave.Focus()
    End Sub

    Private Sub btnMult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMult.Click

        ' declare variables
        Dim result As Decimal = 0
        Dim multMath As MathOp2 = New MathOp2()

        ' load text entries into class
        multMath.decNum1 = CDec(txtNum1.Text)
        multMath.decNum2 = CDec(txtNum2.Text)

        ' calculate and display the multiplcation of the numbers
        result = multMath.multNum()
        multMath.decResult = result
        txtResult.Text = multMath.decNum1 & " x " & multMath.decNum2 & " = " & multMath.decResult
        Me.btnSave.Focus()
    End Sub

    Private Sub btnDiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDiv.Click

        ' declare variables
        Dim result As Decimal = 0
        Dim divMath As MathOp2 = New MathOp2()

        ' load text entries into cloass
        divMath.decNum1 = CDec(txtNum1.Text)
        divMath.decNum2 = CDec(txtNum2.Text)

        ' divides the 1st & 2nd number unless the 2nd number is a 0, then will display message box
        ' to user telling them they can't divide by 0
        Try
            result = divMath.divNum()
        Catch ex As DivideByZeroException When divMath.decNum2 = 0
            MessageBox.Show("Sorry, You can't divide by 0.")
            txtNum2.Focus()
        End Try

        ' displays the results in the results text box, with only 2 decimal places, unless the 2nd number is a
        ' 0, then it will display the error in the results text box by utilizing a IF-THEN-ELSE statement
        If divMath.decNum2 = 0 Then
            txtResult.Text = "Can't divide by 0"
        Else
            divMath.decResult = result
            txtResult.Text = divMath.decNum1 & " / " & divMath.decNum2 & " = " & divMath.decResult.ToString("N2")
            Me.btnSave.Focus()
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        Dim ask As MsgBoxResult = MsgBox("This will clear all your records, would you like to continue?", MsgBoxStyle.YesNo,
                                         "Clear History?")
        If ask = MsgBoxResult.Yes Then
            Using con As New OleDb.OleDbConnection
                con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Users\thiem\Desktop\Thieme_MyCalc 3\Thieme_MyCalc 3\Calc.mdb'"
                con.Open()

                Dim data As String = "DELETE * FROM MyCalc"
                Dim dataInsert As New OleDbCommand

                With dataInsert
                    .CommandText = data
                    .Connection = con
                    .ExecuteNonQuery()
                End With
                con.Close()
            End Using

            ' clear all boxes
            lstOutput.DataSource = Nothing
            lstOutput.Items.Clear()
            txtNum1.Clear()
            txtNum2.Clear()
            txtResult.Clear()
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If (count < 10) Then
            Using con As New OleDb.OleDbConnection
                con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Users\thiem\Desktop\Thieme_MyCalc 3\Thieme_MyCalc 3\Calc.mdb'"
                con.Open()

                Dim data As String = "INSERT INTO MyCalc (Num1, Num2, Result) VALUES (@Num1, @Num2, @Result);"

                Dim dataInsert As New OleDbCommand

                With dataInsert
                    .Parameters.AddWithValue("@Num1", txtNum1.Text)
                    .Parameters.AddWithValue("@Num2", txtNum2.Text)
                    .Parameters.AddWithValue("@email", txtResult.Text)
                    .CommandText = data
                    .Connection = con
                    .ExecuteNonQuery()
                End With

                con.Close()

            End Using

            MessageBox.Show("Result Saved")
            count = count + 1
        Else
            MessageBox.Show("Sorry, you have exceeded 10 entries, press Clear.")
        End If
        Me.btnDisplay.Focus()
    End Sub

    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

        ' declare variables
        Dim con As New OleDbConnection()
        Dim ds As New DataSet
        Dim dt As New DataTable
        ds.Tables.Add(dt)
        Dim da As New OleDbDataAdapter

        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Users\thiem\Desktop\Thieme_MyCalc 3\Thieme_MyCalc 3\Calc.mdb'"
        con.Open()
        da = New OleDbDataAdapter("SELECT * FROM MyCalc", con)
        da.Fill(dt)
        lstOutput.DataSource = dt.DefaultView
        lstOutput.DisplayMember = "Result"

        Me.txtNum1.Focus()

    End Sub

    Private Sub lstOutput_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles lstOutput.MouseDoubleClick

        Dim con As New OleDbConnection()
        Dim comm As New OleDbCommand()
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Users\thiem\Desktop\Thieme_MyCalc 3\Thieme_MyCalc 3\Calc.mdb'"

        Dim reader As OleDbDataReader
        Try
            con.Open()
            Dim data As String = "SELECT * FROM MyCalc where Result = '" & lstOutput.Text & "'"

            comm = New OleDbCommand(data, con)
            reader = comm.ExecuteReader
            While reader.Read
                txtNum1.Text = reader.GetString(CInt("Num1")) 'CInt suggested by Visual Studio
                txtNum2.Text = reader.GetString(CInt("Num2")) 'CInt suggested by Visual Studio
                txtResult.Text = reader.GetString(CInt("Result")) 'CInt suggested by Visual Studio
            End While

            con.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        Finally
            con.Dispose()
        End Try

    End Sub

    Private Sub input_validation(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles txtNum1.KeyPress, txtNum2.KeyPress

        ' only allow numbers, decimal, and backspace in textboxes
        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> "." AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If

    End Sub

    Private Sub txt_Enter(sender As Object, e As EventArgs) Handles txtNum1.Enter, txtNum2.Enter

        ' select all the text when entering a textbox
        txtNum1.SelectAll()
        txtNum2.SelectAll()

    End Sub

End Class

总之,在做了进一步的研究后发现了这个问题,并找到了一篇大约5年前的帖子stackoverflow.com/questions/23042273/…。基本上,通过以下方式修复:

        While reader.Read
            txtNum1.Text = CType(reader("Num1"), String)
            txtNum2.Text = CType(reader("Num2"), String)
            txtResult.Text = CType(reader("Result"), String)
        End While

这篇文章很有帮助,因为它说要删除.GetString,然后VisualStudio可以建议对上面的代码进行编辑。。。想发布它,以防其他人也有同样的问题。

您好,欢迎光临,您需要将输入文本转换为小数,如:num=CDecTextBox1.text您要做的第一件事就是在Visual Studio配置中默认设置Option Strict和Option Explicit-ON,因为这是一个生死攸关的问题。-请注意,不要在这里转储所有代码,只转储重现问题的部分。@Jimi感谢您的反馈,这是我在这里的第一篇文章,我不确定问题到底出在哪里。我确实实现了选项Strict和Option Explicit,这在以前的课程中已经强调过,我只是忽略了在这里执行,它帮助我找到了一些需要解决的问题。@minimalist感谢您的回复。我实现了你所说的转换。当我添加选项Strict/Explicit ON时,VisualStudio警告我此错误,您的更正起到了帮助作用。然而,在所有的更改之后,我仍然得到转换错误。
Public Class MathOp2

    Inherits MathOp

    ' create function to multiply numbers
    Public Function multNum() As Decimal
        Return decNum1 * decNum2
    End Function

    ' create function to divide numbers
    Public Function divNum() As Decimal
        Return decNum1 / decNum2
    End Function

End Class
        While reader.Read
            txtNum1.Text = CType(reader("Num1"), String)
            txtNum2.Text = CType(reader("Num2"), String)
            txtResult.Text = CType(reader("Result"), String)
        End While