#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    North Yorkshire (U.K.)
    Posts
    64
    Rep Power
    14

    Dynamic component events


    Hi all

    Anybody know how to assign a mousemove event to a dynamic component

    I have already created a series of checkboxes using (TmpCheck) as a TCheckBox component
    , and want to assign a mouse move event to each of the dynamically created check boxes.

    This is what I think should be the call...
    TmpCheck.OnMouseMove := MyMouseMove( TmpCheck, ?, TmpCheck.Left, TmpCheck.Top );

    The above should then in turn call...
    procedure TForm1.MyMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    begin
    blah blah blah
    end;

    The only problem with calling the procedure, is TShiftState. I tried passing ssShift (as per D7 help files) just to fill the param gap, but D7 returns...
    [Error] Unit1.pas(95): Incompatible types: 'TShiftState' and 'Enumeration'
    [Error] Unit1.pas(95): Incompatible types: 'TMouseMoveEvent' and 'procedure, untyped pointer or untyped parameter'

    Any ideas?

    Cheers
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    In the private section of your form, declare a procedure like this:
    Code:
    	private
    		{ Private declarations }
    		procedure MyMouseMove(Sender: TObject; Shift: TShiftState; X,
    			Y: Integer);
    Then, assign this event to your dynamic checkboxes like this:
    cb.OnMouseMove := MyMouseMove;

    Here's a sample program that I wrote. The program creates 10 checkboxes dynamically when you click on a button. Then, when you move your mouse over a checkbox, the MyMouseMove event handler determines which checkbox it is and adjusts its caption accordingly.
    Code:
    unit Unit1;
    
    interface
    
    uses
    	Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    	Dialogs, StdCtrls;
    
    type
    	TForm1 = class(TForm)
    		Button1: TButton;
    		Edit1: TEdit;
    		procedure Button1Click(Sender: TObject);
    	private
    		{ Private declarations }
    		procedure MyMouseMove(Sender: TObject; Shift: TShiftState; X,
    			Y: Integer);
    	public
    		{ Public declarations }
    	end;
    
    var
    	Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.MyMouseMove(Sender: TObject; Shift: TShiftState; X,
    	Y: Integer);
    begin
    	Edit1.Text := 'Your mouse is now on ' + TCheckBox(Sender).Caption;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
    	cb : TCheckBox;
    	i : integer;
    begin
    	for i := 1 to 10 do
    	begin
    		cb := TCheckBox.Create(self);
    		cb.Parent := self;
    		cb.Caption := 'Checkbox ' + IntToStr(i);
    		cb.Left := 100; cb.Top := i * 20;
    		cb.OnMouseMove := MyMouseMove;
    	end;
    end;
    
    end.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    North Yorkshire (U.K.)
    Posts
    64
    Rep Power
    14

    Thumbs up


    Very helpful - thank you

IMN logo majestic logo threadwatch logo seochat tools logo