Search code examples
comboboxinno-setuppascalscript

Does the TNewComboBox support Item Data in Inno Setup [Code] section?


I did ask this question on the Inno Setup newsgroup but did not get any responses.

Can someone please clarify if it is possible to use Item Data in the [Code] section with TNewComboBox controls, like you can do in other languages?

I tried to find out and could not see anything on the subject.


For the benefit of completeness ... in my current script I have this pre-processor script at the top of my file:

; Language values
#define MDB_ENG "0"
#define MDB_ESP "1"
#define MDB_DEU "2"
#define MDB_ITA "3"
#define MDB_NLD "4"
#define MDB_TRK "5"
#define MDB_PTB "6"
#define MDB_SVE "7"
#define MDB_DAN "8"
#define MDB_RUS "9"
#define MDB_FIN "10"
#define MDB_AFK "11"
#define MDB_CHS "12"
#define MDB_SQI "13"
#define MDB_FRA "14"
#define MDB_BGR "15"
#define MDB_TWI "16"
#define MDB_FPO "17"
#define MDB_SWK "18"
#define MDB_ELL "19"
#define MDB_UKR "20"
#define MDB_KHM "21"
#define MDB_ROM "22"
#define MDB_SMO "23"
#define MDB_IND "24"
#define MDB_VIT "25"
#define MDB_ARA "26"
#define MDB_PLK "27"
#define MDB_SRN "28"
#define MDB_JPN "29"
#define MDB_LIN "30"

#define DatabaseFiles() \
    DatabaseFile('English', 'English') + \
    DatabaseFile('Spanish', 'Español') + \
    DatabaseFile('German', 'Deutsch') + \
    DatabaseFile('Italian', 'Italiano') + \
    DatabaseFile('Dutch', 'Nederlands') + \
    DatabaseFile('Turkish', 'Türkçe') + \
    DatabaseFile('Portuguese', 'Português') + \
    DatabaseFile('Swedish', 'Svenska') + \
    DatabaseFile('Danish', 'Dansk') + \
    DatabaseFile('Russian', 'Русский') + \
    DatabaseFile('Finnish', 'Suomi') + \
    DatabaseFile('Afrikaans', 'Afrikaans') + \
    DatabaseFile('Chinese Simplified', '汉语(简化字)') + \
    DatabaseFile('Albanian', 'Shqip') + \
    DatabaseFile('French', 'Française') + \
    DatabaseFile('Bulgarian', 'Български') + \
    DatabaseFile('Twi', 'Akan') + \
    DatabaseFile('Tagalog', 'Tagalog') + \
    DatabaseFile('Swahili', 'Kiswahili') + \
    DatabaseFile('Greek', 'Ελληνική') + \
    DatabaseFile('Ukrainian', 'Українська') + \
    DatabaseFile('Cambodian', 'Cambodian') + \
    DatabaseFile('Romanian', 'Română') + \
    DatabaseFile('Samoan', 'Faa-Sāmoa') + \
    DatabaseFile('Indonesian', 'Indonesia') + \
    DatabaseFile('Vietnamese', 'Vietnamese') + \
    DatabaseFile('Arabic', 'العربية') + \
    DatabaseFile('Polish', 'Polski') + \
    DatabaseFile('Sranantongo', 'Sranantongo') + \
    DatabaseFile('Japanese', '日本語') + \
    DatabaseFile('Lingala', 'Lingala')

Then, down in the [Code] section where I create my custom page, I create the combo like this:

// cbDatabase
cbDatabase := TNewComboBox.Create(Page);
with cbDatabase do
begin
#define DatabaseFile(Name, Text) \
    'Items.Add(''' + Text + ''');' + NewLine + \
    'if ActiveLanguage = ''' + Name + ''' then ItemIndex := Items.Count - 1;' + NewLine

    Parent := Page.Surface;
    Left := ScaleX(8);
    Top := ScaleY(128);
    Width := ScaleX(177);
    Height := ScaleY(21);
    Style := csDropDownList;
    TabOrder := 3;

    // Languages
    {#DatabaseFiles}
end;

So, I have to upgrade this code to use the Objects approach described in the answer here. And I need to make it sortable. I am sure other code will be affected but best to not bloat this question.


Solution

  • You can use Objects property:

    var
      Page: TWizardPage;
      ComboBox: TNewComboBox;
      DataLabel: TLabel;
    
    procedure AddItem(S: string; Data: Integer);
    begin
      ComboBox.Items.Objects[ComboBox.Items.Add(S)] := Integer(Data);
    end;
    
    procedure ComboBoxChange(Sender: TObject);
    var
      I: Integer;
    begin
      if ComboBox.ItemIndex >= 0 then
      begin
        I := ComboBox.Items.Objects[ComboBox.ItemIndex];
        DataLabel.Caption := IntToStr(I);
      end
        else DataLabel.Caption := 'none';
    end;
    
    procedure InitializeWizard();
    begin
      Page := CreateCustomPage(wpSelectDir, 'Combo box test', '');
    
      ComboBox := TNewComboBox.Create(WizardForm);
      ComboBox.Style := csDropDownList;
      ComboBox.Parent := Page.Surface;
      ComboBox.OnChange := @ComboBoxChange;
    
      AddItem('one', 1);
      AddItem('two', 2);
      AddItem('twelve', 12);
      AddItem('hundred', 100);
    
      DataLabel := TLabel.Create(WizardForm);
      DataLabel.Parent := Page.Surface;
      DataLabel.Top := ComboBox.Top + ComboBox.Height + ScaleY(8); 
    end;
    

    enter image description here


    To glue it together with your code, I assume you want something like this:

    #define DatabaseFile(Name, Text, Data) \
        'AddItem(''' + Text + ''', Data);' + NewLine + \
        'if ActiveLanguage = ''' + Name + ''' then ItemIndex := Items.Count - 1;' + NewLine
    
    #define MDB_AFK 11
    #define MDB_TWI 16
    ...
    
    #define DatabaseFiles() \
        DatabaseFile('Afrikaans', 'Afrikaans', MDB_AFK) + \
        DatabaseFile('Twi', 'Akan', MDB_TWI) + \
        ...