Cry about...
Delphi Programming with TWebBrowser
How to set a radio button checked
The notes on this page cover how to get or set the checked status of a radio button hosted on a form inside a TWebBrowser.
This involves three simple steps:
1. Identify the form
There are two ways to identify the form - by name or by number. Code for these is short and can be found by following either of these links:
The examples used below assume the first form on the page and make use of the GetFormByNumber function.
2. Get the Radio Button
Every control on a web page will have a name. These names need not be distinct. When it comes to radio buttons every radio button with the same name is logically grouped together. So if the user clicks on one radio button in the group then all the other radio buttons in the same group (i.e. with the same name) become unchecked.
If you are working with a specific form then you should be able to look at the html to identify the name of the radio buttons. For example, the HTML for:
is:
<form method="get" target="_self"> <input name="Radio1" type="radio" checked> <input name="Radio1" type="radio"> <input name="Radio1" type="radio"> </form>
So the name of the group is "Radio1", and each radio button shares the anem "Radio1".
You can lookup the names of all the controls on a page in code. Look at List the names of all the fields on a form for details.
Once you know the name of the radio button you can access the radio button through code using:
function GetInputField(fromForm: IHTMLFormElement; const inputName: string; const instance: integer=0): HTMLInputElement;
var
field: IHTMLElement;
begin
field := fromForm.Item(inputName,instance) as IHTMLElement;
if Assigned(field) then
begin
if field.tagName = 'INPUT' then
begin
result := field as HTMLInputElement;
exit;
end;
end;
result := nil;
end;
this function takes the form, the name of the radio button and the instance. The first radio button with the given name has an instance of 0, the second 1 and so on.
For example:
TBD
3. Get or Set the Checked Status
Once you have the radio button, getting or setting its checked status is simply a case of using the .checked property.
For example, the following code toggles between checking the first and second radio boxes on a form:
procedure TForm1.ToolButtonTestClick(Sender: TObject); var
form: IHTMLFormElement;
radioButton: HTMLInputElement;
begin
form := GetFormByNumber(WebBrowser.Document as IHTMLDocument2,0);
if Assigned(form) then
begin
radioButton := GetInputField(form,'Radio1',0);
if radioButton.checked then
begin
radioButton := GetInputField(form,'Radio1',1);
radioButton.checked := true;
end
else
radioButton.checked := true; end; 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.