WebBrowser load and display URLs from file - vb.net

I want to load a list of URLs in a text file. I have done that using a For Each loop, but I want the browser to load the page first fully before going to the next URL. Here is my code:
For Each url in File.ReadAllLines("urls.txt")
Browser.Navigate(url)
Next
It doesn't display anything. I tried using:
Do While Browser.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop
It displays the page but the first line is the only one being loaded.

If you want to navigate to each URL after the other you are better of storing all of them in a class level array and subscribing to the WebBrowser.DocumentCompleted event, then keep track of which URL index you're currently at via a class level Integer variable.
When the DocumentCompleted event is raised you just increment the integer variable and load the URL from the next item of the array.
Public Class Form1
Dim URLs As String()
Dim UrlIndex As Integer = 0
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
URLs = File.ReadAllLines("urls.txt")
WebBrowser1.Navigate(URLs(UrlIndex))
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
UrlIndex += 1
If UrlIndex >= URLs.Length Then
UrlIndex = 0 'Go back to the beginning.
End If
WebBrowser1.Navigate(URLs(UrlIndex))
End Sub
End Class
To add a delay to it to make each URL show for a little while you can use a Timer (Credit to Werdna for bringing up the subject):
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
UrlIndex += 1
If UrlIndex >= URLs.Length Then
UrlIndex = 0 'Go back to the beginning.
End If
WebBrowser1.Navigate(URLs(UrlIndex))
Timer1.Stop()
End Sub
Just set the timer's Interval property to change how long a site should be displayed (in milliseconds).

Related

finding web page source at desired instance VB.NET

