Combobox OnChange event not working

不想你离开。 提交于 2020-01-07 04:21:28

问题


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;

回答1:


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.



来源:https://stackoverflow.com/questions/42933184/combobox-onchange-event-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!