WebBrowser1.Navigate loop request - vb.net

How can i make this code to loop urls continuously in Webbrowser1, after run trough these links one time.
This is the code i have so far, but when i run this, the Webbrowser stop with last url, wich is google.com. What i want to do is when the last url is reached i want it to start from top again, and run trough links again and again, until i exit the program.
Public Class Form1
Private WithEvents backgroundWorker1 As System.ComponentModel.BackgroundWorker
Dim l As New List(Of String)
Dim flagDC As Boolean = False
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Hide()
Button3.Show()
l = New List(Of String)
l.Add("http://bbc.com/sipeu/php/zq.php")
l.Add("http://google.com")
l.Add("http://DCWC.com/sipeu/us.php")
l.Add("http://google.com")
l.Add("http://example.com/sipeu/v.php")
l.Add("http://google.com")
backgroundWorker1 = New System.ComponentModel.BackgroundWorker
backgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object,
ByVal e As System.ComponentModel.DoWorkEventArgs) _
Handles backgroundWorker1.DoWork
For Each www In l
flagDC = False
WebBrowser1.Navigate(www)
Do
Loop While Not flagDC
Next
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
System.Threading.Thread.Sleep(1000)
flagDC = True
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Button3.Hide()
ChangeUserAgent("troll001_v2-agent34")
WebBrowser1.Navigate("http://example.com/welcome.php")
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Close()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs)
End Sub

Related

How do I get the parent form from a child form in VB.net?

I have a VB form which may be called by a number of forms. Upon exiting this child form, I want to restore the calling form but I cannot seem to figure out how to determine which form called it. Any help would be greatly appreciated.
You can create a tagging for parent forms (maybe a global array) which you will update the value every time you open a child form. You just have to remember which form is which on the tagging you will make.
Maybe you can try this-
Module with form tagging:
Module Module1
Public parentForms(2) As String
'parentForms(0) - parent form of Form 1
'parentForms(1) - parent form of Form 2
'parentForms(2) - parent form of Form 3
Public Sub openParentForm(ByVal ParentTag As String)
If ParentTag {use the notequal operator} "" Then
Select Case ParentTag
Case Form1.Tag
Form1.Show()
Case Form2.Tag
Form2.Show()
Case Form3.Tag
Form3.Show()
End Select
End If
End Sub
End Module
Form1:
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Tag = "Form1"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
parentForms(1) = Me.Tag
Form2.Show()
'Dont use .Close as the Parent Form will be disposed and the whole application will exit
Me.Visible = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
parentForms(2) = Me.Tag
Form3.Show()
Me.Visible = False
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Call openParentForm(parentForms(0))
End Sub
End Class
Form2
Public Class Form2
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Tag = "Form2"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
parentForms(0) = Me.Tag
Form1.Show()
Me.Visible = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
parentForms(2) = Me.Tag
Form3.Show()
Me.Visible = False
End Sub
Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Call openParentForm(parentForms(1))
End Sub
End Class
Form3
Public Class Form3
Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Tag = "Form3"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
parentForms(0) = Me.Tag
Form1.Show()
Me.Visible = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
parentForms(1) = Me.Tag
Form2.Show()
Me.Visible = False
End Sub
Private Sub Form3_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Call openParentForm(parentForms(2))
End Sub
End Class

Button Click Event Within Another

