Cry about...
Delphi Programming with TWebBrowser


Example reading and setting a radio button 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

This example also assumes a page is loaded into a TWebBrowser instance which contains the following HTML:

<form name="TestForm">
<input name="Radio1" type="radio" value="r1" checked>
<input name="Radio1" type="radio" value="r2">
<input name="Radio1" type="radio" value="r3">
</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.
  • In this example it is assumed that the name of the radio button group is known (in this case "Radio1"), however no assumption is made as to the number of radio buttons in the group.
  • 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.GetRadioButtonExample1Click(Sender: TObject);
var
  doc:IHTMLDocument2;
  theForm:IHTMLFormElement;
  value: string;
  found: boolean;
  index: integer;
begin
  doc:=webbrowser.document as IHTMLDocument2;
  theForm := GetFormByName(doc,'TestForm');
  if not Assigned(theForm) then
    exit;
  // Consider each radio button in turn.
  found := false;
  index := 0;
  while not found do
  begin
    value := GetFieldValue(theForm,'Radio1',index);
    if value = '' then
      break
    else if value = 'checked' then
      found := true
    else
      Inc(index);
  end;
  if not found then
    ShowMessage('No radio button checked')
  else
    ShowMessage('Radio button ' + IntToStr(index) + ' is 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.

This procedure generates a simple message indicating which radio button (if any) was selected. The only time no radio button will have been selected is if the HTML does not specify a default selected radio button.

Method 2 - Using GetInputField

procedure TForm1.GetRadioButtonExample2Click(Sender: TObject);
var
  doc:IHTMLDocument2;
  theForm:IHTMLFormElement;
  inputElement: HTMLInputElement;
  found: boolean;
  index: integer;
begin
  doc:=webbrowser.document as IHTMLDocument2;
  theForm := GetFormByName(doc,'TestForm');
  if not Assigned(theForm) then
    exit;
  // Consider each radio button in turn.
  found := false;
  index := 0;
  while not found do
  begin
    inputElement := GetInputField(theForm,'Radio1',index);
    if not Assigned(inputElement) then
      break
    else if inputElement.checked then
      found := true
    else
      Inc(index);
  end;
  if not found then
    ShowMessage('No radio button checked')
  else
    ShowMessage('Radio button ' + IntToStr(index) + ' with value '
      + inputElement.value + ' is checked');
end;

Example of writing the check box value

Method 1 - Using SetFieldValue

The following example selects the next radio button in the group:

procedure TForm1.CycleExample1Click(Sender: TObject);
var
  doc:IHTMLDocument2;
  theForm:IHTMLFormElement;
  value: string;
  found: boolean;
  index: integer;
begin
  doc:=webbrowser.document as IHTMLDocument2;
  theForm := GetFormByName(doc,'TestForm');
  if not Assigned(theForm) then
    exit;
  found := false;
  index := 0;
  while not found do
  begin
    value := GetFieldValue(theForm,'Radio1',index);
    if value = '' then
      break
    else if value = 'checked' then
      found := true
    else
      Inc(index);
  end;
  if not found then
    index := 0
  else
  begin
    Inc(index);
    value := GetFieldValue(theForm,'Radio1',index);
    if value = '' then
      index := 0;
  end;
  SetFieldValue(theForm,'Radio1','checked',index);
end;

Remember that a radio button does not behave the same way as a check-box, as only one radio button in a group can be selected at a time. The browser takes care of this, so when you set one radio button checked the browser automatically ensures that the other radio buttons in the group are unchecked so you don't have to worry about it.

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. The following example selects the next radio button in the group:

procedure TForm1.CycleExample2Click(Sender: TObject);
var
  doc:IHTMLDocument2;
  theForm:IHTMLFormElement;
  inputElement: HTMLInputElement;
  found: boolean;
  index: integer;
begin
  doc:=webbrowser.document as IHTMLDocument2;
  theForm := GetFormByName(doc,'TestForm');
  if not Assigned(theForm) then
    exit;
  found := false;
  index := 0;
  while not found do
  begin
    inputElement := GetInputField(theForm,'Radio1',index);
    if not Assigned(inputElement) then
      break
    else if inputElement.checked then
      found := true
    else
      Inc(index);
  end;
  if not found then
    index := 0
  else
    Inc(index);
  inputElement := GetInputField(theForm,'Radio1',index);
  if not Assigned(inputElement) then
  begin
    index := 0;
    inputElement := GetInputField(theForm,'Radio1',index);
  end;
  inputElement.checked := true;
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: 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.