Cry about...
Delphi Programming with TWebBrowser
Example reading and setting a checkbox on a web page in a TWebBrowser
This page provides examples of how to set or read the value of a check box in a web page which is hosted in TWebBrowser.
Contents
- Introduction and Example HTML
- Read example 1 - using GetFieldValue
- Read example 2 - using GetInputField
- Write example 1 - using SetFieldValue
- Write example 2 - using GetInputField
Introduction and Example HTML
This example also assumes a page is loaded into a TWebBrowser instance which contains the following HTML:
<form name="TestForm">
<input name="TheCheckBox" type="checkbox">
</form>
or you can use this page as an example, and load it into your TWebBrowser instance as it contains this same html:
Note:
- The HTML above uses a named form because it makes it easier to use this page as an example (you can load this page into your TWebBrowser instance and expect the examples to work as is) - since this page uses DHTML it cannot always be guaranteed that this will be the first form on the page, so using a named form avoids that issue.
- If you do not want to (or cannot) use a named form then look at
using the
GetFormByNumber
function provided in the article How to read and write form elements. - The examples on this page use the code provided on the page: How to read and write form elements. You must use the functions on that page in order for these examples to work.
- These examples assume that the TWebBrowser instance is called "webbrowser".
Examples of reading the check box value
Method 1 - Using GetFieldValue
procedure TForm1.ReadExample1Click(Sender: TObject);
var
doc:IHTMLDocument2;
theForm:IHTMLFormElement;
value: string;
begin
doc:=webbrowser.document as IHTMLDocument2;
theForm := GetFormByName(doc,'TestForm');
if not Assigned(theForm) then
exit;
value := GetFieldValue(theForm,'TheCheckBox');
if value = 'checked' then
ShowMessage('checked')
else
ShowMessage('not checked');
end;
Remember: if you load this page into your TWebBrowser instance then the above example can be used without change - and its probably a good idea to be confident with the code before you customise it to your own needs.
Method 2 - Using GetInputField
procedure TForm1.ReadExample2Click(Sender: TObject);
var
doc:IHTMLDocument2;
theForm:IHTMLFormElement;
inputElement: HTMLInputElement;
begin
doc:=webbrowser.document as IHTMLDocument2;
theForm := GetFormByName(doc,'TestForm');
if not Assigned(theForm) then
exit;
inputElement := GetInputField(theForm,'TheCheckBox');
if inputElement.checked then
ShowMessage('checked')
else
ShowMessage('not checked');
end;
This procedure generates a simple message "checked" or "not checked" according to the status of the check box.
Example of writing the check box value
Method 1 - Using SetFieldValue
The following example toggles the value of the check-box, so if it was checked it clears it and if it was cleared it sets it.
procedure TForm1.ToggleExample1Click(Sender: TObject);
var
doc:IHTMLDocument2;
theForm:IHTMLFormElement;
value: string;
begin
doc:=webbrowser.document as IHTMLDocument2;
theForm := GetFormByName(doc,'TestForm');
if not Assigned(theForm) then
exit;
value := GetFieldValue(theForm,'TheCheckBox');
if value = 'checked' then
SetFieldValue(theForm,'TheCheckBox','unchecked')
else
SetFieldValue(theForm,'TheCheckBox','checked');
end;
Method 2 - Using GetInputField
Yes, this example uses "GetInputField" to set the value. This is because "GetInputField" returns the field (and not the value), and once you have the field you can then read or set the value of the field.
procedure TForm1.ToggleExample2Click(Sender: TObject);
var
doc:IHTMLDocument2;
theForm:IHTMLFormElement;
inputElement: HTMLInputElement;
begin
doc:=webbrowser.document as IHTMLDocument2;
theForm := GetFormByName(doc,'TestForm');
if not Assigned(theForm) then
exit;
inputElement := GetInputField(theForm,'TheCheckBox');
inputElement.checked := not inputElement.checked;
end;
These notes are believed to be correct for Delphi 6 and Delphi 7, and may apply to other versions as well.
About the author: Brian Cryer is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.