动态生成两个speedbutton 怎样点击一个修改另一个的caption???(50分)

  • 主题发起人 主题发起人 567567
  • 开始时间 开始时间
5

567567

Unregistered / Unconfirmed
GUEST, unregistred user!
button:=TSpeedButton.Create(Self);
button.Name:='abc';
button:=TSpeedButton.Create(Self);
button.Name:='xyz';
 
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;

type
TForm2 = class(TForm)
Button1: TButton;
SpeedButton1: TSpeedButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
Fsbtn1:TSpeedButton;
Fsbtn2:TSpeedButton;
procedure SbtnClick(sender:TObject);
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
Fsbtn1:=TSpeedButton.Create(Self);
Fsbtn1.SetBounds(10,10,200,20);
Fsbtn1.Caption := 'SpeedButton1';
Fsbtn1.OnClick := SbtnClick;
Fsbtn1.Parent := Self;

Fsbtn2:=TSpeedButton.Create(Self);
Fsbtn2.SetBounds(10,30,200,20);
Fsbtn2.Caption := 'SpeedButton2';
Fsbtn2.OnClick := SbtnClick;
Fsbtn2.Parent := Self;
end;

procedure TForm2.SbtnClick(sender: TObject);
begin
if sender = Fsbtn1 then
begin
Fsbtn2.Caption := Fsbtn2.Caption + '=#';
end
else
begin
Fsbtn1.Caption := Fsbtn1.Caption + '=%';

end;
end;

end.
 
procedure TForm1.Button2Click(Sender: TObject);
function MyGetAveCharSize(Canvas: TCanvas): TPoint;
var
I: Integer;
Buffer: array[0..51] of Char;
begin
for I := 0 to 25 do Buffer := Chr(I + Ord('A'));
for I := 0 to 25 do Buffer[I + 26] := Chr(I + Ord('a'));
GetTextExtentPoint(Canvas.Handle, Buffer, 52, TSize(Result));
Result.X := Result.X div 52;
end;
var
button:TSpeedButton;
DialogUnits:Tpoint;
begin
button:=TSpeedButton.Create(Self);
DialogUnits := MyGetAveCharSize(Canvas);
with button do
begin
Name:='abc';
Caption:='abc';
Parent := Form1;
Left := MulDiv(8, DialogUnits.X, 4);
Top := MulDiv(8, DialogUnits.Y, 8);
OnClick:=SpeedButtonClick;
end;

button:=TSpeedButton.Create(Self);
with button do
begin
Name:='xyz';
Caption:='xyz';
Parent := Form1;
Left := MulDiv(19, DialogUnits.X, 4);
Top := MulDiv(164, DialogUnits.Y, 8);
end;
end;

procedure TForm1.SpeedButtonClick(Sender: TObject);
var
i:Integer;
begin
for i:=0 to self.ComponentCount-1 do
begin
if (self.Components.ClassName = 'TSpeedButton') and (self.Components.Name = 'xyz') then
begin
TSpeedButton(self.Components).Caption:='test';
break;
end;
end;
end;
 
procedure Btn1Event(Sender:Tobject);
procedure Btn2Event(Sender:Tobject);
//------------------
button1:=TSpeedButton.Create(Self);
button1.parent:=self;
button1.Name:='abc';
button1.onclick=Btn1Event;

button2:=TSpeedButton.Create(Self);
button2.Name:='xyz';
button2.parent:=self;
button2.onclick=Btn2Event;
//-------------------------
procedure Btn1Event(Sender:Tobject);
begin
button2.caption:='ffff';
end;
procedure Btn2Event(Sender:Tobject);
begin
//...
end;
 
谢谢!!!! 小陵比较适用!!!

因为我的程序生成button的数量是不固定的!!!

只能通过name来查找点击的是哪一个button !!

wr960204 第一个回答!!!

谢谢!!!!!!
 
多人接受答案了。
 
可以在一个Button的代码如下:
procedure ButtonOnClick(Sender: TObject);

button:=TSpeedButton.Create(Self);
button.Name:='abc';
tbutton.OnClick := ButtonOnClick;
 
后退
顶部