VB Clicked Links Open In New Tab - vb.net

I've been searching around for a while for some code to do this, and I found a couple. most of them didn't work but I'm trying to get this one to work.
Private Sub WebBrowser1_NewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
'This creates a new tab
Dim tp As New TabPage
TabControl1.Controls.Add(tp)
'This creates a new webbrowser with the NewWindow Event
'And navigates it to the link wanting to be opened
Dim wb As New WebBrowser
Dim myElement As HtmlElement = WebBrowser1.Document.ActiveElement
Dim target As String = myElement.GetAttribute("href")
With wb
.Navigate(target)
.Dock = DockStyle.Fill
End With
AddHandler wb.NewWindow, AddressOf WebBrowser_NewWindow
tp.Controls.Add(wb)
'This prevents ie from popping up
e.Cancel = True
End Sub
But then I get a error on here WebBrowser_NewWindow, and when I check and see what it says and I am told WebBrowser_NewWindow Is Not Declared. It may be inaccessible due to protection level How am I supposed to fix this?
Full Code
Public Class Form2
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.BringToFront()
WebBrowser1.Navigate("www.google.com")
End Sub
Private Sub IClarityButton2_Click_1(sender As Object, e As EventArgs) Handles IClarityButton2.Click
If TextBox2.Text = "Close" Then
End
Else
TextBox2.Text = "Invalid"
End If
End Sub
Private Sub WebBrowser1_NewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
'This creates a new tab
Dim tp As New TabPage
TabControl1.Controls.Add(tp)
'This creates a new webbrowser with the NewWindow Event
'And navigates it to the link wanting to be opened
Dim wb As New WebBrowser
Dim myElement As HtmlElement = WebBrowser1.Document.ActiveElement
Dim target As String = myElement.GetAttribute("href")
With wb
.Navigate(target)
.Dock = DockStyle.Fill
End With
'AddHandler wb.NewWindow, AddressOf WebBrowser_NewWindow
tp.Controls.Add(wb)
'This prevents ie from popping up
e.Cancel = True
End Sub
End Class

Try removing AddHandler wb.NewWindow, AddressOf WebBrowser_NewWindow

Related

Parameter Field current value exception was Unhandled

I'm having a problem in my program that when i click the print button it always show up this error Sample error and i don't know what to do now.
this is the code, this is my parameters sample parameters
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CrystalReportViewer1.ShowPrintButton = False
For Each ctrl As Control In CrystalReportViewer1.Controls
If TypeOf ctrl Is Windows.Forms.ToolStrip Then
Dim btnNew As New ToolStripButton
btnNew.Text = "PRINT"
btnNew.ToolTipText = "Print Reciept"
CType(ctrl, ToolStrip).Items.Insert(0, btnNew)
AddHandler btnNew.Click, AddressOf printClick
End If
Next
End Sub
Private Sub printClick(sender As Object, e As EventArgs)
Dim PrintDialog As New PrintDialog
If PrintDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
CrystalReport11.PrintOptions.PrinterName = PrintDialog.PrinterSettings.PrinterName
CrystalReport11.PrintToPrinter(1, False, 0, 0) 'this is where the error coming up'
Transact()
End If
End Sub
Private Sub btnSlip_Click(sender As Object, e As EventArgs) Handles btnSlip.Click
Dim reportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument
reportDocument = New CrystalReport1
reportDocument.Refresh()
reportDocument.SetDataSource(dt)
reportDocument.SetParameterValue(0, ComboBox2.Text)
reportDocument.SetParameterValue(1, TextBox1.Text)
Form2.CrystalReportViewer1.ReportSource = reportDocument
Form2.ShowDialog()
Form2.Dispose()
Form2.WindowState = FormWindowState.Maximized
End Sub

Webscrape into Visual Studio VB Form

I've got a form that looks like the following:
I would like to simply enter the stock, click the get price button, have the button return the price it scraped.
I'm experienced with Excel VBA and how to scrape but not so much how it translates into Visual Studio (VB language)
Below is what I've got for the form so far. I attempted to add the HTML document and Internet controls methods already. So far I can get it to successfully open and shut internet explorer--I just can't get it to scrape anything. It just errors out, and I can't dim internet explorer as an HTML object. Below is the code so far...
Public Class Form1
Dim stockBox As Single
Dim price As Single
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click
End Sub
'Private Function READYSTATE_COMPLETE() As Object
'Throw New NotImplementedException()
' End Function
' Private Sub DoEvents()
'Throw New NotImplementedException()
' End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ie As Object
Dim px As Single
ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.navigate("http://finance.yahoo.com/q?s=" & (TextBox1.Text))
'Do
' DoEvents
'Loop Until ie.readyState = READYSTATE_COMPLETE
'Dim doc As HtmlDocument
'doc = ie.document
While ie.readyState <> 4
End While
On Error Resume Next
px = ie.GetElementById("yfs_l84_" & (TextBox1.Text)).InnerText
ie.Quit
End With
Label3.Text = px
End Sub
End Class

