Simulate web search using the WebBrowser control - vb.net

I am trying to write a program that simulates a simple browser search in www.google.com using the WebBrowser control. I'm really just wanting to simulate internet activity.
I came up with the idea of using a loop to send a number to the google search box and then pressing enter.
The line WebBrowser1.Document.GetElementById("q").SetAttribute("value", i) successfully sends each number in the loop to the google search box, but the next line WebBrowser1.Document.GetElementById("btnK").InvokeMember("Click") won't initiate the google search button. I don't get any errors.
Does anyone have any ideas why WebBrowser1.Document.GetElementById("btnK").InvokeMember("Click") doesn't work?
Also I've noticed that when I run this code and then launch Internet Explorer, the code stops. Does anyone have any ideas on this as well?
Most grateful for any help!
Regards
George
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call LoadBrowser()
End Sub
Private Sub LoadBrowser()
WebBrowser1.Navigate("http://www.google.com/")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Send search string 'i' to browser n times
Dim i As Integer
For i = 1 To 100
' Browser search
WebBrowser1.Document.GetElementById("q").SetAttribute("value", i)
WebBrowser1.Document.GetElementById("btnK").InvokeMember("Click")
' Pause n seconds before next loop
For x As Integer = 0 To 5 * 100 ' Pause for 5 seconds
Threading.Thread.Sleep(10)
Application.DoEvents()
Next
Next
End Sub