IN CODE: At 1 browser clicks a button and takes a time to load. AT 2 i get source code of page in RichTextBox1. but as page take time to load code 2 starts before completion of 1 because of that i am unable to get the web page source at desired state? what do i do ? i want to get web page source when web browser completely loads after the execution of code 1.
i have tried
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WebBrowser1.Navigate("some website")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
WebBrowser1.Document.GetElementById("notification_address").SetAttribute("value", TextBox1.Text)
WebBrowser1.Document.Forms(0).InvokeMember("submit") (-----1-----)
RichTextBox1.Text = WebBrowser1.DocumentText (-----2-----)
End Sub
You can use the DocumentCompleted event.
https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
Private Sub DocumentCompleted(ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
' this is where your code goes RichTextBox1.Text = WebBrowser1.DocumentText (-----2-----)
End Sub

Get files one at a time from a folder in VB.NET

I am trying to read some text files path in a folder sequentially. However, I get only the first file.
I need to get the first file, execute a timer, get the next file path, execute a timer right up to the last file in the folder, and stop. How can I get around this?
Private zMailbox As String = "c:\Fold\"
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles Button1.Click
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Timer1.Tick
Dim finfo As New IO.DirectoryInfo(zMailbox)
For Each fi In finfo.GetFiles("*.txt")
TextBox1.Text = fi.FullName
Next
End Sub
Thanks to the contributions below I got the code to work with the text box value. However, it gives the index count instead of the path which I want to retrieve.
Private zMailbox As String = "c:\Fold\"
Dim files As FileInfo()
Dim index As Integer = 0
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles Button1.Click
Dim finfo As New IO.DirectoryInfo(zMailbox)
files = finfo.GetFiles("*.txt")
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Timer1.Tick
If index >= files.Length Then
index = 0
End If
TextBox1.Text = (ListBox1.Items.Add(files(index)))
index += 1
End Sub
Your code loads all the files in the Timer event and assign them to the TextBox1.Text property inside the loop. Every loop overwrites the data that has been written in the previous loop.
At the end of the loop you see only the last value.
To show sequentially the files inside the Timer Tick event, you need to read the directory content before starting the Timer in a global FileInfo array. Another global variable will be used as indexer to show a particular file from this FileInfo array in your Timer.Tick event.
The index will be incremented and, at the next Tick, you could show the next file
Dim files as FileInfo()
Dim index As Integer = 0
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim finfo As New IO.DirectoryInfo(zMailbox)
files = finfo.GetFiles("*.txt")
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
if index >= files.Length Then
index = 0
End If
TextBox1.Text = files(index)
index += 1
End Sub
EDIT
According to your comment, you need to set the MultiLine property of the TextBox to true (using the form designer) and then, at every Tick, instead of replacing the Text property, append to it
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
if index >= files.Length Then
return ' Reached the end of the array. Stop the Timer???
End If
TextBox1.AppendText(files(index) & Environment.NewLine)
index += 1
End Sub
As a side note, if you want to show all file names together then it is not clear why you need a timer at all.
You could get the same result with code like this
Dim finfo As New IO.DirectoryInfo(zMailbox)
Dim files = finfo.EnumerateFiles("*.txt")
TextBox1.Text = string.Join(Environment.NewLine, files.Select(Function(x) x.FullName).ToArray())
On the original code you posted you where getting all files in the for loop each time the timer clicks.
After reading steve answer, and your comments, probably you always got all the files, but you override the textbox.text value.
TextBox1.Text += < String > & vbNewLine
Where < String >, of course, is the string returned by DirectoryInfo.GetFiles()
I think steve answer works just fine, but you are not implementing it well.
I would try and make this as easy as possible for you. You Microsoft's Reactive Framework for this. Just NuGet "Rx-Main".
Here's what you can then do:
finfo.GetFiles("*.txt").ToObservable() _
.Zip(Observable.Interval(TimeSpan.FromSeconds(1.0)), Function(f, _) f.Name) _
.ObserveOn(TextBox1) _
.Subscribe(Function(n) textbox_text += n + Environment.NewLine)
That's it. No timers. No separate methods. No need for module-level variables. Just one line of code and you're done.
It's processed on a background thread and then marshalled back to the UI via the .ObserveOn(TextBox1) call.
You can even keep a reference to the IDisposable returned by the .Subscribe(...) call to terminate the observable (timer) early.
Simple.
This seems a bit Rube Goldberg-ish. Just get all the files and loop through them in your Button_Click method:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim finfo As New IO.DirectoryInfo(zMailbox)
For Each fi In finfo.GetFiles("*.txt")
TextBox1.Text = fi.FullName
Next
End Sub

VB.NET Save Combo Box to My.Settings

Looking to save combobox items to my.settings collection. I developed a webbrowser and a combobox will be my address bar. I am attempting to save a history of visited sites.
I tried the below code and it doesnt work. It errors out with "Object reference not set to an instance of an object":
Went into settings added MyItems for the name, and then select System.Collections.Specialized.StringCollection as the data type. Then onload is the below:
For Each i As String In My.Settings.MyItems
ComboBox1.Items.Add(i)
Next
FormClosing and ive tried FormClosed: For now i put it in a button event to save it for testing
My.Settings.MyItems.Clear()
For Each i As String In ComboBox1.Items
My.Settings.MyItems.Add(i)
Next
I love this site very much! So I came back to post the correct code that will correctly save and load combobox entries to my.setting! This has been tested as working!!!
Private Sub Form1_FormClosing(
sender As Object,
e As FormClosingEventArgs) Handles Me.FormClosing
My.Settings.Categories.Clear()
For Each item In ComboBox1.Items
My.Settings.Categories.Add(item.ToString)
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each item In My.Settings.Categories
ComboBox1.Items.Add(item)
Next
ComboBox1.SelectedIndex = 0
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ComboBox1.Items.Add(TextBox1.Text)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If ComboBox1.Items.Count > 0 Then
Dim Index As Int32 = ComboBox1.SelectedIndex
ComboBox1.Items.RemoveAt(Index)
If Index - 1 <> -1 Then
ComboBox1.SelectedIndex = Index - 1
End If
End If
End Sub

VB.NET 2010 Web Request Refresh

Hey im kind of new to VB and am trying to get a web request to refresh every 30 seconds, and have got stuck on how to implement a timer in to it. Here is the code I have produced so far, i would be grateful for the correct solution:
Public Class main
Dim boatid As Integer
Sub googlemaps()
Dim url As String = "http://www.google.com"
Me.WebRequest.Navigate(New Uri(url))
'Implement timer here? (me.refresh)?
End Sub
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectBoat.ValueChanged
boatid = SelectBoat.Value
SelectBoat.Maximum = 10
SelectBoat.Minimum = 1
Lbboatid.Text = boatid
End Sub
Private Sub btnsequence_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsequence.Click
Dim i As Integer
boatid = i
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshTimer.Tick
RefreshTimer.Enabled = True
RefreshTimer.Interval = 30000
End Sub
Private Sub main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Datetime.Text = Now.ToString
googlemaps()
End Sub
End Class
The method that has to be executed every certain period of time should be put on the
Timer1_Tick event as well. This is a small implementation try it and see if it works adapting your code accordingly:
Set up your desired Timer properties:
Click on the Timer on the Design View and on its Properties Box List set:
Interval property on 30000 (The Event will fire every 30 seconds)
Enabled on True (The Timer will start working after your form is loaded)
Then on your Codebehind
private void ShowMessage()
{
MessageBox.Show("Hello Timer");
}
private void timer1_Tick(object sender, EventArgs e)
{
ShowMessage();
}
Also here is a working implementation according to the code you posted, as for what i understand out of your code you want the browser to refresh every certain seconds as well as the numeric up and down control show the value of the variable set on boatId, this code does that:
Set the minimum and maximum properties of your numeric up down control on the Properties box of it (right click on the control in design view and search for those two properties)
Then try the following;
Public boatid As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.lblDate.Text = Now.ToString()
''Set a bigger winform to show the browser page better
Me.Width = 800
Me.Height = 600
googlemaps()
End Sub
''The browser will be refreshed every n seconds) according to what you have set on interval property of the timer control
Sub googlemaps()
Dim url As String = "http://www.google.com"
Me.WebBrowser1.Navigate(New Uri(url))
End Sub
''Loop over this method using the Tick event, while doing that verify the value
''of the boatid variable and if its less than the maximun value allowed by the numeric
''updown control increment it in one unit, then show it on the numeric selected value
''as well on the label control
Private Sub ChangeBoatIdValueCycling()
If boatid < 10 Then
boatid += 1
Else
boatid = 1
End If
Me.NumericUpDown1.Value = boatid
Me.lblBoatId.Text = boatid.ToString()
End Sub
''This wil show the id on the label text when you click up and down the numeric control
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.lblBoatId.Text = Me.NumericUpDown1.Value.ToString()
End Sub
''This will set a variable with value 5 that will get shown selected on the numeric
''control as well as visible on the label text, the value will be shown because
''it exists in the range of 1 to 10 that is your requeriment.
Private Sub btnsequence_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsequence.Click
Dim i As Integer = 5
boatid = i
Me.NumericUpDown1.Value = boatid
Me.lblBoatId.Text = boatid.ToString()
End Sub
''Needed to repeat the form refresh and boatid cycling every n seconds according to your Interval property
''value
Private Sub RefreshTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshTimer.Tick
googlemaps()
ChangeBoatIdValueCycling()
End Sub
I hope it helps. Let me know how it goes.

