Lazarus

Programming => LCL => Topic started by: airpas on September 17, 2013, 09:50:37 pm

Title: Sender as T...
Post 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
Code: [Select]
with sender as TRadioButton do
case tag of
1:
2:
3:
end;

thanks
Title: Re: Sender as T...
Post by: tfk 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
Title: Re: Sender as T...
Post by: howardpc 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;
Title: Re: Sender as T...
Post by: airpas 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



Title: Re: Sender as T...
Post by: Mike.Cornflake 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? :-)
Title: Re: Sender as T...
Post by: airpas 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
Title: Re: Sender as T...
Post by: howardpc 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.