Conversion from string "0-1" to type 'Double' is not valid error : VB.net

Conversion from string "0-1" to type 'Double' is not valid error : VB.net

我正在尝试创建网络浏览器 Visual Studio 2013,但我一直收到错误消息:

An exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll but was not handled in user code

Additional information: Conversion from string "0-1" to type 'Double' is not valid.

当我运行程序。

添加进度条后出现错误

我的代码:

Public Class Form1
Dim MyTemp As String = My.Settings.homepage

Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
    Me.Close()
End Sub

Private Sub AboutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem.Click
    MsgBox("Created by Lachlan Johnson" & vbCrLf & "                    (2016)", 0, "About")
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    WebBrowser1.Navigate(TextBox1.Text)
End Sub

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
        WebBrowser1.Navigate(TextBox1.Text)
        e.Handled = True
    End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    WebBrowser1.Navigate("https://www.google.com.au/webhp#q=" + TextBox2.Text)
End Sub
Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
        WebBrowser1.Navigate("https://www.google.com.au/webhp#q=" + TextBox2.Text)
        e.Handled = True
    End If
End Sub

Private Sub WebBrowser1_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
    ToolStripStatusLabel1.Text = "Loading..."
End Sub

Private Sub WebBrowser1_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
    ToolStripStatusLabel1.Text = "Complete"
End Sub

Private Sub SetAsHomepageToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SetAsHomepageToolStripMenuItem.Click
    My.Settings.homepage = WebBrowser1.Url.ToString
    My.Settings.Save()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    WebBrowser1.Navigate(MyTemp)
End Sub

Private Sub WebBrowser1_ProgressChanged(sender As Object, e As WebBrowserProgressChangedEventArgs) Handles WebBrowser1.ProgressChanged
    If Int(e.MaximumProgress > 0 & e.CurrentProgress > 0) Then
        ToolStripProgressBar1.ProgressBar.Value = e.CurrentProgress * 100 / e.MaximumProgress
    End If
End Sub
End Class

添加后出现错误:

Private Sub WebBrowser1_ProgressChanged(sender As Object, e As WebBrowserProgressChangedEventArgs) Handles WebBrowser1.ProgressChanged
    If Int(e.MaximumProgress > 0 & e.CurrentProgress > 0) Then
        ToolStripProgressBar1.ProgressBar.Value = e.CurrentProgress * 100 / e.MaximumProgress
    End If
End Sub

我似乎找不到问题,

非常感谢任何帮助。

拉克伦

问题是您在此处使用了字符串连接运算符:

If Int(e.MaximumProgress > 0 & e.CurrentProgress > 0) Then

应该是:

If e.MaximumProgress > 0.0 AndAlso e.CurrentProgress > 0.0 Then

这个

Int(e.MaximumProgress > 0 & e.CurrentProgress > 0)

执行 MaximumProgressCurrentProgress 的字符串连接。如果CurrentProgress-1(表示完成)且MaximumProgress为0(表示要传输的字节总数为0)则e.MaximumProgress > 0 & e.CurrentProgress > 0将得到字符串“0 -1”。然后 Int() 函数将其参数“0-1”转换为双精度数,因此它可以截断小数部分,而 return 仅截断整数部分。问题是,“0-1”不是有效的双精度数。