Navigate multiple browsers in one command Visual Basic - vba

So, if I have for example three webbrowsers and I want them all to navigate to google.com I have to do it like this:
webbrowser1.navigate("google.com")
webbrowser2.navigate("google.com")
webbrowser3.navigate("google.com")
Is it possible do it in one line of code?

A sub routine:
NavigateTo "google.com"
Which calls
sub NavigateTo(url as string)
webbrowser1.navigate(url)
webbrowser2.navigate(url)
webbrowser3.navigate(url)
end sub

You can use an Array along with a for loop as follows.
Dim webbrowser(3) As WebBrowser
For value As Integer = 0 To 3
webbrowser(value) = New WebBrowser
webbrowser(value).Navigate("www.google.com")
Next
Note:- For only 3 instances, this code may not be necessary, but for 30 it might definitely help.

Related

List(Of...) WebBrowser - How to declare Web Browsers based on number of strings in an array

I am trying to grab images from a number of similar URLs (The urls do not end in an image format like .jpg). The number of URLs can vary, and is determined by counting the number of strings in an array of strings.
The best method I have found for downloading images from a website is taken from here:
https://www.codeguru.com/columns/vb/how-to-use-visual-studio-2012-to-download-images-from-websites.htm
This was working perfectly, however now I need to be able to declare more web browsers, depending on how many urls have to be visited to grab images for downloading. The following is what I would like to be able to do:
Dim photonumbers() As String = {"hdshshaga","sjshaghah","akajaha"}
Dim WebBrowsers As New List(Of WebBrowser)
Dim x As Integer
For x = 0 To photonumbers.Count - 1 Step 1
WebBrowsers.Add("wb"&x.ToString) 'I understand I am trying to add a string to a list of webbrowsers, but is there a way to declare a new webbrowser with this name?
Next
The reason I want to declare a new webbrowser for each url is that I cannot get the code from the above link to work as a single function solution. Instead you have to download the URL onto the web browser using one subroutine (eg.Triggered by a Button click), and then save all images from that webbrowser with the next subroutine. If anyone is able to advise me on how to change this code to a single function so that it can be called for each string, that would be even better.
Dim photonumbers() As String = {"hdshshaga", "sjshaghah", "akajaha"}
Dim WebBrowsers As New List(Of WebBrowser)
For Each photonumber As String In photonumbers
WebBrowsers.Add(New WebBrowser() With {.Name = "wb" & photonumber})
Next

Scrape html data Vba

I want to make a function that extracts data from a part of a site.
The following is the HTML site. HTML code.
Code for the function
Function GetElementById(url As String, id As String, Optional isVolatile As Boolean)
Application.Volatile (isVolatile)
On Error Resume Next
Dim html As Object, objResult As Object
ret = GetPageContent(url)
Set html = CreateObject("htmlfile")
html.Body.innerHtml = ret
Set objResult = html.GetElementById(id)
GetElementById = objResult.innerHtml
End Function
I need that extracts only the class "panel-body"
directly into the function. I think it would be .children (3). Is that correct?
And so that it is practical and fast, because I need to extract more than 50 sites.
I see at least two options.
Once you have the HTMLDivElement with id=Result you could simply get the children. Please test this by first doing objResult.Children(2) and checking what the element is that is returned.
objResult.Children(2).Children(0).Children(0)
The second is that in later versions of MSHTML I think with IE8 or later installed you have the method "GetElementsByClassName" This will return a collection of IHTMLElements. If the HTMLDocument only has 1 "panel-body" then you are in luck. If not you would need to loop through each one and check some other unique feature to know you have the right one.
Another way to generate the code for this job is to record a macro, then add a loop around the recorded macro that loops through your 50 pages and gets the results.
On the data tab in the ribbon there is an option get data from external sources. If you use this it's gives you a point and click interface that let's you chose the table your looking for. Record a macro while your doing this and it generates the code for you.

Multithreading in vb.net to simulate task

