need help with loop in search engine finder code
Ran into a problem. My for loop seems to stop after just for the task. I can't seem how to get it to continue on, I've used this type of loop many times and have never had this problem. Any suggestions?
Code:
Imports System.Net
Imports System.Text.RegularExpressions
Public Class Form1
Dim sites As String = Nothing
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.AddRange(TextBox1.Text.Split(Environment.NewLine))
TextBox1.Clear()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim pages As Integer = 1
Dim p As Integer = 0
Dim source As String
For i = 0 To ListBox1.Items.Count - 1
RichTextBox1.Text = RichTextBox1.Text + ListBox1.Items.Item(i)
Do Until p = NumericUpDown1.Value
Dim wc As New WebClient
source = wc.DownloadString("http://www.bing.com/search?q=" & ListBox1.Items.Item(i) & "&go=&qs=n&sk=&sc=4-13&first=" & pages & "&FORM=PERE")
sites = sites + source
links(i)
pages += 10
p += 1
Loop
source = Nothing
Next
End Sub
Private Sub links(ByVal i As Integer)
Dim links As MatchCollection = Regex.Matches(sites, "(http|https):\/\/([\w\-_\.]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?", RegexOptions.Singleline + RegexOptions.IgnoreCase)
For Each link As Match In links
Dim value As String = link.Groups(0).Value
If value.Contains(ListBox1.Items.Item(i)) Then
RichTextBox1.Text = RichTextBox1.Text + value + vbNewLine
End If
Next
End Sub
End Class
Re: need help with loop in search engine finder code
At first glance it looks to me like, you need to reset 'p' to when you drop out of the 'Do Until loop'. On the Next 'For Loop' 'p' is already equal to NumericUpDown.Value and so won't perform the Do Until.
Where you have source = nothing, add on a new line p = 0 and see if that works
Cheers
Dan