?
Solved

One event for multiple shapes.

Posted on 2004-11-29
5
Medium Priority
?
357 Views
Last Modified: 2012-05-05
Hey,

I want to have one event for all shapes on a form. All shapes are already added to the form.
I want that if you click on the shape that it will turn red and that it will turn white if it already is red.
Ofcourse this would be easy if you just go to each OnMouseDown of every shape but I just want one OnMouseDown event for all the shapes, so that event should be able to recognize what shape was being clicked on.

Thnx!
0
Comment
Question by:TonyJiz
5 Comments
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 120 total points
ID: 12695496
yep, create one event-proc like

procedure ShapeClick(sender : TObject);
begin
  if (sender is TShape) then
    if TShape(sender).Brush.Color = clRed then
      TShape(sender).Brush.Color := clWhite
    else
      TShape(sender).Brush.Color := clRed;
end;

assign this proc to  all shapes
(currently i dont know if a TShape has an onClick-event)

meikl ;-)

 
0
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 120 total points
ID: 12695637
@Meikl, A TShape has NO onClick event in Delphi 5. Not sure about D7. However, as Tony said, it does have an OnMouseDown event that you can respond to. :-)

Btw, if you want a funny, alternative solution then use TWO events! Something like this:

procedure TFormMain.ShapeMouseDownWhite( Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer );
begin
  if ( Sender is TShape ) then begin
    ( Sender as TShape ).Brush.Color := clRed;
    ( Sender as TShape ).OnMouseDown := ShapeMouseDownRed;
  end;
end;

procedure TFormMain.ShapeMouseDownRed( Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer );
begin
  if ( Sender is TShape ) then begin
    ( Sender as TShape ).Brush.Color := clWhite;
    ( Sender as TShape ).OnMouseDown := ShapeMouseDownWhite;
  end;
end;

But as Meikl suggested, it is easier to use this:

procedure TFormMain.ShapeMouseDownWhite( Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer );
begin
  if ( Sender is TShape ) then begin
    ( Sender as TShape ).Brush.Color := clRed;
    ( Sender as TShape ).OnMouseDown := ShapeMouseDownRed;
  end;
end;

procedure TFormMain.ShapeMouseDownRed( Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer );
begin
  if ( Sender is TShape ) then begin
    if ( ( Sender as TShape ).Brush.Color = clWhite ) then begin
      ( Sender as TShape ).Brush.Color := clRed;
    end
    else begin
      ( Sender as TShape ).Brush.Color := clWhite;
    end;
  end;
end;

Or if you want it look very neat for your teacher (if this is a homework assignment):

procedure TFormMain.ShapeMouseDownWhite( Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer );
const
  Colors: array[ False..True ] of TColor = ( clWhite, clRed );
begin
  if ( Sender is TShape ) then ( Sender as TShape ).Brush.Color := Colors[ ( Sender as TShape ).Brush.Color = clWhite ];
end;
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 260 total points
ID: 12695639
Hi meikl,
TShape has not event OnClick, but has event OnMouseDown:

procedure TForm1.Shape1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if (Button=mbLeft) then
  begin
    if (Sender is TShape) then
      if TShape(Sender).Brush.Color = clRed then
        TShape(Sender).Brush.Color := clWhite
      else
        TShape(Sender).Brush.Color := clRed;
  end;
end;
0
 

Author Comment

by:TonyJiz
ID: 12695666
Thnx!
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12695677
:-)) well, ok, no onClick-event
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question