比如下面代码中出现了很多Sender的地方,不知道怎么用,望高手指教!

procedure TForm_QueTail.DBGridEh1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumnEh;
State: TGridDrawState);
begin
if not (Sender as TDBGridEh).DataSource.DataSet.Active then Exit;
//隔行改变网格背景色:
if not Odd((Sender as TDBGridEh).DataSource.DataSet.RecNo) then begin
(Sender as TDBGridEh).Canvas.Font.Color := clBlack;
(Sender as TDBGridEh).Canvas.Brush.Color := RGB(249, 244, 245); //背景颜色1
end else begin
(Sender as TDBGridEh).Canvas.Font.Color := clBlack;
(Sender as TDBGridEh).Canvas.Brush.Color := clWhite; //背景颜色2
end;
if (Sender as TDBGridEh).SelectedRows.CurrentRowSelected then begin
(Sender as TDBGridEh).Canvas.Font.Color := clBlack;
(Sender as TDBGridEh).Canvas.Brush.Color := RGB(209, 234, 249); //选中背景色
end;
if ((State = [gdSelected]) or (State=[gdSelected,gdFocused])) then begin
(Sender as TDBGridEh).Canvas.Font.Color := clWhite;
(Sender as TDBGridEh).Canvas.Brush.Color := $00E25A76;
end;

if ((Sender as TDBGridEh).DataSource.DataSet.FieldByName('reversal').Asinteger = 1) or
((Sender as TDBGridEh).DataSource.DataSet.FieldByName('rejcode').AsString <> '00') then
begin
(Sender as TDBGridEh).Canvas.Font.Color := clRed;
end
else
begin
if (Sender as TDBGridEh).DataSource.DataSet.FieldByName('voided').Asinteger = 1 then
begin
(Sender as TDBGridEh).Canvas.Font.Color := clBlue;
end
else
begin
(Sender as TDBGridEh).Canvas.Font.Color := clBlack;
end;
end;

// //状态不正常的商户红色显示
// if (Copy(Trim((Sender as TDBGridEh).DataSource.DataSet.FieldByName('SUBMERSTATE').AsString),1,1)<>'1') and
// (Copy(Trim((Sender as TDBGridEh).DataSource.DataSet.FieldByName('SUBMERSTATE').AsString),1,1)<>'2') and
// (Copy(Trim((Sender as TDBGridEh).DataSource.DataSet.FieldByName('SUBMERSTATE').AsString),1,1)<>'6') 
// then
// (Sender as TDBGridEh).Canvas.Font.Color := clRed;

(Sender as TDBGridEh).DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;

临摹微笑
浏览 110回答 2
2回答

ibeautiful

指的是发送者&nbsp;Sender是一个TObject类型的参数,它告诉Delphi哪个控件接收这个事件并调用相应的处理过程。你可以编写一个单一的事件处理句柄,通过Sender参数和IF…THEN…语句或者CASE语句配合,来处理多个构件。发生事件的构件或控件的值已经赋给了Sender参数,该参数的用途之一就在于:可以使用保留字IS来测试Sender,以便找到调用这个事件处理句柄的构件或控件的类型。例如,将表单中编辑框和标签的Click事件的处理句柄都指向表单的xxx过程,编辑框和标签对Click事件有不同的反应:&nbsp;  procedure TForm1 xxx(Sender:TObject);&nbsp;  begin&nbsp;  if(sender is Tedit) then&nbsp;  showmessage(′this is a editbox′);&nbsp;  if(sender is Tlabel) then&nbsp;  showmessage(′this is a label′);&nbsp;  end;&nbsp;  Sender参数的第二个用途是结合AS操作符进行类型转换,将若干个派生于某一父类的子类强制转换成该父类。例如表单中有一个TEdit类控件和一个TMemo控件,它们实际上都派生于TcustomEdit类,如果你要为二者的某一事件提供同样处理,可以将二者事件句柄都指向自定义的过程yyy:&nbsp;  Procedure TForm1.yyy(Sender:TObject);&nbsp;  begin&nbsp;  (sender as TcustomEdit).text:=′This is some demo text′;&nbsp;  end;&nbsp;  在过程中,AS操作符将TEdit类和TMemo类均强制转换成TcustomEdit类,再对TcustomEdit类的属性赋值。注意这种转换必须符合Delphi中类的层次关系。&nbsp;  使用Sender参数可以通过单一过程段处理多类控件,真正体现了Delphi面向对象的重用性。

MMMHUHU

1.Sender的定义:&nbsp;每一个事件处理里面至少都有一个Sender参数。比如:&nbsp;procedure TForm1.Button1Click(Sender:TObject);begin...end;Sender的含义就是代表调用TForm1.Button1Click这个过程的控件. 由于Sender是TObject,所以任何object都可以赋给Sender.当你点击BUTTON1时,会产生一个Button1Click事件,系统会把Button1传递给Button1Click过程作为参数,也就是所说的Sender.2.Sender的用法:&nbsp;<1>.由于Sender代表了调用所在过程的控件,那么你就可以直接把它拿来当那个控件用,不过如果要用属性的话,最好写成(Sender as 控件名).控件属性:=... 例如:procedure TForm1.Edit1Click(Sender: TObject);beginwith Sender as TEdit dobegintext:=’hello’;end;end;<2>.如果在两个事件中处理同样的事情,那么可以利用Sender来省去重写同样的过程。例如:&nbsp;Procedure TForm1.Button1Click(Sender:TObject);begindo same sth.....;if Sender=Button1 thendo sth....;if Sender=Button2 thendo other sth....;end;procedure TForm1.Button2Click(Sender:TOBJect);beginButton1Click(Button2);end;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java