Recent

Author Topic: visible and invisible image with onMouseEnter and OnMouseLeave events, Help  (Read 1138 times)

ReinaldoSergioPTBR

  • New Member
  • *
  • Posts: 19
I have a program that when it starts it reads the database and creates panels inside a scrollbox, and inside the created panels are also created images that start with visible = false.
Now the problem starts because I want when I hover over the panel the image of that panel is visible and when the mouse comes off it becomes invisible. But when I hover over any panel their images are not visible but an image from another panel, always the same panel, no matter if I close the program and he opens it again.
I use lazarus 1.8.2 on windows 7.
Code to create panels and images:
procedure TfArrastarSoltarDois.FormShow(Sender: TObject);
var
  //img : TImage;
  pan : TPanel;
  iConPan, iHei, i : Integer;
begin
  box_AFazer.DestroyComponents;
  box_Finalizado.DestroyComponents;
  box_Andamento.DestroyComponents;
  i := 0;
  With dm.qry_TarefaDois do
  Begin
    Close;
    SQL.CLEAR;
    SQL.ADD('SELECT * FROM TB_TAREFA');
    Open;
    FetchAll;
    First;

    iHei := 50;
    While not eof do
    Begin
      inc(i);
      With fArrastarSoltarDois do
      Begin
        if FieldByName('SITUACAO').AsString = 'TAREFA A FAZER' then
        Begin
          iConPan := box_AFazer.ControlCount;
          pan := TPanel.Create(box_AFazer);
          pan.Parent := box_AFazer;
        end
        else if FieldByName('SITUACAO').AsString = 'EM ANDAMENTO' then
        Begin
          iConPan := box_Andamento.ControlCount;
          pan := TPanel.Create(box_Andamento);
          pan.Parent := box_Andamento;
        end
        else if FieldByName('SITUACAO').AsString = 'FINALIZADO' then
        Begin
          iConPan := box_Finalizado.ControlCount;
          pan := TPanel.Create(box_Finalizado);
          pan.Parent := box_Finalizado;
        end;
        pan.Align     := alTop;
        pan.Alignment := taLeftJustify;
        pan.Top       := (iConPan * iHei) + 1;
        pan.Name      := 'pan_'+IntToStr(i);
        pan.Color     := clWhite;
        pan.Height    := iHei;
        pan.Caption   := FieldByName('TAREFA').AsString;
        pan.DragKind  := dkDock;
        pan.DragMode  := dmAutomatic;
        pan.BorderSpacing.Bottom := 10;
        pan.BorderSpacing.Top    := 10;
        pan.BorderSpacing.Left   := 10;
        pan.BorderSpacing.Right  := 10;
        pan.OnEndDock    := @Panel2EndDock;
        pan.OnMouseDown  := @Panel2MouseDown;
        pan.OnMouseEnter := @Panel7MouseEnter;
        pan.OnMouseLeave := @Panel7MouseLeave;
        pan.OnEnter := @Panel7Enter;

        img := TImage.Create(pan);
        img.Parent := pan;
        img.Name   := 'img_'+IntToStr(i);
        img.Top    := 7;
        img.Left   := 276;
        img.Width  := 38;
        img.Height := 38;
        img.Picture.LoadFromFile('C:\Users\sergio\Downloads\editar.png');
        img.OnClick := @Panel2Click;
        img.OnMouseEnter := @Panel7MouseEnter;
        img.OnMouseLeave := @Panel7MouseLeave;
        img.Visible := False;
      end;
      Next;
    end;
  end;
end;

Code to make visible:
procedure TfArrastarSoltarDois.Panel7MouseEnter(Sender: TObject);
begin
  TPanel(Sender).color := $00FBFBFB;
  img.Visible := True;
end;

Code to make invisible :
procedure TfArrastarSoltarDois.Panel7MouseLeave(Sender: TObject);
begin
  TPanel(Sender).color := clWhite;
  img.Visible := False;
end;

if anyone knows how to solve this error thank you

jamie

  • Hero Member
  • *****
  • Posts: 6133
Its doing exactly what you are telling it  :D

You are loading the same image into all the panels...

 so you have created multiple panels with the same image, it will appear to be no different.
Code: Pascal  [Select][+][-]
  1. img.Picture.LoadFromFile('C:\Users\sergio\Downloads\editar.png');
  2.  
The image "editar.png" gets loaded for each panel you create, they are all the same ..
The only true wisdom is knowing you know nothing

RAW

  • Hero Member
  • *****
  • Posts: 868
by the way: you may use "Show" and "Hide" ...  :)

Code: Pascal  [Select][+][-]
  1. myImg.Show;
  2. myImg.Hide;
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

 

TinyPortal © 2005-2018