Lachlan Jansen Lachlan Jansen - 7 months ago 30
Vb.net Question

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

Answer

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