VB6 - Fill form example
This example shows how-to use the document interface with Visual Basic using asynchronous methods and our document object.
There's also an Fill form Sync example that uses synchronous methods, to give an idea of the differences in programming style.
At the top of the form we declare our document object that we are going to use.
Option Explicit
Public WithEvents Document As AntviewAx.AntViewDocument
In Form_Initialize we create the document object instance.
Private Sub Form_Initialize()
Debug.Print "Form initialize"
Set Document = CreateObject("AntViewAx.AntViewDocument", "")
Document.BrowserDispatch EdgeWebBrowser.IDispatchPointer
EdgeWebBrowser.UnlockControl "ExampleCompany", "WI5PO2-2KSU3Q-HWFXFU-IUMU2V-QF8P2F"
EdgeWebBrowser.EventsUseHexadecimal = True
EdgeWebBrowser.NextFocusWindowHandleHwnd = GoButton.hwnd
EdgeWebBrowser.PreviousFocusWindowHandleHwnd = GetHtmlButton.hwnd
End Sub
Private Sub Form_Load()
EdgeWebBrowser.CreateWebView
Form_Resize
End Sub
It is required to connect the document object with the browser object. This can be done with the BrowserDispatch method like above.
We also added code to adjust the default Next/Previous tab behavior so that you can navigate out of the control with the keyboard and not rotate within. By pressing tab continuously the focus will go to the "Go" button whereas when you shift+tab repeatedly you will end up at the "Get Html" button.
Another thing in the initialisation code is that EventsUseHexadecimal is set to true so that we can handle the OnNavigationCompleted event.
First press the "Go" button in the view to open the web page.
The relevant part in the html looks like this:
<form id="login" class="login" action="thankYou.html">
<div class="loginName">
<label for="username">UserName</label>
<input type="text" name="username" placeholder="User Name" required />
</div>
<div class="loginPassword">
<label for="password">Password</label>
<input type="password" name="password" placeholder="Password" required />
</div>
<div class="loginRemember">
<input type="checkbox" name="remember" value="memory" />
<label for="remember">Remember UserName</label>
</div>
<div class="loginBtn">
<button id="submit" type="submit" name="submit">Login</button>
</div>
</form>
Pressing the "Go" button will load the html above using Navigate and subsequently trigger that OnNavigationCompleted event that we just mentioned.
Private Sub EdgeWebBrowser_OnNavigationCompletedHex(ByVal IsSuccess As Boolean, ByVal WebErrorStatus As AntViewAx.TxWebErrorStatus, ByVal NavigationIdHex As String)
If IsSuccess = True Then
FillFormButton.Enabled = True
ReadFormButton.Enabled = True
SubmitButton.Enabled = True
ZoomInButton.Enabled = True
ZoomOutButton.Enabled = True
End If
End Sub
If the page was navigated successfully, then it will enable a bunch of buttons.
You can now use the "Fill Form" button to add data to the html inputs in the screen.
The click event in the "Fill Form" button has this code:
Private Sub FillFormButton_Click()
Document.ElementValueByName("username") = "Mr. Smith"
Document.ElementValueByName("password") = "supersecret"
Document.ElementValueByName("your-none-existing-input") = "smith@example.com"
Document.SetInputCheckboxByName "remember", True
End Sub
The property ElementValueByName takes the form input name as parameter and you assign it the value you want it to receive. There's a matching OnSetElementValueByName event that triggers on completion.
You can check that event to see if errors happened. For example the third line with name "your-none-existing-input" will return error 1, because there is no input element with that name.
The line with method SetInputCheckboxByName will put a tick in the "remember" checkbox
The total output in the debug screen looks like:
Setting Element 'username' error = 0
Setting Element 'password' error = 0
Setting Element 'your-none-existing-input' error = 1
Setting checkbox for 'remember' error = 0
You can also read the contents of any of the html elements.
See the click event of the "Read Form" button for this:
Private Sub ReadFormButton_Click()
' the result of this comes via the event:
' Document_OnRequestElementValueByName
Document.RequestElementValueByName "username"
Document.RequestElementValueByName "password"
Document.RequestElementAttributeByName "username", "placeholder"
Document.ElementHasAttributeByName "username", "required"
Document.ElementToggleAttributeByName "username", "required"
Document.RequestInputCheckboxByName "remember"
End Sub
Here we use the RequestElementValueByName function and the result of that function is passed back in the OnRequestElementValueByName event. This can be used to read the contents of what the user entered into those input fields or what we put in using the FillFormButton_Click method.
Private Sub Document_OnRequestElementValueByName(ByVal Name As String, ByVal Value As String, ByVal Error As Long)
Debug.Print ("Name " & Name & " = " & Value)
End Sub
Using RequestElementAttributeByName we can query the contents of one of the attributes of the username input element.
Private Sub Document_OnRequestElementAttributeByName(ByVal Name As String, ByVal AttrName As String, ByVal Value As String, ByVal Error As Long)
Debug.Print ("Element '" & Name & "' attribute '" & AttrName & "' has value '" & Value & "' error = " & CStr(Error))
End Sub
In this case the event OnRequestElementAttributeByName will return:
Element 'username' attribute 'placeholder' has value 'User Name' error = 0
The next line with ElementHasAttributeByName inspects the username input element to see if it has the attribute "required", which it does.
The result of that method can be seen via OnElementHasAttributeByName.
Private Sub Document_OnElementHasAttributeByName(ByVal Name As String, ByVal AttrName As String, ByVal Exists As Boolean, ByVal Error As Long)
Debug.Print ("Element '" & Name & "' attribute '" & AttrName & "' exists '" & CStr(Exists) & "' error = " & CStr(Error))
End Sub
and the output is:
Element 'username' attribute 'required' exists 'True' error = 0
The following line in the code:
Document.ElementToggleAttributeByName "username", "required"
will remove the required attribute, so if you press the button again, the output of the ElementHasAttributeByName method then produces:
Element 'username' attribute 'required' exists 'False' error = 0
The last line RequestInputCheckboxByName queries the state of the checkbox and returns that state in OnRequestInputCheckboxByName.
Private Sub Document_OnRequestInputCheckboxByName(ByVal Name As String, ByVal Checked As Boolean, ByVal Error As Long)
Debug.Print ("Checkbox name '" & Name & "' state = " & CStr(Checked) & " error = " & CStr(Error))
End Sub
as such:
Checkbox name 'remember' state = True error = 0
AntView - The MS Edge WebView2 ActiveX control Date last changed: 09/25/2024