Combobox OnChange event not working

I had a problem with the Combobox onchange event: In principle, if I manually select an item from combobox1, it will load / write / save the file as planned, but if I want to do the same with the loop, when I click UpdateBtn , it partially works. (The loop runs and stops at the last element, but does not create the desired file.)

I want to do if I click UpdateBtn , then Timer2 , turn on the fire (or simulate manual selection) the onChange Combobox event and create a file as it works manually.

Maybe I missed something?

Hello

Code for 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 execute the loop):

 procedure TForm1.UpdateBtnClick(Sender: TObject); begin Timer2.Enabled := True; 
0
source share
1 answer

This is as expected and documented . Changing the ItemIndex software does not OnChange event. It only works in response to user interaction.

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

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

Looking more broadly at your code, it looks like you have connected the logic with the user interface much more than desired. If you want to iterate over a list of names to apply the conversion to a file, this should not include any user interface elements. You should separate the logic from the user interface to make your code more complex.

+1
source

Source: https://habr.com/ru/post/1270406/


All Articles