|
-
Jun 4th, 2007, 09:41 AM
#1
Thread Starter
Addicted Member
[RESOLVED] WebBrowser control help
Hi Everyone,
I have a .html file that I would like to automatically load within the WebBrowser control.
The .html file automatically sets focus to the text box within the page I need to type information into.
How do I load a local .html file into the WebBrowser control, enter some text, then press enter?
Is this doable? Any help would greatly be appreciated. Thanks!
-
Jun 4th, 2007, 10:04 AM
#2
Re: WebBrowser control help
Use .Navigate to browse to an address. I would check the .NET Code Bank submissions.
I'm pretty sure there is an example of what you are talking about or at least something very similar.
-
Jun 4th, 2007, 11:51 AM
#3
Thread Starter
Addicted Member
Re: WebBrowser control help
Hi stimbo,
How do I pass programatically a "click" to a button named "cmdClickMe"
This is the code I am using:
Code:
DirectCast(GetCurrentWebForm.item("cmdClickMe", 0), mshtml.HTMLButtonElement).click()
When I try to execute the code, the get the error:
Object reference not set to an instance of an object.
Any suggestions?
-
Jun 4th, 2007, 02:20 PM
#4
Re: WebBrowser control help
Do you mean Kleinma's code doesn't work (because it does...) or your copy of it doesn't work? Do you have a button on the webpage yu are using called "cmdClickMe" ??
If that didn't exist then you would probably get that error...
-
Jun 4th, 2007, 02:26 PM
#5
Re: WebBrowser control help
Are you using the COM webbrowser? or the built in framework 2.0 webbrowser control?
My example code in the codebank uses the activeX version of the browser control because
1) it works in 1.1 and 2.0
2) it has different methods than the 2.0 framework control that allows session state to be preserved in popups.
Now I use the 2.0 wb control sometimes when I don't care about these 2 important features, but sometimes keeping a session in a popup is a must for a given site.
All that being said, if you are using the built in control, there are other ways to perform certain actions like clicking a button. So which control are you using?
-
Jun 4th, 2007, 02:42 PM
#6
Thread Starter
Addicted Member
Re: WebBrowser control help
kleinma,
I got everything to work as it should by editing your code from http://www.vbforums.com/showthread.php?t=416275 (WebpageManipulation.zip ).
I am now having problems is moving the code into another application. What do I need to do in order to transfer your code into my application? I know I need to use the same browser as you did, but I can not figure out what I need to do to use the browser.
-
Jun 4th, 2007, 02:47 PM
#7
Re: WebBrowser control help
If you open up the references dialog in my example app, you will see what references were added to make it work.
You need to add a reference to Microsoft.MSHTML which is Microsoft's webpage parsing engine (found in the .NET tab of the add reference dialog), and also to the COM browser control (found in the COM tab of the add references dialog as Microsoft Internet Controls)
Note that you will need to distribute the 3 DLLs along with your exe that are added to your BIN folder when you set these references.
-
Jun 4th, 2007, 03:00 PM
#8
Thread Starter
Addicted Member
Re: WebBrowser control help
kleinma,
I really do not want to have to distribute dlls along with the executable. Is there a way to do this with out using the dlls? possibly a different browser? Below is the code I am using to fill the text box and click the button. Can you show me how to convert this so I do not have to distribute dlls as well?
Code:
Private Sub SetTextboxText(ByVal Text As String)
DirectCast(GetCurrentWebForm.item("ServiceTag"), mshtml.HTMLInputElement).value = Text
End Sub
Private Sub ClickSubmitButton()
GetCurrentWebForm.submit()
End Sub
-
Jun 4th, 2007, 03:04 PM
#9
Re: WebBrowser control help
if you don't want to distribute additional DLLs, then you will need to use the built in .NET 2.0 browser control which can be found in the toolbox. You will have to code things differently as well because without the reference to MSHTML, you can not cast things to the correct HTML object type.
That is if you are infact using VB 2005, if you are using a prior version, then you can't use the built in control because it didn't exist until the 2005 version.
-
Jun 4th, 2007, 03:06 PM
#10
Thread Starter
Addicted Member
Re: WebBrowser control help
Could you show me a sample on how to code what I have stated above? The name of the button on my .html form is called "cmdClickMe".
-
Jun 4th, 2007, 03:11 PM
#11
Re: WebBrowser control help
sure, all you need is this:
Code:
WebBrowser1.Document.All("cmdClickMe").InvokeMember("Click")
where WebBrowser1 is the webbrowser control on your form (remember it HAS to be the 2.0 version of the browser control to get this functionality)
-
Jun 4th, 2007, 03:17 PM
#12
Thread Starter
Addicted Member
Re: WebBrowser control help
ah, ok.. cool.
Now how would I do the text box population from this code:
Code:
Private Sub SetTextboxText(ByVal Text As String)
DirectCast(GetCurrentWebForm.item("ServiceTag"), mshtml.HTMLInputElement).value = Text
End Sub
-
Jun 4th, 2007, 03:22 PM
#13
Re: WebBrowser control help
for setting attributes (like the value property of an HTML textbox field) you use the SetAttribute method...
Code:
WebBrowser1.Document.All("ServiceTag").SetAttribute("value", "kleinma rocks")
-
Jun 4th, 2007, 03:24 PM
#14
Thread Starter
Addicted Member
Re: WebBrowser control help
Thank you for all your help. I have one more question...
Is the below code still the same to load the .htm file?
Code:
Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
wb.Navigate("C:\test1.htm")
End Sub
-
Jun 4th, 2007, 03:26 PM
#15
Re: WebBrowser control help
yes.
if you are planning on auto filling fields on the page, you should do so in the DocumentCompleted event of the browser. This is the event that fires when the page you navigated to has finished loading. If you try to do something with the page (like read or write values to it) before this event has fired, it may or may not result in an error.
-
Jun 4th, 2007, 03:28 PM
#16
Thread Starter
Addicted Member
Re: WebBrowser control help
could you show me an example of this? I agree, this would be very handy.
-
Jun 4th, 2007, 03:38 PM
#17
Re: WebBrowser control help
there is really no example to show.
when you call the .navigate() method of the browser control, it loads the specified page in the browser. When loading is done, the browser fires this event
Code:
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
End Sub
if you plan on autofilling fields on the webpage (or collecting data FROM the webpage) as soon as the page is loaded, then you should put that code in the above event. If it is something that will be done when lets say the user clicks a button on your form, then you don't need the code to be put in this event, but you should at least make sure it fired before running your code.
Basically the page has not fully loaded until that event fires.
-
Jun 4th, 2007, 04:13 PM
#18
Thread Starter
Addicted Member
Re: WebBrowser control help
OK, I understand.
When I give the command to click the button, another web page will appear. Is there a way to tell programatically when the new web page is loaded after the button click event?
-
Jun 4th, 2007, 04:14 PM
#19
Re: WebBrowser control help
the DocumentCompleted event of the webbrowser fires everytime a navigation is done, and the navigated page has completed loading. You can combine this with looking at what the current URL is of the webbrowser in order to determine what action to take when a given page loads, based on what page just finished loading.
-
Jun 4th, 2007, 04:17 PM
#20
Thread Starter
Addicted Member
Re: WebBrowser control help
Ah ok cool. Could you give me a quick example of this? Thank you for all your help.
-
Jun 4th, 2007, 04:51 PM
#21
Thread Starter
Addicted Member
Re: WebBrowser control help
I am still trying to figure out how to compare the two URLs, but I am having difficulties
-
Jun 4th, 2007, 07:06 PM
#22
Thread Starter
Addicted Member
Re: WebBrowser control help
OK I figured it out,
The below code shows how to check the URL:
Code:
If WebBrowser1.Url = New System.Uri("http://www.google.com") Then
End If
-
Jun 5th, 2007, 08:34 AM
#23
Re: [RESOLVED] WebBrowser control help
If you are doing your checking in the DocumentCompleted event, you can use the eventargs (e) that are passed in..
you can also do the comparison as strings so you don't need to waste overhead creating a new URI object just to do your comparison.
Code:
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If String.Equals(e.Url.ToString, "http://www.vbforums.com", StringComparison.CurrentCultureIgnoreCase) Then
MessageBox.Show("VB Forums")
ElseIf String.Equals(e.Url.ToString, "http://www.microsoft.com", StringComparison.CurrentCultureIgnoreCase) Then
MessageBox.Show("Microsoft")
End If
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|