Pulling information from the Page Source - vb.net

Ok so I have been have alot of trouble with the
Web browser control on a few applications I am working on.
All of them share the same issue. I want to have the application navigate s webpage a read the write the text in the page source to a variable. I also need to be able to save the files afterwards.
Some Source code:
Public Class Form4
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyFolderBrowser As New System.Windows.Forms.FolderBrowserDialog
MyFolderBrowser.Description = "Select the Folder"
MyFolderBrowser.ShowNewFolderButton = False
Dim dlgResult As DialogResult = MyFolderBrowser.ShowDialog()
If dlgResult = Windows.Forms.DialogResult.OK Then
TextBox1.Text = MyFolderBrowser.SelectedPath
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = "" Then
MessageBox.Show("You have to select a directory!")
Else
WebBrowser1.Navigate("www.realmofthemadgod.com/version.txt")
System.Threading.Thread.Sleep(3000)
Dim PageSource As String = WebBrowser1.Document.Body.InnerText
WebBrowser1.Navigate("http://www.realmofthemadgod.com/AssembleeGameClient" & PageSource & ".swf")
End If
End Sub
End Class
The first thing I am having an issue with is that it never waits for the webpage to load before pulling the Document text. I have tried many different ways to from different solutions people have posted to get around this. Oddly it always seems to work if I do it a second time.
And I want to save the final resulting webpage as an swf to the selected directory if Button2 is clicked.
Thanks for any help I have been looking for this everywhere

Welcome to the dark art of web scraping. First off, I would recommend using WebClient instead of WebBrowser as it has discrete methods for downloading data from web sites. It looks like your version.txt only contains the data you want (and no extraneous html) so we can download it directly. If you needed to parse html I would use HtmlAgilityPack. Untested code to get you started:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = "" Then
MessageBox.Show("You have to select a directory!")
Else
Using wc as New WebClient()
Dim version = wc.DownloadString("www.realmofthemadgod.com/version.txt")
Dim swf = "http://www.realmofthemadgod.com/AssembleeGameClient" + version + ".swf"
wc.DownloadFile(swf,"c:\temp\myswf.swf")
End Using
End If
End Sub

Related

How do I import HTML files to be displayed in a VB.NET WebBrowser

So far I've gotten this from piecing together OpenDialog tutorials and HTML editor/writer tutorials but I just picked this up today, to do this specific thing so my prior knowledge of VB and general software development is EXTREMELY low. :P
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1 Click
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName > "" Then
WebBrowser1.DocumentText =
End sub
Use URL propery of WebBrowser Control also always use filter to select desired format only
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
OpenFileDialog1.Filter = "Html or Pdf-Files(*.html; *.pdf))|*.html;*.pdf"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim cfilenam As String = OpenFileDialog1.FileName()
If cfilenam.Trim().Length > 0 Then
Me.WebBrowser1.Url = New Uri(cfilenam)
Me.WebBrowser1.Refresh()
End If
End If
End Sub

VB.net Printform cant find path

So I am working on a form which need to be printed. I want to end up with a PDF file using Foxit PDF printer. The problem is that I cant figure out how to get the selected path as file location so I keep getting an the Path cannot be null.
error. Where in the code should I put my filelocation when using the Printform?
In this code the foldername is the location where I want to print.
Private Sub BtnPrint_Click(sender As Object, e As EventArgs) Handles BtnPrint.Click
Dim folderDlg As New FolderBrowserDialog
Dim foldername As String
folderDlg.ShowNewFolderButton = True
If (folderDlg.ShowDialog() = DialogResult.OK) Then
foldername = folderDlg.SelectedPath
Dim root As Environment.SpecialFolder = folderDlg.RootFolder
End If
PrintForm1.Print()
End Sub
Edit:
Actually deleted part of the code and still getting the same error (first part wasnt doing anything to start with I know that). All I am using now is:
Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles BtnPrint.Click
PrintForm1.Print()
End Sub
Also microsoft help database about Printform isnt helping since I have done exactly what it says and still getting the Path is Null error
Edit 2:
So I am using this code now and it is working.
Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles BtnPrint.Click
PrintDialog1.PrinterSettings = PrintForm1.PrinterSettings
PrintDialog1.AllowSomePages = True
If PrintDialog1.ShowDialog = DialogResult.OK Then PrintForm1.PrinterSettings = PrintDialog1.PrinterSettings
With Me.PrintForm1
.PrintAction = Printing.PrintAction.PrintToPreview
Dim MyMargins As New Margins
With MyMargins
.Left = 10
.Right = 10
.Top = 10
.Bottom = 10
End With
.PrinterSettings.DefaultPageSettings.Margins = MyMargins
.Print()
End With
End Sub
but as soon as I try to set which area it should print I get the following error: "Printing is not a member of powerpacks". I tried using the following code according to microsoft this is the way it should work.. I have no clue where the error comes from
.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)
You don't need a path to use printform. Printform just prints what you see on the screen to your default printer. You need to install "Visual Basic PowerPacks" to use this command. More explanation you may find here:
https://learn.microsoft.com/en-us/dotnet/visual-basic/developing-apps/printing/how-to-print-a-form-by-using-the-printform-component
To preview your print you don't need to use printdialog and all this. You just click on printform1 in the designer,to get up the properties window of printform1. In printaction you choose PrintToPreview. Thats all its needed.
These are all the lines I need:
Public Class Form1
Private Sub Exit_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub
Private Sub Print_Click(sender As Object, e As EventArgs) Handles Button1.Click
PrintForm1.Print()
End Sub
End Class

