Forum > LCL

Mouse Events for all components

(1/2) > >>

Pe3s:
Hello, how can I implement events for all TPanel components, I mean onMouseEnter, onMouseLeave events
I wrote a loop but I have a problem with the MouseEnter and MouseLeave events

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  Form1: TForm1;   const  _clMouseEntered = clRed;  _clMouseLeaved = clHighlight; implementation {$R *.frm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var  i: Integer;begin   for i := 0 to ComponentCount-1 do    if Components[i] is TPanel then with TPanel(Components[i]) do    begin      OnMouseEnter :=      OnMouseLeave :=    end;end;    

Handoko:
Hello Pe3s,
Welcome to the forum.

It should be something like this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  { TForm1 }   TForm1 = class(TForm)     .     .  private    procedure MyPanelMouseEnter(Sender: TObject);    procedure MyPanelMouseLeave(Sender: TObject);     .     .  end;     .     . implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var  i: Integer;begin   for i := 0 to ComponentCount-1 do    if Components[i] is TPanel then with TPanel(Components[i]) do    begin      OnMouseEnter := @MyPanelMouseEnter;      OnMouseLeave := @MyPanelMouseLeave;    end;end;

Pe3s:
I tried to change the color of the tPanel component, but it does not work


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)    Panel1: TPanel;    Panel2: TPanel;    Panel3: TPanel;    Panel4: TPanel;    procedure FormCreate(Sender: TObject);  private    procedure MyPanelMouseEnter(Sender: TObject);    procedure MyPanelMouseLeave(Sender: TObject);  public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 }  procedure TForm1.FormCreate(Sender: TObject);var  i: Integer;begin   for i := 0 to ComponentCount-1 do    if Components[i] is TPanel then with TPanel(Components[i]) do    begin      OnMouseEnter:=@MyPanelMouseEnter;      OnMouseLeave:=@MyPanelMouseLeave;    end;end; procedure TForm1.MyPanelMouseEnter(Sender: TObject);begin end; procedure TForm1.MyPanelMouseLeave(Sender: TObject);begin end; end.              
I have a problem with completing the procedures

procedure TForm1.MyPanelMouseEnter(Sender: TObject);
begin
     Color:= clRed;
end;

procedure TForm1.MyPanelMouseLeave(Sender: TObject);
begin
    Color:= clLime;

end;   

jamie:
Place a "Beep" in those events to determine if the message is getting called...

if so then you most likely need to call Repaint after changing the color.


 TPanel(Sender).Repaint;

devEric69:
Reusing the transtyping \ typecasting (like jamie showed), you should call TPanel(Sender).Color:= clRed; in your @MyPanelMouseEnter and @MyPanelMouseLeave commun events shared between the panels.

Navigation

[0] Message Index

[#] Next page

Go to full version