(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)、怎样获得焦点在窗口的哪个控件上?
procedure TForm1.Button1Click(Sender: TObject);
begin
application.MessageBox('','',0);
end;
在button2和button3的onclick中都选择button1Click,表示
和button1的操作是相同的效果
现在我想知道每次我点击的是哪个button,把值用MessageBox
的参数显示出来:
如
application.MessageBox('我点击了:button1','',0);
怎么做?
(2)、怎样获得焦点在窗口的哪个控件上?
2)可以考虑循环检查出哪个控件获得焦点;
begin
application.MessageBox(TButton(Sender).Name,'',0);//显示被点击按钮名字。
end;-----------------------------------------------------------------
我是中国鹰派!
拒绝日货!打倒小日本!
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);
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
1.结构和思维逻辑过程都很明了!~!
2. 功能也实现了,是很好的例子!
呵呵!
Delphi中很多的过程的参数中都有Sender这个参数,它是TObject类的参数,它就是传递是哪个对象激发该过程的。所以xzhifei(飞) 的回答就是最好的了,还用这样那样的吗?
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;
if sender is TButton then
showmessage(TButton(sender).caption);