How to use timer for for next loop in visual basic 2008

I have a case where i need to generate millions of unique codes. For this I have created a generate function where the random number is generated. I call this function from a for loop and add the generated number on a list box. my code is as follow
for i=1 to val(txtnumber.txt)
mynum=generate()
next
I have created a lable on form where i wanted to display the no of secs elapsed while processing the loop. I used timer control as
timer1.start()
for i=1 to val(txtnumber.text)
mynum=generate()
listbox1.items.add(mynum)
next
timer1.stop
and on timer1_tick function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = Val(Label1.Text) + 1
End Sub
but when i click generate button, all numbers are generated, but timer doesnot shows time elapsed.
I may have missed something, so please help me out
This is probably best handled in a BackgroundWorker. Place one on the form and set its WorkerReportsProgress=True. Also, placing a million numbers in a ListBox probably isn't a good idea, so I omitted that.
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Button1.Enabled = False
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim started As DateTime = Now
For i As Integer = 1 To val(txtnumber.txt)
mynum=generate()
BackgroundWorker1.ReportProgress(i, Nothing)
Next
Dim ended As TimeSpan = Now.Subtract(started)
BackgroundWorker1.ReportProgress(0, ended.TotalSeconds.ToString)
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
If e.UserState IsNot Nothing Then
Label1.Text = e.UserState.ToString()
Else
Label1.Text = e.ProgressPercentage.ToString
End If
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Button1.Enabled = True
End Sub
Your label should be updating correctly when the worker reports the ProgressChanged event.
What you're encountering is a threading issue. The work you are doing to generate the numbers is being executing by the UI thread, so it never gets a chance to update the screen. Take a look here: How to prevent UI from freezing during lengthy process?
This one might also have good information for you: Updating UI from another thread
Try this:
Private _Counter As Integer = 0
Private _StartTime As Date = Now
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
_StartTime = Now
_Counter = CInt(Val(txtnumber.Text))
ListBox1.Items.Clear()
Label1.Text = "0"
Timer1.Interval = 50
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
ListBox1.Items.Add(generate())
Label1.Text = New Date((Now - _StartTime).Ticks).ToString("HH:mm:ss.ff")
_Counter -= 1
If (_Counter <= 0) Then
Timer1.Stop()
End If
End Sub
Or you can research actual Threading.