
Accessing Unexposed Members through Managed Interfaces
HtmlDocument and HtmlElement provide four methods that enable access to unexposed members. The following table shows the types and their corresponding methods.
When you use these methods, it is assumed that you have an element of the correct underlying type. Suppose that you want to listen to the Submit event of a FORM element on an HTML page, so that you can perform some pre-processing on the FORM's values before the user submits them to the server. Ideally, if you have control over the HTML, you would define the FORM to have a unique ID attribute.
<HTML>
<HEAD>
<TITLE>Form Page</TITLE>
</HEAD>
<BODY>
<FORM ID="form1">
... form fields defined here ...
</FORM>
</BODY>
</HTML>
After you load this page into the WebBrowser control, you can use the GetElementById method to retrieve the FORM at run time using form1 as the argument.
Private Sub SubmitForm(ByVal FormName As String)
Dim Elems As HtmlElementCollection
Dim Elem As HtmlElement
If (WebBrowser1.Document IsNot Nothing) Then
With WebBrowser1.Document
Elems = .All.GetElementsByName(FormName)
If (Not Elems Is Nothing And Elems.Count > 0) Then
Elem = Elems(0)
If (Elem.TagName.Equals("FORM")) Then
Elem.InvokeMember("Submit")
End If
End If
End With
End If
End Sub
private void SubmitForm(String formName)
{
HtmlElementCollection elems = null;
HtmlElement elem = null;
if (webBrowser1.Document != null)
{
HtmlDocument doc = webBrowser1.Document;
elems = doc.All.GetElementsByName(formName);
if (elems != null && elems.Count > 0)
{
elem = elems[0];
if (elem.TagName.Equals("FORM"))
{
elem.InvokeMember("Submit");
}
}
}
}