关于多个按钮代码重用方法

关于多个按钮代码重用方法,窗体上有24个按钮,每个按钮执行的事件一样,但要根据各自的属性下发不同的标识到串口中,则有两种方法可以

 

使它们共用一个事件过程,一种是直接用Sender是否等于某个控件来判断是哪个按钮进行了操作,第二种是用遍历组件的方法,来取得某一种

 

控件的属性数据,这样省了大量的代码。

 

procedure TForm1.ToolButton1Click(Sender: TObject);
var
  s:char;
  Command:array[0..254]of byte;
  CRC:Word;
  Channel:array[0..2]of byte; //3个字节表示24个通道
  ChannelBit:array[0..23]of LongWord;
  iChannel,bit:LongWord;
  i,j,t:integer;
  cname,scmd:string;
begin
  bit:=1;
  iChannel:=0;
  for i:=0 to 23 do  ChannelBit[i]:=bit shl i;
  Command[0]:=$FF;Command[1]:=$FF;
  Command[2]:=$FF;Command[3]:=$FF;
  Command[4]:=$7E;Command[5]:=$55;
  Command[6]:=$0B;Command[7]:=$00;
  Command[8]:=StrToInt('$'+LeftStr(edtNo.Text,2));Command[9]:=StrToInt('$'+Rightstr(edtNo.Text,2));
  Command[10]:=$AA;Command[11]:=$AA;
  if Sender is TToolButton then
  begin
  t:=(Sender as TToolButton).Tag;
  if t=0 then
  begin
    if Sender=ToolButton1 then SA.Brush.Color:=clRed;
    if Sender=ToolButton2 then SB.Brush.Color:=clRed;
    if Sender=ToolButton3 then SC.Brush.Color:=clRed;
    if Sender=ToolButton4 then SD.Brush.Color:=clRed;
    if Sender=ToolButton5 then SE.Brush.Color:=clRed;
    if Sender=ToolButton6 then SF.Brush.Color:=clRed;
    if Sender=ToolButton7 then SG.Brush.Color:=clRed;
    if Sender=ToolButton8 then SH.Brush.Color:=clRed;
    if Sender=ToolButton9 then SI.Brush.Color:=clRed;
    if Sender=ToolButton10 then SJ.Brush.Color:=clRed;
    if Sender=ToolButton11 then SK.Brush.Color:=clRed;
    if Sender=ToolButton12 then SL.Brush.Color:=clRed;
    if Sender=ToolButton13 then SM.Brush.Color:=clRed;
    if Sender=ToolButton14 then SN.Brush.Color:=clRed;
    if Sender=ToolButton15 then SO.Brush.Color:=clRed;
    if Sender=ToolButton16 then SP.Brush.Color:=clRed;
    if Sender=ToolButton17 then SQ.Brush.Color:=clRed;
    if Sender=ToolButton18 then SR.Brush.Color:=clRed;
    if Sender=ToolButton19 then SS.Brush.Color:=clRed;
    if Sender=ToolButton20 then ST.Brush.Color:=clRed;
    if Sender=ToolButton21 then SU.Brush.Color:=clRed;
    if Sender=ToolButton22 then SV.Brush.Color:=clRed;
    if Sender=ToolButton23 then SW.Brush.Color:=clRed;
    if Sender=ToolButton24 then SX.Brush.Color:=clRed;

    (Sender as TToolButton).Tag:=1;


  end
  else
  begin
    if Sender=ToolButton1 then SA.Brush.Color:=clBlue;
    if Sender=ToolButton2 then SB.Brush.Color:=clBlue;
    if Sender=ToolButton3 then SC.Brush.Color:=clBlue;
    if Sender=ToolButton4 then SD.Brush.Color:=clBlue;
    if Sender=ToolButton5 then SE.Brush.Color:=clBlue;
    if Sender=ToolButton6 then SF.Brush.Color:=clBlue;
    if Sender=ToolButton7 then SG.Brush.Color:=clBlue;
    if Sender=ToolButton8 then SH.Brush.Color:=clBlue;
    if Sender=ToolButton9 then SI.Brush.Color:=clBlue;
    if Sender=ToolButton10 then SJ.Brush.Color:=clBlue;
    if Sender=ToolButton11 then SK.Brush.Color:=clBlue;
    if Sender=ToolButton12 then SL.Brush.Color:=clBlue;
    if Sender=ToolButton13 then SM.Brush.Color:=clBlue;
    if Sender=ToolButton14 then SN.Brush.Color:=clBlue;
    if Sender=ToolButton15 then SO.Brush.Color:=clBlue;
    if Sender=ToolButton16 then SP.Brush.Color:=clBlue;
    if Sender=ToolButton17 then SQ.Brush.Color:=clBlue;
    if Sender=ToolButton18 then SR.Brush.Color:=clBlue;
    if Sender=ToolButton19 then SS.Brush.Color:=clBlue;
    if Sender=ToolButton20 then ST.Brush.Color:=clBlue;
    if Sender=ToolButton21 then SU.Brush.Color:=clBlue;
    if Sender=ToolButton22 then SV.Brush.Color:=clBlue;
    if Sender=ToolButton23 then SW.Brush.Color:=clBlue;
    if Sender=ToolButton24 then SX.Brush.Color:=clBlue;
    (Sender as TToolButton).Tag:=0
  end;
  end;

 

  for i:=0 to ComponentCount-1 do
  begin
    cname:=Components[i].Name;
    if Components[i].ClassNameIs('TToolButton') then
    begin
      for j:=0 to 23 do
      if Components[i].Tag=1 then
      begin
        if Components[i].Name='ToolButton'+IntToStr(j+1)then
          iChannel:=iChannel or ChannelBit[j];
      end
      else
      begin
        if Components[i].Name='ToolButton'+IntToStr(j+1)then
          iChannel:=iChannel and(not ChannelBit[j]);
      end;
    end;

 

    if Sender=btnOff then
      if Components[i].ClassNameIs('TShape') then
        TShape(Components[i]).Brush.Color:=clBlue
      else if Components[i].ClassNameIs('TToolButton')then
        TToolButton(Components[i]).Tag:=0;
    if Sender=btnPower then
      if Components[i].ClassNameIs('TShape') then
        TShape(Components[i]).Brush.Color:=clRed
      else if Components[i].ClassNameIs('TToolButton')then
        TToolButton(Components[i]).Tag:=1;

  end;
    if (Sender =btnOff)then
      iChannel:=0;
    if Sender=btnPower then
      iChannel:=16777215;

  Command[12]:=$99;Command[13]:=(iChannel shr 16)and$FF;
  Command[14]:=(iChannel shr 8)and $FF;Command[15]:=iChannel and $FF;
  CRC:=CRC16ITU(@Command[7],9);
  Command[16]:=(CRC shr 8)and $FF;Command[17]:=CRC and $FF;
  comm.WriteCommData(@Command,18);
  scmd:='';
  for i:=0 to 18 do
    scmd:=scmd+IntToHex(Command[i],2)+' ';
  memLog.Lines.Add(scmd);
end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值