I have two buttons Start button and Stop button .I run my program by clicking on start button. I want to stop the program during the start button . But the program will not responde until the start buttun finish its job. How i can do check the stop buttun during the start. i heard about threading but i do not know how i can do it.
Private Sub Button_Start (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
//my code
//check always if the user push stop if no continue if yes go to this sub
//my code
end sub
Private Sub Button_Stop (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
stopClick = True
Dim Response As Integer
Response = MessageBox.Show("Do you really want to exit?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Response = vbYes Then
Me.Close()
End If
End Sub
you can use threading put button1 code in a function and use the thread .
you can refer to this example
'Thread created to handle the Background process for start_function
Dim t As System.Threading.Thread
Private Sub start_function()
While True
'your code here for Eg:
Dim i As Integer
i = i + 1
End While
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
t = New System.Threading.Thread(AddressOf Me.start_function)
t.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
t.Abort()
End Sub
Drag a backgroundworker component onto your form.
Imports System.ComponentModel
Public Class Form1
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Me.Text = "Busy Doing Work"
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
Me.Text = "Asking to Cancel"
BackgroundWorker1.CancelAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
While Not BackgroundWorker1.CancellationPending
System.Threading.Thread.Sleep(1000)
End While
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Me.Text = "Cancelled"
End Sub
End Class

VB.net Form unexpectingly terminating

Hi I have the following form but cant figureout why its upbrubtly terminiating when difrent buttons are clicked?
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub button1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
Dim TEST1 As Integer = System.IO.Directory.GetFiles("C:\test\test").Length
If TEST1 = 0 Then
Me.WebBrowser1.Navigate("http://www.hotmail.com")
End If
End Sub
Private Sub button1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
Me.WebBrowser1.Navigate("http://WWW.facebook.com")
End Sub
Private Sub button2_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.MouseLeave
Me.WebBrowser1.Navigate("http://WWW.facebook.com")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.WebBrowser1.Navigate("file://C:\test\test")
Button1.Enabled = False
Button2.Enabled = True
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.WebBrowser1.Navigate("file://C:\test")
Button2.Enabled = False
Button1.Enabled = True
End Sub
Private Sub button2_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.MouseEnter
Dim TEST2 As Integer = System.IO.Directory.GetFiles("C:\test\test").Length
If TEST2 = 0 Then
Me.WebBrowser1.Navigate("http://www.hotmail.com")
End If
End Sub
The terms face book and hotmail are just random to keep company site private :)
I suspect the Mouse_Enter event and the Mouse_Leave events are not giving time to the webbrowser to fully load the document and maybe it is internally crashing.
Try checking if the webbrowser has finished working before navigating again and tell us:
Use
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete
Me.WebBrowser1.Navigate("http://www.google.com")
End if

Progress Bar won't work for the third download in VB.NET

I'm trying to download 4 files simultaneously using 4 webclients. It download all 4 files simultaneously but the progress bar of first two works fine and the third progress bar exactly moves with the 1st one, and the fourth progress bar exactly moves with the 2nd one. Here's my code relevant to the issue.
Public WithEvents downloadFile1 As WebClient
Public WithEvents downloadFile2 As WebClient
Public WithEvents downloadFile3 As WebClient
Public WithEvents downloadFile4 As WebClient
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
System.Net.ServicePointManager.DefaultConnectionLimit = 10
End Sub
Private Sub startDownloadFile1()
downloadFile1 = New WebClient
Dim targetURL As String = lstURLs.Items.Item(0)
Dim destinationPath As String = "e:\Downloads\0.jpg"
downloadFile1.DownloadFileAsync(New Uri(targetURL), destinationPath)
End Sub
Private Sub startDownloadFile2()
downloadFile2 = New WebClient
Dim targetURL As String = lstURLs.Items.Item(1)
Dim destinationPath As String = "e:\Downloads\1.jpg"
downloadFile2.DownloadFileAsync(New Uri(targetURL), destinationPath)
End Sub
and similler for startDownloadFile3() and startDownloadFile()
Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
startDownloadFile1()
startDownloadFile2()
startDownloadFile3()
startDownloadFile4()
End Sub
Private Sub downloadFile1_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles downloadFile1.DownloadProgressChanged
pb1.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile2_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles downloadFile2.DownloadProgressChanged
pb2.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile3_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles downloadFile1.DownloadProgressChanged
pb3.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile4_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles downloadFile2.DownloadProgressChanged
pb4.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile3_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles **downloadFile1.**DownloadProgressChanged
pb3.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile4_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles **downloadFile2.**DownloadProgressChanged
pb4.Value = e.ProgressPercentage
Change to: simple error
Private Sub downloadFile3_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles **downloadFile3.**DownloadProgressChanged
pb3.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile4_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles **downloadFile4.**DownloadProgressChanged
pb4.Value = e.ProgressPercentage

access more components using a thread

I am trying to access a few component using a thread. My form looks like this:
my source looks like this:
Private Sub btnGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGO.Click
pbAction.Value = 0
bgwProcess.RunWorkerAsync()
Me.Cursor = Cursors.WaitCursor
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Application.Exit()
End Sub
Private Sub bgwProcess_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwProcess.DoWork
'a job consists in retrieving data, populating a listview and update the progressbar
'start job 1.1
'do job 1.1 -> ProgressBar1.value+=1
'do job 1.2 -> ProgressBar1.value+=1
'do job 1.3 -> ProgressBar1.value+=1
'start job 2.1 ProgressBar1.value=1
'do job 2.1 -> ProgressBar2.value+=1
'do job 2.2 -> ProgressBar1.value+=1
End Sub
Private Sub bgwProcess_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwProcess.RunWorkerCompleted
Me.Cursor = Cursors.Default
End Sub
Can anyone help me out?
I have created a class that in the _doWork is populated, and i send it to the _ProgressChanged procedure, where I cand do whatever I want to the component on the form:
Public Class myObj
Public action As String
Public msg As String
Public pbAction As Integer
Public pbMsg As Integer
End Class
...
Private Sub btnGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGO.Click
bgwProcess.RunWorkerAsync()
Me.Cursor = Cursors.WaitCursor
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Application.Exit()
End Sub
Private Sub bgwProcess_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwProcess.DoWork
Dim op As New myObj
op.action = "my action"
op.msg = "My result: Done"
op.pbAction = 1
op.pbMsg = 1
bgwProcess.ReportProgress(0, op)
End Sub
Private Sub bgwProcess_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgwProcess.ProgressChanged
Dim obj As New myObj
obj = DirectCast(e.UserState, myObj)
myListView.BeginUpdate()
Dim li As New ListViewItem(obj.action, 0)
li.SubItems.Add(obj.msg)
myListView.Items.AddRange(New ListViewItem() {li})
myListView.EndUpdate()
myListView.EnsureVisible(myListView.Items.Count - 1)
myListView.Refresh()
pbAction.Value = obj.pbAction
pbTotal.Value = obj.pbMsg
End Sub
Private Sub bgwProcess_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwProcess.RunWorkerCompleted
Me.Cursor = Cursors.Default
End Sub