Visual Basic - Web browser load URLs from text - vb.net

i am not so great with Visual basic, but i need some help on creating a web browser that would load several links import from a text file, and for the web browser to navigate to them. This is what i have so far
Dim link As String = OpenFileDialog2.FileName
Dim links As String = IO.File.ReadAllText(link)
MsgBox(links)
WebBrowser1.Navigate(links)
You help means a lot. Thank You.

The WebBrowser Control either will show the webpage in the Control which will limit you to one page, or you can tell it to open the pages in separate windows which will open an Internet Explorer window for each link. I also used the File.ReadAllLines Method in order to get an array of the Links so that you can iterate through the Web Pages . This works for me but might not be what you are wanting.
Public Class Form1
Dim wb As New WebBrowser
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim filename As String = "C:\temp\links.txt"
Dim links As String() = IO.File.ReadAllLines(filename)
For Each link As String In links
wb.Navigate(link, True)
Next
End Sub
Public Sub New()
InitializeComponent()
Controls.Add(wb)
wb.Dock = DockStyle.Fill
End Sub
End Class
My text file called Links.txt looks like this:
www.google.com
www.msdn.com
www.bing.com

Related

Wait for internet explorer to load everything?

I am scraping a webpage, and waiting for internet explorer to get done loading but for some reason it is not. I'm trying to get a value on the page but the wait part is not waiting therefore the value comes back blank when there should be a value. The IE page has done loading but the value for the elements on the page has not been loaded yet. Is there a way to wait for all elements to get done loading before proceeding to next line of code? Here's my code:
Dim IE As Object
Dim myvalue as string
IE = CreateObject("internetexplorer.application")
IE.navigate("mypage")
While Not IE.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
End While
myValue = IE.document.getElementById("theValue").getAttribute("value")
Debug.Print(myValue)
You SHOULD NOT use Application.DoEvents() in order to keep your UI responsive! I really can't stress this enough! More than often using it is a bad hack which only creates more problems than it solves.
For more information please refer to: Keeping your UI Responsive and the Dangers of Application.DoEvents.
The correct way is to use the InternetExplorer.DocumentComplete event, which is raised when the page (or a sub-part of it, such an an iframe) is completely loaded. Here's a brief example of how you can use it:
Right-click your project in the Solution Explorer and press Add Reference...
Go to the COM tab, find the reference called Microsoft Internet Controls and press OK.
Import the SHDocVw namespace to the file where you are going to use this, and create a class-level WithEvents variable of type InternetExplorer so that you can subscribe to the event with the Handles clause.
And voila!
Imports SHDocVw
Public Class Form1
Dim WithEvents IE As New InternetExplorer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
IE.Navigate("http://www.google.com/")
End Sub
Private Sub IE_DocumentComplete(pDisp As Object, ByRef URL As Object) Handles IE.DocumentComplete
MessageBox.Show("Successfully navigated to: " & URL.ToString())
End Sub
End Class
Alternatively you can also subscribe to the event in-line using a lambda expression:
Imports SHDocVw
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim IE As New InternetExplorer
IE.Navigate("http://www.google.com/")
AddHandler IE.DocumentComplete, Sub(pDisp As Object, ByRef URL As Object)
MessageBox.Show("Successfully navigated to: " & URL.ToString())
End Sub
End Sub
End Class

Asp.Net, VB, SQL Server Reporting Services..dynamically generated reports from directory to be viewed on report viewer on click?

I have an aspx webform that in it's vb code reads files that are .rdl from a directory and then lists them as say a button or hyper link etc. "reports are on local host report server"
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim FileLocation As DirectoryInfo = _
New DirectoryInfo("C:\inetpub\wwwroot\Reports")
Dim fi As FileInfo() = FileLocation.GetFiles("*.rdl")
For Each name As FileInfo In fi
' Dim i As Integer
Dim listed As New LinkButton
' listed.Attributes("CssClass") = "a"
' listed.Attributes("Class") = "a"
listed.Attributes("id") = "listed"
listed.Text = (name.Name)
mine.Controls.Add(listed)
'mine.InnerHtml = ""
'i = i + 1
Next
End Sub
and another that has a reportviewer. When a report was clicked, how can I send the name/value to the viewer and redirect to it?
Thank you very much
EDIT: i got to make the click redirect to the viewer with a string and have the viewer use that string,but with how much ever variation ive tried it still errors that
The path of the item 'salesreport.rdl' is not valid. The full path must be less than 260 characters long; other restrictions apply. If the report server is in native mode, the path must start with slash. (rsInvalidItemPath)
this is the viewer code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
' Retrieve parameter from Route "Reports/{reportName}"
Dim reportName = Picks.Selecty
'Page.Title = reportName
ReportViewer1.ProcessingMode = ProcessingMode.Remote
Dim serverReport As ServerReport
ServerReport = ReportViewer1.ServerReport
serverReport.ReportServerUrl = New Uri("http://localhost/reportserver/")
serverReport.ReportPath = reportName
' reportName
'
' add parameters here
'Dim param As New ReportParameter("name", "value")
'serverReport.SetParameters(param)
serverReport.Refresh()
End If
End Sub
You could try the following.
Create a new page say ReportViewer.aspx which hosts your ReportViewer Control that recieves the name of the .RDL file as a query string.
In the First .aspx page, for every link you display, point the NavigateURL property to ReportViewer.aspx page and pass the name of the .RDL file as a QueryString.
Hope that helps
Thanks,
Prawin

