(1)、form上有三个button,在button1的OnClick事件中写下代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
application.MessageBox('','',0);
end;
在button2和button3的onclick中都选择button1Click,表示
和button1的操作是相同的效果
现在我想知道每次我点击的是哪个button,把值用MessageBox
的参数显示出来:

application.MessageBox('我点击了:button1','',0);
怎么做?
(2)、怎样获得焦点在窗口的哪个控件上?

解决方案 »

  1.   

    1)可以利用BUTTON的TAG属性,不同的BUTTON设置为不同的值,调用时检查其TAG值就可以区分了;
    2)可以考虑循环检查出哪个控件获得焦点;
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    application.MessageBox(TButton(Sender).Name,'',0);//显示被点击按钮名字。
    end;-----------------------------------------------------------------
    我是中国鹰派!
    拒绝日货!打倒小日本!
      

  3.   

    application.MessageBox(pchar(Tbutton(sender).caption),'',0);
      

  4.   

    把button1,button2,button3,的tag属性分别设为1,2,3 
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    case TButton(Sender).tag do
      1:application.MessageBox('我点击了:button1'','',0);
      2:{..}
      3:{..}
    end;
    end;每个控件都有一个focuesed的属性,你找找看了:
    for i:=0 to form1.componentcount-1 do
      if form1.components[i].focuesed then
        showmessage(form1.components[i].caption);
      

  5.   

    聲明一全局變量
    var
    s:string;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    application.MessageBox('我點擊了'+s,'',0);
    end;
    procedure TForm1.Button2Click(Sender: TObject);
    begin
    s:='button2';
    form1.button1.click;
    end;
    procedure TForm1.Button3Click(Sender: TObject);
    begin
    s:='button3';
    form1.button1.click;
    end;
    v
      

  6.   

    sender参数就是你点击的那个控件
      

  7.   

    我也同意Drate(鸟窝里的虫)的做法:
        1.结构和思维逻辑过程都很明了!~!
       2. 功能也实现了,是很好的例子!
    呵呵!
      

  8.   

    这样的问题还用这样子大肆的说吗?
    Delphi中很多的过程的参数中都有Sender这个参数,它是TObject类的参数,它就是传递是哪个对象激发该过程的。所以xzhifei(飞) 的回答就是最好的了,还用这样那样的吗?
      

  9.   

    事件处理程序(通称含有)的sender参数是tobject类型,即该对象属于任何类,用as做强制类型转换,delphi自己会做异常处理,1题答案为
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      //不明白你为什么用messagebox,string、pchar我不知如何转换,
      //恐怕只好用case了,如果你是想控制对话框按钮的话可以用messagedlg
      messagedlg((sender as tbutton).Name,mtcustom,[mbok],0);
    end;
    2题,窗口控件才有输入焦点
      for i:=0 to form1.ComponentCount-1 do
      begin
        if (form1.Components[i] as TWinControl).Focused then
          showmessage((form1.Components[i] as TWinControl).Name);
      end;
      

  10.   

    事件的参数中的sender就是触发事件的对象
    if sender is TButton then
     showmessage(TButton(sender).caption);