You need to know:
-How to use settings
-Some knowledge of how HTML works
A little explanation before we get started:
Most HtmlElements (say a textbox, or a button) have certain properties (such as 'vlaue', or 'id'). Normally most password textboxes on webpages have an id such as 'Passwd', 'Pass', or 'Password'. So what we need our application to do is to look for an HtmlElement in the webbrowser document with an id of 'Password', or something like that. Once we have found this element, we can assume that it is the element which we're looking for, we need to set the 'value' attribute (The one which dictates what the element displays) to whatever password we need to remember. The code which I'm going to use to do this isn't the most beautiful in the world, but it works.
[For those who allready have a webbrowser and just want the code for the password setting, please jump to section 2: Setting Password]
1: Creating Your Form
So first of all create a new project, add a textbox, 2 buttons and a webbrowser to your form.
Name the webbrowser wbMain
Name the textbox txtURL
Name the first button btnLoadPassword
Name the second button btnNavigate
Now double click on btnNavigate, and enter this code into the Click event for the button:
2: Setting Password
Ok, so now we want to be able to remember passwords.
Go to the application settings in 'My Application' anmd create a StringCollection (System.Collections.Specialized.StringCollection). Set the scope to Application, as we don't want users to modify it. Now set the strings in it to:
So now go back to the windows forms designer for your main form, and double click 'btnLoadPassword'
Put this code in the Click event for the button:
'Now for each HtmlElemtn in the webbrowser document (it could be a textbox, a button, or whatever) For Each ele As HtmlElement In WebBrowser1.document.All 'For each setting in the list of password field IDs For Each item In My.Settings.PasswordFields 'If the ID of the element matches the the ID stored in the setting If ele.GetAttribute("id").ToLower = item.ToLower Then 'Get the password from the user Dim Password as String = Inputbox("Please enter your password here") Then set the element's value to the password... ele.SetAttribute("value", Password) End If Next Next
Now press F5 to debug, and go to www.gmail.com with your browser. Press the btnLoadPassword, and an inputbox should show, asking for your password. Enter your gmail password, and then press ok. The password box should be set to your password. This works on numerous other websites, such as Facebook and Youtube. It does not work on Hotmail.com, as the id of the password textbox is randomly generated.
So what did we actually do?
So for every HtmlElement in the Webbrowser document
We check if the id attribute of the element is included in the password string array which we made
if that is true we then set its attribute to whatever the user enters into the textbox.
Nothing is done to other elements which are encountered
In this tutorial I only showed you how to fill out the form with a string collected at runtime, but it shouldn't be very difficult to adapt the code so that it loads the password from a file. This tutorial is a basis for a password remembering system in your browser.
I plan to release a turorial in the next few months (it could be weeks or even days, depending on how bored I get) which will show you how to show an information bar on your form asking if a user wants the program to remember their password after they have logged into a website. DreamInCode may not allow this, because it could be used to steal passwords...
Anyway, I hope you enjoyed my tutorial, and feel free to post any questions or bugs down below
This post has been edited by Jack Eagles1: 26 April 2010 - 08:12 AM