Recent

Author Topic: Sender as T...  (Read 4363 times)

airpas

  • Full Member
  • ***
  • Posts: 179
Sender as T...
« 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
Code: [Select]
with sender as TRadioButton do
case tag of
1:
2:
3:
end;

thanks

tfk

  • New member
  • *
  • Posts: 8
Re: Sender as T...
« Reply #1 on: September 17, 2013, 10:53:06 pm »
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

  • Hero Member
  • *****
  • Posts: 3183
Re: Sender as T...
« Reply #2 on: September 17, 2013, 11:15:46 pm »
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;
« Last Edit: September 17, 2013, 11:26:50 pm by howardpc »

airpas

  • Full Member
  • ***
  • Posts: 179
Re: Sender as T...
« Reply #3 on: September 17, 2013, 11:34:42 pm »
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



« Last Edit: September 17, 2013, 11:37:26 pm by airpas »

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1249
Re: Sender as T...
« Reply #4 on: September 18, 2013, 12:40:38 am »
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? :-)
« Last Edit: September 18, 2013, 12:43:27 am by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

airpas

  • Full Member
  • ***
  • Posts: 179
Re: Sender as T...
« Reply #5 on: September 18, 2013, 11:50:17 am »
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

  • Hero Member
  • *****
  • Posts: 3183
Re: Sender as T...
« Reply #6 on: September 18, 2013, 02:25:35 pm »
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.

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.