I have a program that is doing one task.
For Example i have one list box containing some links.
And on the other hand my program is opening them one by one but i want it to be done faster
i have used for-each loop for that purpose.
All what i want to do is i wanna give every 2 or 3 link to a different thread or if there is any other solution to make it Faster Kindly tell me.
This is a small piece of code from my program.
For value As Integer = 1 To TextBox1.Text
If (value = TextBox1.Text) Then
Exit For
End If
Dim page As String = "-p-" & value
Extractor.ScrapLinks(txturl.Text + page, lstbox)
lbllinks.Text = lstbox.Items.Count
Next
I don't know what Extractor.ScrapLinks actually do, but it seems that you need to access UI thread and you cannot create multiple UI threads so eventually you will process them sequentially
What you can do to improve the solution is to read the data you want from the UI controls and then process that data on a separate thread, after completion you can fill the results into the UI by invoking some method on the UI thread as shown below
Delegate Sub PerformOperationDel(value As Integer)
Sub PerformOperation(value As Integer)
Dim page As String = "-p-" & value
Extractor.ScrapLinks(txturl.Text + page, lstbox)
lbllinks.Text = lstbox.Items.Count
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For value As Integer = 1 To CInt(TextBox1.Text) - 1
lstbox.BeginInvoke(New PerformOperationDel(AddressOf PerformOperation))
Next
End Sub
You can use backgroundworkers but notice that you cannot access UI controls in the DoWork but you can access them on work completed (Refer to: Background worker proper way to access UI)
Best of luck

Visual basic 2008 issue with adding a method to my dynamic objects

I have searched for an answer to my question since last thursday. A lot of answers about my exact same question have been answerd in vb.net. However, I am working on Visual Basic 2008 and those two language seems to have differences that are for me difficult to understand. So here is my issue.
I need to create several picture box and I have created them dynamicly as several sites recommanded. That part works fine. Issue begins when I want to click on them. I read enough to understant that it is not because I have created the object that I have created the method attached to them. Then I create the method. Still no problem except when I am running the code each button does the same thing because they are all attached to the same method. I came to a solution: I need to transfer with the method an argument to tell wich Picturebox I am clicking on, but because I am using addressof I can't. I know few sites that have talked about the same issues and solved it using a lamda expression. If someone could give me the code I should use I would be really thankful.
Here is my code:
For i = 0 To 7
'couleur is the name I give to my picturebox object and objet () is the sub in which I created my object
couleur(i) = objet()
Next
For x = 0 To 7
' initiasation of location, etc.
Next
' This is the issue !!! I do not know how to say this line into vb8
' I want to pass in argument X to know on which object I have cliked on and then use a seled case to make separated command afterward.
For x = 0 To 7
AddHandler couleur(i).Click, Function(senderobj, args) couleur_click(x)
Next
End Sub
Sub couleur_click(ByVal i As Integer)
' select case doing seperated things depending on the x receive in argument
End Sub
Thank all of you for help, sorry for my language it is not my first language.
Why don't you change couleur_click to take the sender as a parameter? You will then know the source of the click, from which you can find the index of the PictureBox in your couleur array:
' ...
For x = 0 To 7
AddHandler couleur(i).Click, AddressOf couleur_click
Next
' ...
Sub couleur_click(sender As Object, e As EventArgs)
Dim pictureBoxSource As PictureBox = sender
' Find the index of the source in the base collection
Dim index = Array.IndexOf(couleur, pictureBoxSource)
Select Case index
' ...
End Select
End Sub
Set the tag property of each PictureBox, then in the click event handler you can do a select case on the tag.
You can't add parameters to the built in event handlers.

Open multiple files using arguments

I'm using this code to load multiple files using windows context menu, but the problem is that the aplication is open many times as files the user has selected.
For example: If I select 14 files, an open them with the application, the aplicacion is opened 14 times and load the form only one.
But there is a way to send all arguments once? Because %1 send only one file, or there is for example a %2 that send all file pats in one argument? If there is I'vent found.
This my actual code:
Public Class Program
Public Shared Sub Main()
Dim FurBase As New Core.clsDatabase
FurBase.Directory = My.Application.Info.DirectoryPath
Dim returnValue As String()
returnValue = Environment.GetCommandLineArgs()
If returnValue.Length > 1 Then
FurBase.AddTemporalFilepath(returnValue(1).ToString)
End If
If Not Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length > 1 Then
ShowUploader()
End If
End Sub
Private Shared Sub ShowUploader()
Dim Uploader As New frmUploader
Application.EnableVisualStyles()
Application.Run(Uploader)
End Sub
End Class
Please tell me what think about the code and if ther is any way to improve it.
Regards~
I was reading about that today; seems you'll need to deal with a DDE server.
There are an old question which can help you: What is the best .net alternative to dde for file associations?