danamically creating controlltabs that can be used

I'm trying to dynamically create tabcontrols which works fine; the tabs are being created however. Once created I would also like them to become clickable and execute other code, this now posing a problem.
The code I’m using to create a tab is as follows
' do whatever wtih filename
Dim myTabPage As New TabPage()
myTabPage.Text = TextBox4.Text
TabControl1.TabPages.Add(myTabPage)
TabPage1.Hide()
Not so nice cause I can now fill my form with as many tabs as I like however none of them can be clicked to execute futher code???
EDIT:
Private Sub TabControl_SelectedIndexchaged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
Dim TabName As String
TabName = TabControl1.SelectedTab.Name
If TabName.Contains("TabPage") Then
' Do something
MsgBox("new tab created")
End If
End Sub
You have to add an event handler for the TabPage click event:
Dim myTabPage As New TabPage
myTabPage.Text = TextBox4.Text
AddHandler myTabPage.Click, AddressOf TabPage1_Click
TabControl1.TabPages.Add(myTabPage)
Which will call this code:
Private Sub TabPage1_Click(sender As Object, e As EventArgs)
MessageBox.Show(DirectCast(sender, TabPage).Text)
End Sub
Per your edit, you would have to add the name property:
myTabPage.Name = TextBox4.Text
And your SelectedIndexChanged event:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedTab IsNot Nothing Then
MessageBox.Show(TabControl1.SelectedTab.Name)
End If
End Sub

How Do I Open a New Webbrowser Tab in Vb.net application

