Jump to content

how to get link on class in another class through parent ?


elGringo
 Share

Recommended Posts

Hi, having 2 classes

 

1 frame is created on another Second frame

on 1 frame there is TUniLabel for example

i'm trying to handle onClick event of label in Second frame and assign it

like

FOL[i].lName.OnClick := OnClickElement; 

here i have "Invalid type Cast"

procedure TLeftMenu2.OnClickElement(Sender: TObject);
var
  i: integer;
begin
  showmessage( //
    ((Sender as TUniLabel).Parent as TLeftMenu2Element).ClassName // << invalid type Cast
  ); //
end;

As Delphi coder I'm doing everything correct, but as UniGUI coder i'm being not so experienced

 

i tried to do things like

procedure TLeftMenu2.OnClickElement(Sender: TObject);
var
  i: integer;
begin
  showmessage( //
    ((Sender as TUniLabel).Parent.Parent).ClassName //
  ); //
end; 

and on click i have TUniSimplePanel - which is not my class.

 

Is any way to get instance of TLeftMenu2Element - 1 frame on click in 2 frame?

 

 

Below full code of 2 frames

unit uLeftMenu2Element;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  uniGUITypes, uniGUIAbstractClasses, uniGUIClasses, uniGUIFrame,
  uniGUIBaseClasses, uniPanel, uniImage, uniLabel;

type
  TLeftMenu2Element = class(TUniFrame)
    iPic: TUniImage;
    lName: TUniLabel;
    procedure UniFrameCreate(Sender: TObject);
    procedure iPicClick(Sender: TObject);
    procedure lNameClick(Sender: TObject);
  private
    FIsExpanded: boolean;
    procedure SetIsExpanded(const Value: boolean);
    { Private declarations }
  public
    { Public declarations }
    property IsExpanded: boolean read FIsExpanded write SetIsExpanded;
  end;

implementation

{$R *.dfm}

uses
  uLeftMenu2;

procedure TLeftMenu2Element.iPicClick(Sender: TObject);
begin
  if FIsExpanded then
  begin
    FIsExpanded := false;
    iPic.Picture.LoadFromFile('pics/arrowRight16x16.png')
  end
  else
  // if not expanded
  begin
    FIsExpanded := true;
    iPic.Picture.LoadFromFile('pics/arrowDown16x16.png');

  end;
end;

procedure TLeftMenu2Element.lNameClick(Sender: TObject);
begin
//
end;

procedure TLeftMenu2Element.SetIsExpanded(const Value: boolean);
begin
  FIsExpanded := Value;
end;

procedure TLeftMenu2Element.UniFrameCreate(Sender: TObject);
begin
  iPic.Picture.LoadFromFile('pics/arrowRight16x16.png');
  lName.Top := trunc(Self.Height / 2 - lName.Height / 2);
  iPic.Top := trunc(Self.Height / 2 - iPic.Height / 2);
end;

end.

And another class

unit uLeftMenu2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  uniGUITypes, uniGUIAbstractClasses, uniGUIClasses, uniGUIFrame,
  uLeftMenu2Element, System.Generics.Collections, uniGUIBaseClasses, uniPanel;

type
  TLeftMenu2 = class(TUniFrame)
    procedure UniFrameCreate(Sender: TObject);
    procedure UniFrameDestroy(Sender: TObject);
  private
    { Private declarations }
    FOL: TObjectList<TLeftMenu2Element>;
    procedure OnClickElement(Sender: TObject);
  public
    { Public declarations }
    procedure LoadGoodsTree;
//    procedure LoadChildren(aID: integer);
  end;

implementation

uses
  FireDAC.Comp.Client, MainModule, uniLabel;

{$R *.dfm}

procedure TLeftMenu2.LoadGoodsTree;

  procedure loadNullLevel();
  var
    q: TFDquery;
    i: integer;
    h: integer;
    itemName: string;
  begin
    h := 0;
    q := TFDquery.Create(nil);
    try
      with q do
      begin
        Connection := UniMainModule.DB.FDConnection;
        sql.Text := 'SELECT * FROM sewworld_db.goods where idParent is NULL order by sortIndex';
        Disconnect();
        Open;
        while not EOF do
        begin
          i := FOL.Add(TLeftMenu2Element.Create(Self));
          itemName := FieldByName('name').AsString;
          if itemName.Length > 15 then
            itemName := itemName.Substring(0, 15) + '...';
          FOL[i].lName.Caption := itemName;
          FOL[i].Parent := Self;
          FOL[i].Left := 0;
          FOL[i].Top := h;
          FOL[i].Show;
          FOL[i].lName.OnClick := OnClickElement;
          h := h + FOL[i].Height;
          Next;
        end;
        Close;
      end;
    finally
      q.Free;
    end;
  end;

begin
  loadNullLevel();
end;

procedure TLeftMenu2.OnClickElement(Sender: TObject);
var
  i: integer;
begin
  showmessage( //
    ((Sender as TUniLabel).Parent as TLeftMenu2Element).ClassName //
  ); //
end;

procedure TLeftMenu2.UniFrameCreate(Sender: TObject);
begin
  FOL := TObjectList<TLeftMenu2Element>.Create;
end;

procedure TLeftMenu2.UniFrameDestroy(Sender: TObject);
begin
  FOL.Free;
end;

end.

Link to comment
Share on other sites

Asked myself - answered myself

 

my workAround

procedure TLeftMenu2.OnClickElement(Sender: TObject);

  function getLeftMenuElement(aID: integer): TLeftMenu2Element;
  var
    i: Integer;
  begin
    Result := nil;
    for i := 0 to FOL.Count - 1 do
      if FOL[i].Tag = aID then
      begin
        result := FOL[i];
        break;
      end;
  end;

var
  leftMenu2Element: TLeftMenu2Element;
begin
  if (Sender is TUniLabel) then
    leftMenu2Element := getLeftMenuElement((Sender as TUniLabel).Tag);
  showmessage(leftMenu2Element.lName.Caption);
end;

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...