I have a legacy Delphi 6 program that inexplicably receives a click event. The code has been reduced to the following minimal example for a single form with two RadioButtons:
procedure TForm1.RadioButton1Click(Sender: TObject);
begin
showmessage('rb 1 clicked');
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
RadioButton2.Checked := TRUE;
showmessage('close query');
end;
To demonstrate the problem:
- run the program
- click RadioButton2
- click RadioButton1, which executes
showmessage('rb 1 clicked') - close the form
FormCloseQuery() will turn on RadioButton2 and executes showmessage('close query'), but then RadioButton1Click() also executes again! Why?
(The original program was doing various things when the RadioButtons were used, including making sure to do RadioButton2 stuff before exiting by setting .Checked := TRUE, but all of that has been removed for the demonstration above.)
The sample code can be created just by creating a new Program1 application, dropping RadioButton1 and RadioButton2 on Form1, and creating the two event handlers above. RadioButton1 doesn't even need to start out with the Checked property TRUE.
UPDATE: Inserting RadioButton2.SetFocus after RadioButton2.Checked := TRUE prevents the spurious click event. I'm guessing there's a VCL bug which incorrectly generates an event when focus returns to RadioButton1 after showmessage('close query').