1
\$\begingroup\$

Test version of my code for handling individual mouseenter / mouseleave events for objects created dynamically. The code emulates a seating cart for a bus, as you mouse over the objects it will display the students name and remove it when your mouse leaves the object. Current test only accounts for four students. If you decide to compile and run the code set the spinedit controls to one row and two seats per row. I hardcoded four "student records" so the actions only work for four of the seats. Thanks again for any help you can provide.

unit SeatingChart;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Spin;

Type
 TPanelSeat = Array of TPanel;
 TPanelRow = Array of TPanelSeat;

Type
 TStudent = Class(Tobject)
   Name: String;
 End;

Type
 TTemplate = Class(Tobject)
  const
   BufferVal   = 20;
   Pan_Height  = 40;
   Pan_Width   = 125;
  Var
   Prev_Top     : Integer;
   Pass_Left    : Integer; //Previous Left Value of Passenger side seat
   Driv_Left    : Integer; //Previous Left Value of Driver side seat
   PanelRow     : TPanelRow;
  Private
   StudentInfo: Array[0..3] of TStudent;
   Procedure Initialize;
 End;


type
  TForm1 = class(TForm)
    lblRow: TLabel;
    lblSPR: TLabel;
    seSPR: TSpinEdit;
    seRow: TSpinEdit;
    btnClearLayout: TButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label2: TLabel;
    procedure btnClearLayoutClick(Sender: TObject);
    procedure seRowChange(Sender: TObject);
    procedure seSPRChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    BusTemplate : TTemplate;
    procedure CreateSeats;
    procedure DestroySeats;
    procedure UpdateSeats;
    procedure ResetSpinValues;
    Procedure CreatePanelDisplay(PR: TPanelRow);
    Function LabelSeats(R,S: Integer): String;
    Function BuildSeatArray(RowNum,SPR: Integer): TPanelRow; //Returns Row
  public
    { Public declarations }
    Procedure FindStudent(Sender: Tobject);
    Class Procedure OnMouseEnter(Sender: Tobject);
    Class Procedure OnMouseLeave(Sender: Tobject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnClearLayoutClick(Sender: TObject);
begin
DestroySeats;
ResetSpinValues;
end;

function TForm1.BuildSeatArray(RowNum, SPR: Integer): TPanelRow;
var
  I,J: Integer;
begin
SetLength(BusTemplate.PanelRow, RowNum);
SetLength(Result, Length(BusTemplate.PanelRow));
 for I := 0 to High(BusTemplate.PanelRow) do
 begin
 SetLength(BusTemplate.PanelRow[I], SPR * 2);
  for J := 0 to High(BusTemplate.PanelRow[I]) do
  begin
    BusTemplate.PanelRow[I][J] := TPanel.Create(Self);
  end;
  Result[I] := BusTemplate.PanelRow[I] ;
 end;
end;

procedure TForm1.CreatePanelDisplay(PR: TPanelRow);
Var
I,J : Integer;
SeatNumber : Integer;
begin
SeatNumber := 0;
BusTemplate.Initialize;
//Create Left/Right Lane Seats
 for I := 0 to High(PR) do /// Row Loop
 begin
  if I > 0 then
   BusTemplate.Prev_Top := BusTemplate.Pan_Height + BusTemplate.Prev_Top + BusTemplate.BufferVal;
   for J := 0 to High(PR[I]) do /// Seats Per Row Loop
   begin
     PR[I][J].Height  := BusTemplate.Pan_Height;
     PR[I][J].Width   := Round(BusTemplate.Pan_Width div (Length(PR[I]) Div 2));
     PR[I][J].Visible := True;
     PR[I][J].Top     := BusTemplate.Prev_Top;
     PR[I][J].Parent  := ScrollBox1;
     PR[I][J].OnMouseEnter := OnMouseEnter;
     PR[I][J].OnMouseLeave := OnMouseLeave;
     Inc(SeatNumber);
     if Not (J >= (Length(PR[I]) Div 2)) then
     begin
      PR[I][J].left    := BusTemplate.Pass_Left + BusTemplate.BufferVal;
      PR[I][J].caption := LabelSeats(Length(PR) - I,SeatNumber);
      BusTemplate.Pass_Left := BusTemplate.Pass_Left + PR[I][J].Width;
     end
     else
     begin
      PR[I][J].left    := (ScrollBox1.Width - ((BusTemplate.Pan_Width + BusTemplate.BufferVal) - BusTemplate.Driv_Left));
      PR[I][J].caption := LabelSeats(Length(PR) - I,SeatNumber);
      BusTemplate.Driv_Left := BusTemplate.Driv_Left + PR[I][J].Width;
     end;
   end;/// End Seats Per Row Loop
   BusTemplate.Pass_Left := 0;
   BusTemplate.Driv_Left := 0 ;
   SeatNumber := 0;
 end;/// End Row Loop
end;

procedure TForm1.CreateSeats;
Var
X,Y: Integer;
begin

Y := seSPR.Value;
X := seRow.Value;

 if (X <> 0) and (Y <> 0) then
  CreatePanelDisplay(BuildSeatArray(X,Y));

end;

procedure TForm1.DestroySeats;
var
I,J:Integer;
begin
//Frees the seat controls
 for I := 0 to Length(BusTemplate.PanelRow) - 1 do
 begin
   For J := 0 to Length(BusTemplate.PanelRow[I]) - 1 do
   begin
    BusTemplate.PanelRow[I][J].Free;
   end;
   SetLength(BusTemplate.PanelRow[I],0);
 end;
 SetLength(BusTemplate.PanelRow,0);
end;

procedure TForm1.FindStudent(Sender: TObject);
Var
I,J: Integer;
begin
 for I := 0 to high(BusTemplate.PanelRow) do
 begin
   for J:= 0 to high(BusTemplate.PanelRow[I]) do
   begin
    if Sender = Bustemplate.PanelRow[I][J] then
     label2.Caption := BusTemplate.StudentInfo[J].Name;
   end;
 end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  I: Integer;
begin
for I := 0 to high(BusTemplate.StudentInfo) do
begin
  BusTemplate.StudentInfo[I].Free;
end;

BusTemplate.Free;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
BusTemplate := TTemplate.Create;
end;

Function TForm1.LabelSeats(R, S: Integer): String;
begin
Result := 'R' + inttostr(R) + 'S' + inttostr(S);
end;

class procedure TForm1.OnMouseEnter(Sender: Tobject);
begin
Form1.FindStudent(Sender);
end;

class procedure TForm1.OnMouseLeave(Sender: Tobject);
begin
Form1.Label2.Caption := '';
end;

procedure TForm1.ResetSpinValues;
begin
seRow.Value     := 0;
seSPR.Value     := 0;
end;

procedure TForm1.seRowChange(Sender: TObject);
begin
UpdateSeats;
end;

procedure TForm1.seSPRChange(Sender: TObject);
begin
UpdateSeats;
end;

procedure TForm1.UpdateSeats;
begin
DestroySeats;
CreateSeats;
end;

{ TTemplate }

Procedure TTemplate.Initialize;
var
I:Integer;
begin
   for I := 0 to High(StudentInfo)do
   begin
     StudentInfo[I] := TStudent.Create;
   end;
   StudentInfo[0].Name := 'Pablo Cruise';
   StudentInfo[1].Name := 'Miguel Sanchez';
   StudentInfo[2].Name := 'The Eradicator';
   StudentInfo[3].Name := 'Fatty McGee';

   Prev_Top  := BufferVal;
   Pass_Left := 0;
   Driv_Left := 0;
end;


end. 
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.