How to get checkbox or radiobutton value in Inno Setup TNewCheckListBox?

2.4k views Asked by At

I thought it was something simple to code, but the fact is that I'm finally facing issue about getting back checked/unchecked value of checkboxes and radiobuttons in a TNewCheckListBox.

I tried to set the checkboxes and radiobutton as global instance so that I can easily access it in the NextButtonClick event.

But in this sample test I get

Could not call proc.

[Code]
var
  PageChoixComposants: TWizardPage;
  CB1, CB2: TNewCheckBox;
  OB1, OB2: TNewRadioButton; 

procedure InitializeWizard;
var
  CheckListBox: TNewCheckListBox;
begin
  PageChoixComposants := CreateCustomPage(
    wpWelcome, 'Custom wizard page controls', 'TNewCheckListBox');

  CheckListBox := TNewCheckListBox.Create(PageChoixComposants);
  CheckListBox.Width := PageChoixComposants.SurfaceWidth;
  CheckListBox.Height := ScaleY(97);
  CheckListBox.Flat := True;
  CheckListBox.Parent := PageChoixComposants.Surface;
  CB1 := CheckListBox.AddCheckBox('CB1', '', 0, True, True, False, True, nil);
  OB1 := CheckListBox.AddRadioButton('CB1 OB1', '', 1, True, True, nil);
  OB2 := CheckListBox.AddRadioButton('CB1 OB2', '', 1, False, True, nil);
  CB2 := CheckListBox.AddCheckBox('CB2', '', 0, True, True, False, True, nil);
end;

//////////////////////////////////////////////////////////////////////
function NextButtonClick(CurPageID: Integer): Boolean;
begin
  if (CurPageID=PageChoixComposants.ID) then
    begin      
      if CB1.Checked = True then
        begin
          MsgBox('CB1 checked', mbInformation, MB_OK);
        end;
    end;
  Result := True;
end;

//////////////////////////////////////////////////////////////////////

What goes wrong here?

1

There are 1 answers

0
Martin Prikryl On BEST ANSWER

The AddCheckBox and AddRadioButton methods return Integer, not TNewCheckBox/TNewRadioButton.

It's even surprising that your code compiles. It looks like some deficiency of a compiler.

The returned number represents an index of the checkbox/radio button. Use the index along with TNewCheckListBox.Checked (or .State) property to query the checkbox/radio button state.

A correct code is:

var
  PageChoixComposants: TWizardPage;
  CheckListBox: TNewCheckListBox;
  CB1, CB2: Integer;
  OB1, OB2: Integer; 

procedure InitializeWizard;
begin
  PageChoixComposants :=
    CreateCustomPage(wpWelcome, 'Custom wizard page controls', 'TNewCheckListBox');

  CheckListBox := TNewCheckListBox.Create(PageChoixComposants);
  CheckListBox.Width := PageChoixComposants.SurfaceWidth;
  CheckListBox.Height := ScaleY(97);
  CheckListBox.Flat := True;
  CheckListBox.Parent := PageChoixComposants.Surface;
  CB1 := CheckListBox.AddCheckBox('CB1', '', 0, True, True, False, True, nil);
  OB1 := CheckListBox.AddRadioButton('CB1 OB1', '', 1, True, True, nil);
  OB2 := CheckListBox.AddRadioButton('CB1 OB2', '', 1, False, True, nil);
  CB2 := CheckListBox.AddCheckBox('CB2', '', 0, True, True, False, True, nil);
end;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  if CurPageID = PageChoixComposants.ID then
  begin      
    if CheckListBox.Checked[CB1] then
    begin
      MsgBox('CB1 checked', mbInformation, MB_OK);
    end;
  end;
  Result := True;
end;