首页 > 其他分享 >delphi 避免两个联动的COMBOBOX进入死循环

delphi 避免两个联动的COMBOBOX进入死循环

时间:2022-10-28 17:11:43浏览次数:70  
标签:end COMBOBOX delphi 默认 str ChangComCount com 死循环 ItemIndex

 

 当物料类型为 刀具仓时,单位 自动修改为 粒

 当物料类型为 原料仓时,单位 自动修改为 公斤

反之亦然.

这是两个相互联动的控件,如果直接写的话,会直接死循环.

如何做到只让控件修改一次就不再联动的目的呢?

我们需要一个计数器来做辅助.

设置一个全局变量 ChangComCount:integer;

当A控件进行修改进,ChangComCount +1,此时 ChangComCount=1

然后联动到B控件,ChangComCount又+1,此时  ChangComCount=2

到这里程序就应该要退出,并且为了下一次修改做准备,ChangComCount要初始化为0,不然ChangComCount永远大于2,就没办法时行联动了;

 

procedure TNewCode.com单位Change(Sender: TObject);
var
  str: string;
begin
  ChangComCount := ChangComCount + 1;
  if ChangComCount > 2 then
  begin
    ChangComCount := 0;
    Exit;
  end;
  str := com单位.Items[com单位.ItemIndex].Caption;
  if str = '公斤' then
    com默认仓库.ItemIndex := com默认仓库.IndexOfCaption('原料仓')end
  else if (str = '粒') or (str = '把') or (str = '支') then
    com默认仓库.ItemIndex := com默认仓库.IndexOfCaption('刀具仓');
end;


procedure TNewCode.com默认仓库Change(Sender: TObject);
var
  str: string;
begin
  ChangComCount := ChangComCount + 1;
  if ChangComCount > 2 then
  begin
    ChangComCount := 0;
    Exit;
  end;
  str := com默认仓库.Items[com默认仓库.ItemIndex].Caption;
  if str = '原料仓' then
    Com单位.ItemIndex := com单位.IndexOfCaption('公斤')end
  else if str = '刀具仓' then
    Com单位.ItemIndex := com单位.IndexOfCaption('粒');
end;

 

标签:end,COMBOBOX,delphi,默认,str,ChangComCount,com,死循环,ItemIndex
From: https://www.cnblogs.com/yoooos/p/16836703.html

相关文章