I have a strange problem. I have created a component as a modal dialog to act as a menu. (I created it because of focus issues when working with PDFs not allowing for accelerator keys on other components). The user clicks a button or presses Alt+o (the button's hot key) to open the dialog. If the user clicks the button everything works as it should, but if the user presses the accelerator key instead, the modal dialog briefly flashes open and then immediately closes before any user interaction with the dialog form. To complicate matters, if I put a breakpoint on the line DropDownDialog.ShowModal; and then singlestep that line the dialog will remain open and wait for user input. If I put the breakpoint on the following line instead, the window closes before the code reaches the breakpoint.
The menu is a listbox and the code of the Execute function looks like this:
function JDropDown.Execute: Boolean;
var Position: TPoint; Idx: Integer;
begin
Result := False;
DropDownDialog := TDropDownDialog.Create(Application);
try
DropDownDialog.ListBox1.Items.Assign(FItems);
Position := Button.ClientToScreen(Point(Button.Left-FOffsetX,Button.Top+FOffsetY));
DropDownDialog.Left := Position.X;
DropDownDialog.Top := Position.Y;
DropDownDialog.ModalResult := mrNone;
DropDownDialog.ShowModal;
If mItemIndex <> -1 then
begin
FItemSelected := mItemIndex-1;
DropDownDialog.Free;
end else
begin
FItemSelected := -1;
DropDownDialog.Free;
end;
Result := True;
except
DropDownDialog.Free;
end;
end;
The button on the main form has this code:
procedure TForm1.Button1Click(Sender: TObject);
begin
If JDropDown1.Execute then
begin
ShowMessage(IntToStr(JDropDown1.ItemIndex));
end;
end;
and the code on the dialog form is
procedure TDropDownDialog.ListBox1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
ModalResult := mrNone;
If Key <> 27 then
begin
If NotFound(Key) then
begin
mItemIndex := -1;
end else
begin
mItemIndex:= ListBox1.ItemIndex+1;
end;
end else mItemIndex := -1;
ModalResult := mrOK;
end;
procedure TDropDownDialog.ListBox1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var N: Integer;
begin
ModalResult := mrNone;
mItemIndex := ListBox1.ItemIndex+1;
ModalResult := mrOK;
end;
The listbox is OwnerDrawFixed. Can anyone see what I'm doing wrong? Any help greatly appreciated.