Combobox OnChange事件不起作用 [英] Combobox OnChange event not working

查看:41
本文介绍了Combobox OnChange事件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了组合框onchange 事件:基本上,如果我从combobox1中手动选择一个项目,它会按计划加载/写入/保存文件,但是如果我想在按 UpdateBtn 时使用循环进行同样的操作,则它会部分起作用.(循环有效,并在最后一个元素上停止,但不创建所需的文件.)

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.)

如果我按下 UpdateBtn 然后 Timer2 插入,触发(或模拟手动选择)组合框的onChange事件,然后创建文件作为手动工作.

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.

我可以错过什么吗?

致谢

combobox1的代码(OnChange事件,样式:csDropDown):

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(默认为False,时间间隔50):

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

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

UpdateButton(UpdateBtn)(执行循环):

UpdateButton(UpdateBtn) (to do the loop):

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

推荐答案

这与预期相同,并且与已记录.以编程方式更改 ItemIndex 不会导致触发 OnChange 事件.它仅在响应用户交互时触发.

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.

注意:OnChange仅在响应用户操作时发生.以编程方式更改Text属性不会触发OnChange事件.

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

OnChange 处理程序的内容提取到一个单独的方法中,并从您的 OnChange 处理程序以及任何修改 ItemIndex 的代码中调用该方法,文本

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.

从更广泛的角度看待代码,似乎您已将逻辑与UI捆绑在一起,而不是超出期望的范围.如果要遍历名称列表以将转换应用于文件,则该过程不应包含任何UI元素.您最好将逻辑与UI分开,以使您的代码更具可组合性.

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.

这篇关于Combobox OnChange事件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