Lazarus
Programming => LCL => Topic started by: airpas on September 17, 2013, 09:50:37 pm
-
hi
how to use tag of components ?
supose i have 3 TRadioButton and one Button , in the button click event how to check if one of those radiobutton is checked using tags .
this code dosen't work
with sender as TRadioButton do
case tag of
1:
2:
3:
end;
thanks
-
Are you looking for something like this?
procedure TForm1.Button1Click(Sender: TObject);
var
I, ATag: Integer;
Dummy: string;
begin
for I := 0 to ComponentCount - 1 do
begin
if (Components[I] is TRadioButton) then
begin
ATag := (Component[I] as TRadioButton).Tag
case ATag of
1: Dummy := 'Do something';
2: Dummy := 'Do something else';
end;
end;
end;
end;
TFK
-
It's much simpler to use a radiogroup component rather than check the Tag properties of individual radiobuttons.
Use the provided ItemIndex property, something like this:
procedure TForm1.RadioGroup1Click(Sender: TObject);
var
rg: TRadioGroup;
begin
if not (Sender is TRadioGroup) then Exit;
rg:=TRadioGroup(Sender);
ShowMessageFmt('Radiogroup item %s is selected',[rg.Items[rg.ItemIndex]])
end;
-
thanks
@tfk : it dosen't work too
@howardpc : the tags are 0 , 1 , 2, maybe its absurd example but what i want to know is , how to handle alot of components with the same type and get their status in one event like Button1Click with tag property
-
How have you hooked up the event? the way you've described things, it's only the button that you've hooked up to the OnClick, in which sender will always be TButton, and never TRadioButton.
For each RadioButton, ensure it's onclick is pointing to your buttonclick handler.
Other than that, your posted snippet looks good and should work, Can you post a more complete example?
Alternatively: In the ButtonClick simply reference each component by name, that way you won't need .Tag at all.
It's kind of hard giving a generic answer without knowing exactly what you're trying to achieve.. So, what are you trying to acheive? :-)
-
i found the solution
i must select all RadioButtons first , then go to the Object Inspector and choose OnChange event , then my first code work good .
thanks
-
what i want to know is , how to handle alot of components with the same type and get their status in one event like Button1Click with tag property
The following is a little example which creates radiobuttons dynamically (rather than dropping Palette components on the form), sets the Tag property and uses it in a dynamically assigned event handler.
unit main;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Buttons, StdCtrls;
type
TBtnRange = bkOK..bkClose;
{ TForm1 }
TForm1 = class(TForm)
private
FBitBtn: TBitBtn;
RBarray: array[TBtnRange] of TRadioButton;
public
constructor Create(aComponent: TComponent); override;
destructor Destroy; override;
procedure RBClick(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
constructor TForm1.Create(aComponent: TComponent);
function NewRB(aLeft, aTop: integer; aCaption: string): TRadioButton;
begin
Result:=TRadioButton.Create(nil);
Result.Parent:=Self;
Result.Left:=aLeft;
Result.Top:=aTop;
Result.Caption:=aCaption;
end;
var r: TBtnRange;
t: integer = 50;
l: integer = 10;
begin
inherited CreateNew(aComponent, 1);
Caption:='Dynamic RadioButton creation';
FBitBtn:=TBitBtn.Create(Self);
FBitBtn.Top:=10;
FBitBtn.Left:=10;
FBitBtn.Parent:=Self;
for r := Low(TBtnRange) to High(TBtnRange) do begin
RBarray[TBtnRange(r)]:= NewRB(l, t, GetButtonCaption(integer(r)));
Inc(t, 30);
RBarray[TBtnRange(r)].Tag:=PtrInt(r);
RBarray[TBtnRange(r)].OnChange:=@RBClick;
end;
end;
destructor TForm1.Destroy;
var r: integer;
begin
for r := integer(High(RBarray)) downto integer(Low(RBarray)) do
FreeAndNil(RBarray[TBtnRange(r)]);
inherited Destroy;
end;
procedure TForm1.RBClick(Sender: TObject);
begin
if not (Sender is TRadioButton) then Exit;
FBitBtn.Kind:=TBitBtnKind(TRadioButton(Sender).Tag);
end;
end.