how to automatically click search button in google visual basic . net - vb.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.

Related

Opening a Form from UserControl and pass a Value to Form

I have a small problem, I have UserControl1 containing a Button and a TextBox. I've an instance of the user control on Form1 and I want when I click the button of user control, Form2 get opened and the text from the textbox of user control appear in TextBox1 if Form2.
NineBerry give me this example Link but I can't solve the problem yet using the link, any help please :)
This how i Don it, Thanks for my Friend Reza Aghaei for helping.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New SbillPrint
frm.smoney_txt.Text = moneys_txt.Text
frm.ShowDialog()
End Sub

Get id of element (in webbrowser) on Mouseover in vb.net

In vb.net,
In my form, i have a webbrowser and a textbox.
my question is :
Is it possible to get the id of any element in my webbrowser (especialy the id of a cell of a table) and to copy it in my textbox :
-when I click on the element
or when my mouse is over the element ...
as in firefox when u right click on a element and you click "inspect element" and u get the HTML code(so the ID too) where the mouse is...
I hope i was clear enough, if not, you can ask me more question.
Thanks a lot
best regards
To get id of element on MouseOver, you must be :
1- Declare a HtmlDocument.
2- Assign the WebBrowser Document to HtmlDocument.
3- Create an event handler that retrieves id element on MouseOver.
4- Assign the event handler to HtmlDocument.
As following :
Dim htmlDocument As HtmlDocument
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
htmlDocument = WebBrowser1.Document
AddHandler htmlDocument.MouseOver, AddressOf Document_MouseOver
End Sub
Private Sub Document_MouseOver(sender As Object, e As HtmlElementEventArgs)
TextBox1.Text = TryCast(sender, HtmlDocument).GetElementFromPoint(e.ClientMousePosition).GetAttribute("id")
End Sub

WebBrowser Visual Basic

How do I stop a new tab from opening google.com automaticaly in visual basic. I need a blank url textbox
This is my code for new tab
Private Sub AddTabToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AddTabToolStripMenuItem.Click
Dim tab As New TabPage
Dim newtab As New tab
newtab.Show()
newtab.TopLevel = False
newtab.Dock = DockStyle.Fill
tab.Controls.Add(newtab)
Form1.TabControl1.TabPages.Add(tab)
Form1.TabControl1.SelectedTab = tab
And this is the code when form loads
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim t As New TabPage
Dim newtab As New tab
newtab.Show()
newtab.TopLevel = False
newtab.Dock = DockStyle.Fill
t.Controls.Add(newtab)
TabControl1.TabPages.Add(t)
End Sub
If you wanted you could allow the user to set their own homepage.
Simply create a button, or an item in the drop down bar that says "Set Homepage"
Create a new setting for your web browser called homePage, set it to string. Do nothing else.
Next create a new form called Homepage and link it to the button you created in step one by double clicking the button and putting
homepage.show()
in the code for that button.
On the other form you created you need a text box, and a button that says save.
double click the button and put this code in the button's code block.
textbox1.text = my.settings.homePage
my.settings.save()
Finally on your original browser form, under form1_load put this line of code
webbrowser1.navigate(my.settings.homePage)
That should work, it did for my browser at least
It won't open a page until you actually set it to open a page. Somewhere in your code you have something that will look similar to
webbrowser1.navigate("www.google.com")
Most likely within your form_load event. Just delete that.

WebBrowser Control (VB.NET) new tab

I have a problem with WebBrowser Control in vb.net (Windows form application) . The problem is when i click on a hyperlink that opens it new tab , it opens new Internet Explorer window? How can I make to open new tab in my tab control instead of Internet Explorer ? I searched online , but i only found results for c# . For example this result Open link in new TAB (WebBrowser Control)
The code in question from the other link.........
Private Sub InitializeBrowserEvents(SourceBrowser As ExtendedWebBrowser)
SourceBrowser.NewWindow2 += New EventHandler(Of NewWindow2EventArgs)(AddressOf SourceBrowser_NewWindow2)
End Sub
'
Private Sub SourceBrowser_NewWindow2(sender As Object, e As NewWindow2EventArgs)
Dim NewTabPage As New TabPage() With { Key .Text = "Loading..." }
Dim NewTabBrowser As New ExtendedWebBrowser() With { Key .Parent = NewTabPage, Key .Dock = DockStyle.Fill, Key .Tag = NewTabPage }
'
e.PPDisp = NewTabBrowser.Application
InitializeBrowserEvents(NewTabBrowser)
'
Tabs.TabPages.Add(NewTabPage)
Tabs.SelectedTab = NewTabPage
End Sub
'
Private Sub Form1_Load(sender As Object, e As EventArgs)
InitializeBrowserEvents(InitialTabBrowser)
End Sub
Please note, I used THIS link to convert code...
http://www.developerfusion.com/tools/convert/csharp-to-vb/?batchId=2183e979-2b56-4c82-a7d5-c0822e7f0bca
The easiest solution is to add my Tabbed_EI .dll to your tool box and link your application button clicks and other events to Tabbed_EI's public/global variables, objects, and Subs. Located here
You can otherwise use the Webbrowser.newWindow event and add
"e.cancel = true"

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