Sender as T... lazarus / 31-LCL ---------------------------------------------------------------------- airpas · 2013-09-17 19:50 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 Code: [Select]with sender as TRadioButton do case tag of 1: 2: 3: end; thanks ---------------------------------------------------------------------- tfk · 2013-09-17 20:53 Are you looking for something like this? Code: [Select]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 ---------------------------------------------------------------------- howardpc · 2013-09-17 21:15 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: Code: [Select]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; ---------------------------------------------------------------------- airpas · 2013-09-17 21:34 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 ---------------------------------------------------------------------- Mike.Cornflake · 2013-09-17 22:40 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? :-) ---------------------------------------------------------------------- airpas · 2013-09-18 09:50 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 ---------------------------------------------------------------------- howardpc · 2013-09-18 12:25 Quote from: airpas on September 17, 2013, 11:34:42 pmwhat 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. Code: [Select]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.