Lachlan Jansen
Lachlan Jansen

Reputation: 55

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

Im trying to create a web browser is Visual Studio 2013 but i keep getting the error:

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.

when I run the program.

The error occurred after i added a progress bar.

My Code:

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

The error occured after i added this:

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

I cant seem to find the issue,

Any help is greatly appreciated.

Lachlan

Upvotes: 1

Views: 470

Answers (2)

Joels Elf
Joels Elf

Reputation: 784

This

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

Does a string concatenation of MaximumProgress and CurrentProgress. If CurrentProgress is -1 (indicating completion) and MaximumProgress is 0 (indicating the total number of bytes to be transferred is 0) then e.MaximumProgress > 0 & e.CurrentProgress > 0 will result in the string "0-1". Then the Int() function converts it's parameter, "0-1" into a double so it can truncate the decimal part and return just the integral part. The problem is, "0-1" isn't a valid double.

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54457

The issue is that you're using a string concatenation operator here:

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

That should be:

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

Upvotes: 2

Related Questions