Ok, working off the example you have given us.
Try this.
Imports WindowsInput '' <---------
''' <summary>
''' This program will run quick slow, would be best to use another thread for it, I'll leave that to you.
''' </summary>
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("www.google.com.au")
End Sub
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim SIM = New InputSimulator() ''Use NuGet package manager and search for Input Simulator at Import it at the top
For i = 0 To 100
WebBrowser1.Document.GetElementById("lst-ib").InvokeMember("Click") ''this will click on googles searchbox
WebBrowser1.Document.GetElementById("q").InnerText = i
Await Task.Delay(500) ''This will delay your code so you can see what is in the searchbox, I prefer this over Thread.Sleep, each to their own I guess.
WebBrowser1.Document.GetElementById("q").InvokeMember("Click") ''This will click on googles search box to get the delete simulator ready.
SIM.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.DELETE) '' This will delete whatever is in the searchbox since your original code was doing this and you didn't complain about it
Next
WebBrowser1.Document.GetElementById("lst-ib").InvokeMember("Click") ''ONCE the loop as completed it will click on googles search box
SIM.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.RETURN) '' Another keypress for the enter key (MAY NOT WORK DEPENDING IF YOU HAVE CLICK OFF THE WEBBROWSER CONTROL) maybe you can tab back to it?
'WebBrowser1.Document.GetElementById("sblsbb").InvokeMember("Click") ''if the above line doesn't work
End Sub
End Class
Sorry for the long comments, I'm just trying to help you understand what the code is doing and the possible errors you may or may not encounter along with possible fixes and or work arounds.Good luck.

Related

How to open two applications using one button?

Hi sorry i am beginner in vb.net Can you please help me how to open 2 applications using 1 button and what code should i use?
I already try this code in 1 application
shell("C:\Windows\System32\calc.exe") and it's work.
This really depends on what you'd like to be done when clicking the button.
If you want the button to open two applications at the same time, then you could just call 2 Process.Start() functions.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim p As Process = Process.Start("PATH for first application.")
Dim x As Process = Process.Start("PATH for second application.")
End Sub
However, if you'd like to run two separate applications and have the second application wait until the first is closed then you can use the .WaitForExit() method. For example:
Private Sub Button1_Click(sender as Object, e As EventArgs) Handles Button1.Click
Dim p As Process = Process.Start("PATH for first application.")
p.WaitForExit()
Dim x As Process = Process.Start("PATH for second application.")
x.WaitForExit()
End Sub

Saving a setting to MySettings and exiting the program isn't working

I'm having a problem with saving a setting to My Project and then exiting my program with an 'End' statement. If I save the setting but don't execute the end statement, everything works. If I save the setting and then execute the 'End', the setting doesn't get saved. Here's some code that illustrates the problem:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'reads the last setting correctly
TextBox1.Text = My.Settings.MySetting
End Sub
Private Sub btnWrite_Click(sender As Object, e As EventArgs) Handles btnWrite.Click
'write value, don't exit; works
My.Settings.MySetting = TextBox1.Text
End Sub
Private Sub btnWriteEnd_Click(sender As Object, e As EventArgs) Handles btnWriteEnd.Click
'write value and end; fails
My.Settings.MySetting = TextBox1.Text
End
End Sub
End Class
When I execute the code, whatever was last in My.Settings.MySetting appears in TextBox1. If I change the text in the textbox and click on the 'Write' button and manually exit the program by clicking on the 'X', the new text appears properly changed when I execute the program again. If I change the text and exit programmatically by clicking on 'WriteEnd', the changed setting text doesn't get written to 'MySetting'.
What am I doing wrong?
Thanks
Settings will be saved automatically at shutdown by default, so there's generally no need to call Save. End is definitely the issue. NEVER use End. Call Close on the startup form or call Application.Exit. I compare End with a bouncer grabbing someone by the scruff of the neck and throwing them out, spilling their drink on everyone and leaving their jacket behind, rather than asking them to leave of their own accord.

Improve UI responsiveness on windows form application

I am currently working on a project and decided to create a user interface for it using visual studio with a windows forms application(Visual Basic).
The problem I'm facing is that the user interface doesn't respond as quickly and smoothly as I'd like it to.
Mainly, I am using pictures as buttons to make the user form look more modern.
However, when I hover my mouse over a "button" it takes a while until the "highlighted button" appears.
P1 is the picture of the "normal button" and P2 is the picture of the "highlighted button".
Here is the short code I have for now:
Public Class Main
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub PictureBox1_MouseHover(sender As Object, e As EventArgs) Handles P1.MouseHover
P1.Visible = False
P2.Visible = True
End Sub
Private Sub P2_MouseClick(sender As Object, e As MouseEventArgs) Handles P2.MouseClick
'Call cmdInit()
'Call cmdConnectRobot()
'Call cmdUnlock()
End Sub
Private Sub Main_MouseHover(sender As Object, e As EventArgs) Handles Me.MouseHover
If P2.Visible = True Then
P2.Visible = False
P1.Visible = True
End If
End Sub
Private Sub P4_Click(sender As Object, e As EventArgs) Handles P4.Click
End Sub
End Class
Another problem I'm facing is that when I call other subs, the user form becomes unresponsive while the sub is running.
I researched and found that I could implement multi threading or async tasks but I'm a bit lost and would be extremely grateful if someone could guide me or point me in the right direction.
Thanks in advance!!
In this case your UI is responsive, however the MouseHover event is only raised once the mouse cursor has hovered over the control for a certain amount of time (default is 400 ms), which is what is causing the delay.
What you are looking for is the MouseEnter event, which is raised as soon as the cursor enters ("touches") the control:
Private Sub P1_MouseEnter(sender As Object, e As EventArgs) Handles P1.MouseEnter
P1.Visible = False
P2.Visible = True
End Sub
You can then use that together with the MouseLeave event on the second picture box to switch back to the non-highlighted image:
Private Sub P2_MouseLeave(sender As Object, e As EventArgs) Handles P2.MouseLeave
P1.Visible = True
P2.Visible = False
End Sub
However switching picture boxes like this is not optimal. I recommend you to look into how you can use Application Resources, then modify your code to only switch the image that one picture box displays.
Here are the basic steps:
Right-click your project in the Solution Explorer and press Properties.
Select the Resources tab.
To add an image either:
a. Drag and drop the image onto the resource pane.
b. Click the arrow next to the Add Resource... button and press Add Existing File....
Now, in your code add this right below Public Class Form1:
Dim ButtonNormal As Image = My.Resources.<first image name>
Dim ButtonHighlighted As Image = My.Resources.<second image name>
Replace <first image name> and <second image name> with the names of your button images.
Now you only need one picture box for the button:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
P1.Image = ButtonNormal
End Sub
Private Sub P1_MouseEnter(sender As System.Object, e As System.EventArgs) Handles P1.MouseEnter
P1.Image = ButtonHighlighted
End Sub
Private Sub P1_MouseLeave(sender As System.Object, e As System.EventArgs) Handles P1.MouseLeave
P1.Image = ButtonNormal
End Sub
I'll start by saying i'm not a programmer by trade, and i'm sure someone will point out better ways of doing these things, but in regards to the threading question it's fairly simple to implement.
Imports System.Threading
Public Class Form1
Dim WorkerThread As New Thread(AddressOf DoWork)
'WorkerThread' can be any name you like, and 'DoWork' is the name of the sub you want to run in the new thread, and is launched by calling:
WorkerThread.start()
However there is a catch, the new thread is not able to interact directly with the GUI, so you cannot change textbox text etc... I find the easiest way to get changes made to the GUI is to drag a timer onto your form, and have the new thread change variables (pre-defined just below Public Class Form1), then use the Timer1 Tick event to monitor the variables and update the GUI if there are any changes.

Enabling the use of Bing between two forms using a Button, Web browser & Textbox

Hi I have written a piece of a code which takes the postcode from my textbox then searches it within Bing for the user, at the moment all of it is on Form1 and works as it should, I'm looking to improve on the idea by allowing the user to press a button on Form1 then the web browser appearing in Form2, so far my attempts to figure this out have failed and was wondering if anyone had any ideas. Just as a side note this a slow progression of my programming as I'm a beginner and in the future I will be looking into implementing a Google or Bing API
I have attached a copy of my code which all works within in one Form
Public Class Form1
Dim Automate As Boolean
Private Sub BTNMap_Click_1(sender As Object, e As EventArgs) Handles BTNMap.Click
Automate = True
WebBrowser1.Navigate("http://www.bing.com")
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If Automate = True Then Automate = False Else Exit Sub
Dim txt As HtmlElement = WebBrowser1.Document.GetElementById("q")
Dim btn As HtmlElement = WebBrowser1.Document.GetElementById("go")
txt.SetAttribute("value", PostcodeTextBox.Text)
btn.InvokeMember("click")
End Sub
Thank you for reading any advice is welcome.

Why dont the Command Button excecute the form

Good day. I Created a form that worked perfectlt, after testing the Command Button a few times and entering information into the form I decided to straiten out the form and make it tidy. after saving the form i tried to click the Command butten but this time it gave me a Error 424. Object Required.
(I tried to upload pictures with no success)
When I Check the Debug it highlight the .show command.
Private Sub CommandButton1_Click()
ClaimUserForm.Show
End Sub
I also tried:
Private Sub CommandButton1_Click()
Dim Claim as ClaimUserForm
Set Claim = new ClaimUser
claim.show
End Sub
But the debug re-appear.
in the form property window the form name is ClaimUserForm
Please help, I cant understand why it suddenly gave this problem.
Thanks
Is your button working properly? It seems it's missing the sender-object and the events.
Also set is not necessary.
Private Sub CommandButton1_Click(sender As Object, e As EventArgs) Handles CommandButton1.Click
Dim Claim As ClaimUserForm = New ClaimUser
Claim.Show
End Sub
sender As Object, e As EventArgs and Handles Button1.Click are missing.