how to automatically click search button in google visual basic . net

I have a form with textbox, button and a tabcontrol.
in the button I have this code :
[Dim browser As New WebBrowser()
TabPage1.Controls.Add(browser)
browser.Dock = DockStyle.Fill
browser.Navigate(New Uri("http://www.google.com"))]
The code above works, but I need to be able to search from my textbox and when I click the button it will take me to google and then automatically enter's the word I searched for in my textbox and then clicks the search on google button. I tried this but it does not work. Thanks
Dim textElement. As HtmlElement = browser.Document.All.GetElementsByName("q")(0)
textElement.SetAttribute("value", textbox.text")
Dim btnElement As HtmlElement = browser.Document.All.GetElementsByName("btnG")(0)
btnElement.InvokeMember("click")
I also needed to search in Google Browser with the text the User wanted and by adding the code below to the Button Click Event it did what I wanted.
Code:
Dim sInfo As New ProcessStartInfo("https://www.google.co.in/search?q=" & TXT_Entidade.Text)
Try
Process.Start(sInfo)
Catch ex As Exception
Process.Start("iexplore.exe", sInfo.FileName)
End Try
You'll need to set focus to the textElement before clicking the button.
textElement.Focus()
Otherwise, the page won't run the search apparently.
You can see this by trying the same basic steps you've got above in the Console window. They won't work until the field has had focus (from my testing).
(I also used the mshtml type library so that the click function was directly exposed in the full code below)
Imports mshtml
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
web.Navigate("http://www.google.com")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim textElement As HtmlElement = web.Document.All.GetElementsByName("q")(0)
textElement.SetAttribute("value", TextBox1.Text)
textElement.Focus()
Dim btnElement As HTMLButtonElement =
CType(web.Document.All.GetElementsByName("btnG")(0).DomElement,
HTMLButtonElement)
btnElement.click()
End Sub
End Class
I think you can use this in your button click handler...
browser.navigate("https://www.google.co.in/search?q="+textbox.Text)
This will search Google for the text in your Text Box.
You can search without "automatically clicking the search button" and you do not have to set the value in the text element of the html. This works for me. Hope this helps.

Capture New Window in WebBrowser Control VB.NET

I've been searching a bit and I haven't been able to exactly find what I need. I need to contain a popup window within the WebBrowser control in VB.NET
I found this project: http://www.codeproject.com/KB/cpp/ExtendedWebBrowser.aspx
But I've been having trouble parsing out what I need from it. It looks like it implements what I need, but I'm not really sure how the heck it's doing it. I just need to capture a popup and display it in a new WebBrowser object.
Private Sub WebBrowser1_NewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
Dim myelement As HtmlElement = WebBrowser1.Document.ActiveElement
Dim target As String = myelement.GetAttribute("href")
Dim newinstance As New WebBrowser
newinstance.Show()
newinstance.Navigate(target)
e.Cancel = True
End Sub

How to open a Bing Maps from vb.net?

I would like to pass and address to and open Bing Maps from a vb.net app. Anywhere I can find an example of how to do this?
You're going to want to check out the Bing Maps Web Services SDK. It allows you to use Bing to Geocode (retrieve a location from an address) as well as Download Imagery Data. Another place for Bing developers information can be found here.
Edit: I just reread your question and I think you can do what you need using a simple string concatenation to create a url. This might work:
Dim MyURL As String
Dim Location As String
Location = "Pittsburgh, PA"
MyURL = "http://www.bing.com/maps/?v=2&where1=" + HttpUtility.UrlEncode(Location) + "&encType=1"
System.Diagnostics.Process.Start(MyURL)
My VB is a little rusty, but this should get you started. You will also need to import the System.Web Namespace for the HttpUtility Class.
Refrences:
Launch Browser from VB.NET
HttpUtility.UrlEncode Method - MSDN
Bing URL Parameters
'In addition to kersney's example this uses the webbrowser control in a vb form
Imports System.Web
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Location As String = "Pittsburgh, PA"
Dim MyURL As String = "http://www.bing.com/maps/?v=2&where1=" + HttpUtility.UrlEncode(Location)
Dim WebBrowser1 As New WebBrowser
Me.WindowState = FormWindowState.Maximized 'maximize window
Me.Controls.Add(WebBrowser1) 'add browser control
WebBrowser1.Dock = DockStyle.Fill 'fill to form
WebBrowser1.Navigate(MyURL) 'display page in form
End Sub
Try the nerd dinner asp.net mvc tutorial. It's near the end and you can download a working example.