How to do you make a ListBox display all files in a drive?

I'm coding an Anti-Virus at the moment, so it's been very complicated to code it and design it. Anyway, the other day I ran into a problem, where my ListBox is not displaying all the files that are in the selected drive/directory.
I'll put some code and images so you get the idea.
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
ListBox1.Items.Clear()
ListBox3.Items.Clear()
FolderBrowserDialog1.SelectedPath = Path.GetPathRoot(Environment.SystemDirectory) & "Boot\"
On Error Resume Next
For Each strDir As String In System.IO.Directory.GetDirectories(FolderBrowserDialog1.SelectedPath)
For Each strFile As String In System.IO.Directory.GetFiles(strDir)
ListBox1.Items.Add(strFile)
ListBox3.Items.Add(strFile)
Next
Next
Timer1.Start()
End Sub
However, instead of the files appearing in the ListBox (ListBox3), it just gives a black screen. Maybe I should remove the TabControl that it is surrounded by?
See how it's black? It even happens when I run it.
Hope this helps! Comment if you need more information.
You may want to simplify your events by creating subs and standardizing the code instead of embedding it directly into the button click. I have created a working example of how to load directories and files dynamically.
I would also recommend doing a isolated experiment, you can easily throw together a test project to isolate the directories in question and the layout you are trying to achieve. It could be that there are other events causing noise in your debugging.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
load_dirs(ListBox2, "H:\")
End Sub
Sub load_files(LB As ListBox,
IO_dir As String)
Dim Files_ = IO.Directory.GetFiles(IO_dir)
With LB.Items
.Clear()
.AddRange(Files_)
End With
End Sub
Sub load_dirs(LB As ListBox,
IO_dir As String)
Dim dir_ = IO.Directory.GetDirectories(IO_dir)
With LB.Items
.Clear()
.AddRange(dir_)
End With
End Sub
Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
Try
Dim lb As ListBox = sender
load_files(ListBox1, lb.SelectedItem)
Catch ex As Exception
End Try
End Sub
End Class

VB.Net Open New Form Instance as Separate Process

I am trying to launch a separate instance of my application with a new process ID. Is there any way to do this? The following code will show a separate form, but it shares the process ID of the original form:
Private Sub NewToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles NewToolStripMenuItem.Click
Dim SecondForm As New MyForm()
SecondForm.Show()
End Sub
I basically want to replicate the same behavior as opening the application when a user selects the New option from the tool strip menu.
I found a solution for what I was trying to do.
Private Sub NewToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles NewToolStripMenuItem.Click
Dim ExePath As String = Application.ExecutablePath
Process.Start(ExePath)
End Sub

Webbrowser current URL (VB.NET)

Im working on a project , and i have an obstacle about webbrowser tool in vb.net . I want to show a msgbox when the user is in a specific site , how can this be done ? , in another words , how can i get the current url in the webberowser tool in vb.net?
Webbrowser.Url is a Uri, not a string. So compare it with a Uri.
If WebBrowser1.Url = New Uri("http://stackoverflow.com") Then
I would say you should check the Host of the URI, that way it works for all of the URLs and not just the top level for a given site:
Private Sub Button1_Click_1( sender As System.Object, e As System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate("http://www.stackoverflow.com")
End Sub
Private Sub WebBrowser1_DocumentCompleted( sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If WebBrowser1.Url.Host = "stackoverflow.com"
MessageBox.Show("You are at stack overflow")
End If
End Sub
Is this what you mean?
Dim browser As String
browser = TextBox1.Text
WebBrowser1.Navigate(browser)
MsgBox("Your visiting " & browser)
End Sub