VB6 - Fill form Sync example
File(s): frmFillFormSyncDemo.frm, html\loginDemo.html, html\thankYou.html
This example shows how-to use the document interface with Visual Basic using synchronous methods and our document object.
There's also a Fill Form Example that uses asynchronous 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 AntViewAx2.AntViewDocument
In Form_Initialize we create the document object instance.
Private Sub Form_Initialize()
Debug.Print "Form initialize"
Set Document = CreateObject("AntViewAx2.AntViewDocument", "")
Document.BrowserDispatch EdgeWebBrowser.IDispatchPointer
EdgeWebBrowser.UnlockControl "ExampleCompany", "WI5PO2-2KSU3Q-HWFXFU-IUMU2V-QF8P2F"
EdgeWebBrowser.NextFocusWindowHandleHwnd = GoButton.hwnd
EdgeWebBrowser.PreviousFocusWindowHandleHwnd = GetHtmlButton.hwnd
End Sub
Private Sub Form_Activate()
Dim HResult As Long
Dim Err As Long
Err = EdgeWebBrowser.CreateWebViewSync(HResult)
If Err = 0 Then
LoadHtmlForm
Form_Resize
End If
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.
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 NavigateSync and subsequently enable the disabled buttons if the navigation succeeded.
Private Sub GoButton_Click()
Dim HtmlFile As String
Dim Path As String
Dim IsSuccess As Boolean
Dim WebErrStatus As TxWebErrorStatus
Dim Err As Long
' Load the html from file loginDemo.html
Path = strGetVisualBasicDemoPath
HtmlFile = Path & "\html\loginDemo.html"
Err = EdgeWebBrowser.NavigateSync(HtmlFile, IsSuccess, WebErrStatus)
If (Err = 0) And (IsSuccess = True) Then
FillFormButton.Enabled = True
ReadFormButton.Enabled = True
SubmitButton.Enabled = True
ZoomInButton.Enabled = True
ZoomOutButton.Enabled = True
End If
'Debug.Print EdgeWebBrowser.LastErrorCode
End Sub
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()
Dim Err As Long
Err = Document.SetElementValueByNameSync("username", "Mr. Smith")
Debug.Print ("Setting Element 'username' error = " & CStr(Err))
Err = Document.SetElementValueByNameSync("password", "supersecret")
Debug.Print ("Setting Element 'password' error = " & CStr(Err))
Err = Document.SetElementValueByNameSync("your-none-existing-input", "smith@example.com")
Debug.Print ("Setting Element 'your-none-existing-input' error = " & CStr(Err))
Err = Document.SetInputCheckboxByNameSync("remember", True)
Debug.Print ("Setting checkbox for 'remember' error = " & CStr(Err))
End Sub
The method SetElementValueByNameSync takes the form input name as the first parameter and the second parameter has the value you want it to receive.
As the method returns immediately after the method, you can display any errors that might have happened.
For example the third line with name "your-none-existing-input" will return error 1, because there is no element with that name.
The line with method SetInputCheckboxByNameSync 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()
Dim Value As String
Dim Err As Long
Dim Exists As Boolean
Dim Checked As Boolean
Err = Document.RequestElementValueByNameSync("username", Value)
If (Err = 0) Then
Debug.Print ("Name 'username' = " & Value)
End If
Err = Document.RequestElementValueByNameSync("password", Value)
If (Err = 0) Then
Debug.Print ("Name 'password' = " & Value)
End If
Err = Document.RequestElementAttributeByNameSync("username", "placeholder", Value)
Debug.Print ("Element 'username' attribute 'placeholder' has value '" & Value & "' error = " & CStr(Err))
Err = Document.ElementHasAttributeByNameSync("username", "required", Exists)
Debug.Print ("Element 'username' attribute 'required' exists '" & CStr(Exists) & "' error = " & CStr(Err))
Err = Document.ElementToggleAttributeByNameSync("username", "required")
Err = Document.RequestInputCheckboxByNameSync("remember", Checked)
Debug.Print ("Checkbox name 'remember' state = " & CStr(Checked) & " error = " & CStr(Err))
End Sub
Here we use the RequestElementValueByNameSync function and the result of that function is immediately returned in the 2nd parameter. 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.
Using RequestElementAttributeByNameSync we can query the contents of one of the attributes of the username input element.
Element 'username' attribute 'placeholder' has value 'User Name' error = 0
The line with ElementHasAttributeByNameSync inspects the username input element to see if it has the attribute "required", which it does.
The output is:
Element 'username' attribute 'required' exists 'True' error = 0
The following line in the code:
Document.ElementToggleAttributeByNameSync("username", "required")
will remove the required attribute, so if you press the button again, the output of the ElementHasAttributeByNameSync method then produces:
Element 'username' attribute 'required' exists 'False' error = 0
The last line RequestInputCheckboxByNameSync queries the state of the checkbox and returns that state as such:
Checkbox name 'remember' state = True error = 0
AntView - The MS Edge WebView2 ActiveX control Date last changed: 09/30/2024