1

I already have a combobox in a TInputQueryWizardPage page but the problem is I don’t know how to retrieve the selected value from the registry after writing from the first run.

My code for combobox is:

  AuthComboBox := TNewComboBox.Create(ReportPage);
  AuthComboBox.Parent := ReportPage.Edits[1].Parent;
  AuthComboBox.Left := ReportPage.Edits[1].Left;
  AuthComboBox.Top := ReportPage.Edits[1].Top;
  AuthComboBox.Width := ReportPage.Edits[1].Width;
  AuthComboBox.Height := ReportPage.Edits[1].Height;
  AuthComboBox.TabOrder := ReportPage.Edits[1].TabOrder;
  AuthComboBox.Items.Add('Password Authentication');          
  AuthComboBox.Items.Add('Windows Authentication');
  AuthComboBox.ItemIndex := 0;
  { Hide the original edit box }
  ReportPage.PromptLabels[1].FocusControl := AuthComboBox;
  ReportPage.Edits[1].Visible := False;
  AuthComboBox.OnChange := @ComboBoxChange;

Values behind AuthComboBox.Items.Add are:

function GetAuthCombo(Param: String): String;
begin
  case AuthComboBox.ItemIndex of
    0: Result := 'False';
    1: Result := 'True';
  end;
end;

I write them to the registry with the following code:

if (CurStep=ssPostInstall) then 
  begin
     RegWriteStringValue(HKEY_LOCAL_MACHINE, 'Software\RiskValue',
    'ReportProdAuthType', ExpandConstant('{code:GetAuthCombo}'));
  end;

If I choose the second choice Windows Authentication from combobox I expect to have the same value (Windows Authentication) as default value now when I run the installer for the second time.

Peter Star
  • 47
  • 5

1 Answers1

1

Replace this:

  AuthComboBox.ItemIndex := 0;

with:

var
  S: string;
begin
  { ... }
  if RegQueryStringValue(HKLM, 'Software\RiskValue', 'ReportProdAuthType', S) and
     SameText(S, 'True') then
  begin
    AuthComboBox.ItemIndex := 1;
  end
    else
  begin
    AuthComboBox.ItemIndex := 0;
  end;
  { ... }
end;

Also the use of ExpandConstant to get the value for the registry key is over engineered.

Either use it from [Registry] section (what scripted constants are intended for):

[Registry]
Root: HKLM; Subkey: "Software\RiskValue"; ValueType: string; \
    ValueName: "ReportProdAuthType"; ValueData: "{code:GetAuthCombo}"

Or, if you want to use Pascal Script, use GetAuthCombo directly:

if (CurStep=ssPostInstall) then 
begin
  RegWriteStringValue(HKEY_LOCAL_MACHINE, 'Software\RiskValue',
    'ReportProdAuthType', GetAuthCombo(''));
end;

Then you can even remove the Param: String, or actually even inline the GetAuthCombo function completely, unless you use it elsewhere.

var
  S: string;
begin
  { ... }
  if (CurStep=ssPostInstall) then 
  begin
    case AuthComboBox.ItemIndex of
      0: S := 'False';
      1: S := 'True';
    end;
    RegWriteStringValue(HKEY_LOCAL_MACHINE, 'Software\RiskValue', 'ReportProdAuthType', S);
  end;
end;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Thanks for your fast answer! Because I am not a pro developer I have a problem with the first part of your answer. When i delete `AuthComboBox.ItemIndex := 0;` then I replace it at the same place with the code that you propose me but I am getting the following error "Unknown Identifier 'SameText'. So, I have to replace with your code this line `AuthComboBox.ItemIndex := 0;` or I have to do something else in another place? – Peter Star Jun 21 '19 at 10:24
  • 1
    `SameText` was introduced in Inno Setup 6. You are probably using an older version of Inno Setup. You should upgrade. If you cannot upgrade, you can replace `SameText(S, 'True')` with `(CompareText(S, 'True') = 0)`. – Martin Prikryl Jun 21 '19 at 10:27
  • I really want to upgrade to the latest version but I cannot find many things that do not work in Inno Setup 6 like `LoadStringFromFile` and much more. Is there any compare guide? – Peter Star Jun 21 '19 at 11:35
  • 1
    Upgrading from Inno Setup 5 to Inno Setup 6 should be without any problem, but - You probably use Ansi version of Inno Setup 5. While Inno Setup 6 comes in Unicode version only. See [Upgrading from Ansi to Unicode version of Inno Setup (any disadvantages)](https://stackoverflow.com/q/43158677/850848). – Martin Prikryl Jun 21 '19 at 13:11