Dear Alistair

Forgive me, I can't help but try answer this one :)

Let's break it down.

1. Old (bad) code:

procedure TMainform.CheckListBox3Click(Sender: TObject); 
begin
  with sender as TCheckListBox do
    Checklistbox3.Checked[ItemIndex]:= not
Checklistbox3.Checked[ItemIndex];
end;


Ok, looking at this, the With Statement resolves to:

procedure TMainform.CheckListBox3Click(Sender: TObject); 
begin
  Checklistbox3.Checked[(sender as TCheckListBox).ItemIndex]:= not
Checklistbox3.Checked[(sender as TCheckListBox).ItemIndex];
end;

Obviously, this is a bad thing:

a) If you have more than one checklistbox sharing this event, then
CheckListBox3 will be changed (as if by magic) whenever you change the
other checkboxes (doh).

b) If there are other (non CheckListBox) components sharing this event,
you are in for a rather unhappy exception caused by the unsafe typecast
(doh*2).

2. New (slightly improved code)

procedure TMainform.CheckListBox3Click(Sender: TObject); 
begin
  if sender = Checklistbox3 then
  with sender as TCheckListBox do
  Checklistbox3.Checked[ItemIndex]:=not
Checklistbox3.Checked[ItemIndex];
end;

Once again, resolving the with, we get:

procedure TMainform.CheckListBox3Click(Sender: TObject); 
begin
  if sender = Checklistbox3 then
  with sender as TCheckListBox do
  Checklistbox3.Checked[[(sender as TCheckListBox).ItemIndex]:=not
Checklistbox3.Checked[[(sender as TCheckListBox).ItemIndex];
end;

Ok, this is still bad in that

a) If you have more than one checklistbox sharing this event, then
CheckListBox3 will be changed (as if by magic) whenever you change the
other checkboxes.

A better approach would be the following:

procedure TMainform.CheckListBox3Click(Sender: TObject); 
begin
  if sender is TCheckListBox then  
  begin
    with sender as TCheckListBox do 
      Checked[ItemIndex] := not Checked[ItemIndex];
  end
  Else
  Begin
    // I don't know why you would want anything
    // other than a checklistbox firing this event
  End;
end;

Usually, though the use of "Sender" is redundant seeing as the event is
most likely only being triggered by a single component.

Cheers

Stephen

> -----Original Message-----
> From: vss [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, 26 August 2003 1:12 p.m.
> To: Multiple recipients of list delphi
> Subject: Re: [DUG]: Checklist box problem
> 
> 
> Hi Al.
> Um, I might be wrong, but by doing "with sender as 
> TCheckListBox do" you 
> are saying that the Sender "IS" a TCheckBox when it might not 
> be, where 
> as "if sender = Checklistbox3 then" is checking that it is a 
> TCheckBox....if you get my meaning.
> 
> Jeremy
> 
> -----Original Message-----
> From: Alistair George <[EMAIL PROTECTED]>
> To: Multiple recipients of list delphi <[EMAIL PROTECTED]>
> Date: Tue, 26 Aug 2003 12:55:42 +1200
> Subject: [DUG]:  Checklist box problem
> 
> > Hi all.
> > I made some code (below) and it worked fine here but user 
> reported the
> > following:
> > If I click on any option in the "Other Functions" zone 
> (main screen), 
> > there are a lot of display "problems". These problems can vary from 
> > one time to the other.
> > 
> > example 1 : I click on any option in "Other Functions", 
> then all the 
> > options in this zone and the other zones (right of the browser) 
> > disappear from the screen ! If I click on any other zone ("Add
> > settings for example), the options (they had disappeared) 
> re-appear (!)
> > and it is the same for all
> > the other zones who had disappeared (I have to click once and they
> > "re-appear" : magic !) except
> > for the "Other Functions" (the options never re-appear).
> > 
> > example 2 : I click on any option in "Other Functions", 
> then all the 
> > options in this zone disappear and they never re-appear.
> > 
> > This was the original faulty code (designed to allow users 
> to click on 
> > the caption, as well as the checkbox):
> > 
> > procedure TMainform.CheckListBox3Click(Sender: TObject); begin
> > with sender as TCheckListBox do
> > Checklistbox3.Checked[ItemIndex]:=not 
> Checklistbox3.Checked[ItemIndex];
> > end;
> > 
> > 
> > this is the new which I have not tried with him, but also works OK
> > here:
> > procedure TMainform.CheckListBox3Click(Sender: TObject); begin
> > if sender = Checklistbox3 then
> > with sender as TCheckListBox do
> > Checklistbox3.Checked[ItemIndex]:=not 
> Checklistbox3.Checked[ItemIndex];
> > end;
> > 
> > 
> > Before doing the above click event the program had no problems. Any 
> > ideas? Tks Al+
> > 
> > 
> ----------------------------------------------------------------------
> > -
> > ----
> >     New Zealand Delphi Users group - Delphi List - 
> [EMAIL PROTECTED]
> >                   Website: http://www.delphi.org.nz
> > To UnSub, send email to: [EMAIL PROTECTED] 
> > with body of "unsubscribe delphi"
> > Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
> 
> --------------------------------------------------------------
> -------------
>     New Zealand Delphi Users group - Delphi List - 
> [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz
> To UnSub, send email to: [EMAIL PROTECTED] 
> with body of "unsubscribe delphi"
> Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
> 
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

Reply via email to