SzaBee
SzaBee

Reputation: 43

Combobox OnChange event not working

I had an issue with Combobox onchange event: Basically if i select manually an item from combobox1 it load/write/save back the file as planned, but if i want to do the same with a loop when i hit the UpdateBtn, it partially work. (loop is work, and stop on last element, but not create the desired file.)

I want to do if i hit the UpdateBtn then the Timer2 kick in , fire (or simulate the manual selection) the onChange event of the Combobox, and create the file as manually works.

May i missed something?

Regards

Code for the combobox1 (OnChange event, Style: csDropDown):

    procedure TForm1.ComboBox1Change(Sender: TObject);
      var sl : TStringList;
    begin
     if
   ComboBox1.ItemIndex = ComboBox1.Items.IndexOf('')
      then
SetCurrentDir('C:\Net-AdminUpdater\') ;
sl := TStringList.Create;
sl.LoadFromFile('Ugloader.ini');
sl[3] := 'AppPath=c:\users\'+ ComboBox1.Text +'\AppData\Local\Systemica';
sl[5] := 'LASTUPGRADE=';
sl[10] := 'AppPath=c:\users\'+ ComboBox1.Text +'\AppData\Local\Systemica';
sl[12] := 'LASTUPGRADE=';
sl[17] := 'AppPath=c:\users\'+ ComboBox1.Text +'\AppData\Local\Systemica';
sl[19] := 'LASTUPGRADE=';
sl[24] := 'AppPath=c:\users\'+ ComboBox1.Text +'\AppData\Local\Systemica';
sl[26] := 'LASTUPGRADE=';
sl[31] := 'AppPath=c:\users\'+ ComboBox1.Text +'\AppData\Local\Systemica';
sl[33] := 'LASTUPGRADE=';
sl[38] := 'AppPath=c:\users\'+ ComboBox1.Text +'\AppData\Local\Systemica';
sl[40] := 'LASTUPGRADE=';
sl[45] := 'AppPath=c:\users\'+ ComboBox1.Text +'\AppData\Local\Systemica';

SetCurrentDir('C:\Users\'+ ComboBox1.Text +'\WINDOWS\');
sl.SaveToFile('Ugloader.ini');
sl.LoadFromFile('Ugloader.ini');

sl.Free;

end;

Timer2 (Enabled =false by default,interval 50):

ComboBox1.ItemIndex := (ComboBox1.ItemIndex + 1)

UpdateButton(UpdateBtn) (to do the loop):

procedure TForm1.UpdateBtnClick(Sender: TObject);
begin
   Timer2.Enabled := True;

Upvotes: 0

Views: 3225

Answers (1)

David Heffernan
David Heffernan

Reputation: 613302

This is as expected and as documented. Changing ItemIndex programmatically does not result in the OnChange event being fired. It fires only in response to user interaction.

Note: OnChange only occurs in response to user actions. Changing the Text property programmatically does not trigger an OnChange event.

Extract the contents of the OnChange handler to a separate method and call that method from your OnChange handler and any code that modifies ItemIndex, Text etc.

Looking more broadly at your code, it looks like you have tied up the logic with the UI rather more than is desirable. If you want to iterate through a list of names to apply a transformation to a file, that should not involve any UI elements. You would do well to separate the logic from the UI so that your code becomes more composable.

Upvotes: 1

Related Questions