I have a Form1 with button1 and a webbrowser1. When I click on button1, I want to open a new web browser tab in the same form, not in Firefox or Internet Explorer or Chrome.
I tried using TabControl but am not sure how that works since it does not resize and its kind of annoying. I just want to open a new tab with web browser in the form.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim wb As New WebBrowser
wb.Navigate("www.google.com")
Dim tab As New TabPage("Title")
tab.Controls.Add(wb)
TabControl1.TabPages.Add(tab)
TabControl1.SelectedTab = tab
tab.Size = New System.Drawing.Size(280, 174)
End Sub
End Class
To add a new Tabbed browser, first u need to add a new Tab to your existing TabControl,
once the new Tab is added, then you need to add a new browser control into the created Tab
Private Sub btnAddTab_Click(sender As Object, e As EventArgs)
Dim page As New TabPage(String.Format("Tab # {0}", tabControl1.TabPages.Count + 1))
tabControl1.TabPages.Add(page)
Dim browser As New WebBrowser()
page.Controls.Add(browser)
browser.Dock = DockStyle.Fill
browser.Navigate(New Uri("http://www.google.co.in"))
End Sub
Create your own TabPage so that you can handle events and controls easly:
Public Class WBTab
Inherits TabPage 'it actually is a tabpage
Public WithEvents WB As New WebBrowser 'that has a single webbrowser in it
Sub New(ByVal URL As String) 'when the page is created, show it and load the URL
WB.Dock = DockStyle.Fill
Me.Controls.Add(WB)
WB.Navigate(URL)
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WB.DocumentCompleted
Me.Text = WB.DocumentTitle 'when the page is loaded you may now show its title in your tab.
End Sub
Private Sub WB_Navigating(sender As Object, e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WB.Navigating
Me.Text = e.Url.ToString
End Sub
End Class
Now it is ready to be used:
Dim google As New WBTab("google.com") 'create a new tab with URL
TabControl1.TabPages.Add(google) 'show it
This should work:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim tabpage As New TabPage
tabpage.Text = "New Tab"
TabControl1.TabPages.Add(tabpage)
Dim webBrowser As New WebBrowser
TabControl1.SelectedTab = tabpage
tabpage.Controls.Add(webBrowser)
webBrowser.Dock = DockStyle.Fill
webBrowser.Navigate("http://www.stackoverflow.com")
End Sub

VB.NET ~ How can I encapsulate my code to be used over and over?

I know I seem to ask the same types of questions but with much trial and error I finally got my app to do what I needed it to do. I have a form with a button on it. When clicked it creates a form, a web browser, a picture box, a text box, and a button.
The web browser, picture box, textbox, and the button are all placed onto this form at run time.
My question is now how can I encapsulate this to be used over and over again. I've tried just putting this into a class and creating an instance of the class but I keept getting an error on my document completing stating my code isn't referring to an object.
But here is my code.
Thanks
Public Class CaptchaWindow
Dim myform As New Form
Dim webbrowser As New WebBrowser
Dim picturebox As New PictureBox
Dim textbox As New TextBox
Dim submitbtn As New Button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ProfileMaker.Show()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call myspecs()
AddHandler submitbtn.Click, AddressOf captchasubmit
AddHandler webbrowser.DocumentCompleted, AddressOf webcompleteddesignfloat
webbrowser.Navigate("http://www.designfloat.com/register/")
End Sub
Sub myspecs()
'FORM SIZE AND LOCATION
myform.Size = New Drawing.Size(1542, 771)
myform.Location = New Drawing.Point(15, 15)
'WEBBROWSER SIZE AND LOCATION
webbrowser.Size = New Drawing.Size(1000, 577)
webbrowser.Location = New Drawing.Point(12, 181)
webbrowser.ScriptErrorsSuppressed = True
'PICTUREBOX SIZE AND LOCATION
picturebox.Size = New Drawing.Size(299, 83)
picturebox.Location = New Drawing.Point(12, 12)
picturebox.BorderStyle = BorderStyle.Fixed3D
'TEXBOX SIZE AND LOCATION
textbox.Size = New Drawing.Size(299, 20)
textbox.Location = New Drawing.Point(12, 101)
'BUTTON SIZE AND LOCATION
submitbtn.Size = New Drawing.Size(75, 23)
submitbtn.Location = New Drawing.Point(118, 127)
submitbtn.Text = "Submit"
myform.Controls.Add(webbrowser)
myform.Controls.Add(picturebox)
myform.Controls.Add(textbox)
myform.Controls.Add(submitbtn)
myform.Show()
End Sub
Sub webcompleteddesignfloat()
For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("input")
If element.GetAttribute("name") = "reg_username" Then
element.SetAttribute("value", ProfileMaker.Username.Text)
End If
Next
For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("input")
If element.GetAttribute("name") = "reg_email" Then
element.SetAttribute("value", ProfileMaker.Email.Text)
End If
Next
For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("input")
If element.GetAttribute("name") = "reg_password" Then
element.SetAttribute("value", ProfileMaker.Password.Text)
End If
Next
For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("input")
If element.GetAttribute("name") = "reg_password2" Then
element.SetAttribute("value", ProfileMaker.Password.Text)
End If
Next
If webbrowser.ReadyState = WebBrowserReadyState.Complete Then
For Each Captcha As HtmlElement In webbrowser.Document.Images
If Captcha.GetAttribute("src").Contains("http://www.google.com/recaptcha/api/image? c=") Then
picturebox.Load(Captcha.GetAttribute("src"))
End If
Next
End If
End Sub
Sub captchasubmit()
If webbrowser.ReadyState = WebBrowserReadyState.Complete Then
For Each Captcha As HtmlElement In webbrowser.Document.Images
If Captcha.GetAttribute("src").Contains("http://www.google.com/recaptcha/api/image?c=") Then
picturebox.Load(Captcha.GetAttribute("src"))
End If
Next
End If
For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("input")
If element.GetAttribute("id") = "recaptcha_response_field" Then
element.SetAttribute("value", textbox.Text)
End If
Next
For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("button")
If element.GetAttribute("name") = "submit" Then
element.InvokeMember("click")
End If
Next
myform.Dispose()
End Sub
The below part is when I tried to create an instance of this from a class.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim mystuff As New CaptchaClass
mystuff.Handler()
mystuff.captchasubmit()
mystuff.webcompleteddesignfloat()
End Sub
End Class
You should Imports your class namespace to the current scope (page) at the beggining of the code
Imports Namespace.CaptchaWindow
Class Page1
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim mystuff As New CaptchaClass
End Sub
End Class
Also look at Namespaces if you doesn't know what it is.