Forum > LCL

BitBtn bkClose closes window even though ModalResult is mrNone. Bug or feature?

<< < (2/3) > >>

I forgot: Another, more general way to prevent a form from closing is the OnCloseQuery event in which you simply set the parameter CanClose to false if the form should remain open. This technique is used primarily for the MainForm. In case of secondary forms it has problems, though, because the form even cannot be closed by the system close button ("x") which is generally very confusing to a user.

Yes, I see, but in my program the "Close" BitBtn is on a panel - to hide the panel - not to close a form.
So I want to prohibit the button to close a form in any case, because its function refers to a panel, not to a form.

Of course, I can use another button that won't close a form, but I think it's a bug that the bkClose BitBtn on Form1 always closes the program, altough it is to send mrNone as ModalResult.


--- Quote from: Aidex on November 12, 2017, 12:26:39 pm ---I think it's a bug that the bkClose BitBtn on Form1 always closes the program, altough it is to send mrNone as ModalResult.

--- End quote ---
Certainly not. That's the way buttons are designed. A modal form executes a continuous loop which exits when its ModalResult is different from mrNone. The click on a button transfers its ModalResult to the Modalresult of the form, and therefore closes the form if the button's Modalresult is not mrNone. If the form is the MainForm then the program quits.

If you want the button to only hide a panel, then the button's ModalResult must be mrNone. But: in my tests it turned out that the form may close if the button's Kind is still mkClose; only after switching it back to mkCustom the form stayed open. (This step preserves the button's Close icon). Then, in the button's OnClick you can hide the panel.

Yes, that's all right and clear to me, but shouldn't the button behave on Form1 in the same way you're using it on Form2 ?!
So I'm still thinking that it's a bug to ignore its mrNone setting (property in IDE) of the bkClose button.
Should I report it as a bug?

No, the "Close" in TBitBtn's Kind property value bkClose  refers specifically to the closing of the button's containing form.
You cannot use a TBitBtn of Kind bkClose for closing panels etc. without working around the tightly integrated ModalResult and form-closing modality that is built in to TBitBtn in the LCL (if you don't want the button also to close the form). Far better to use a non-bkClose button for closing panels etc. when the 'problem' of the form's ModalResult changing behind your back is simply not present.

But a possible work-around would be as follows:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type   TForm1 = class(TForm)    PanelCloseBitBtn: TBitBtn;    Panel1: TPanel;    procedure PanelCloseBitBtnMouseDown(Sender: TObject; Button: TMouseButton;      Shift: TShiftState; X, Y: Integer);    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);  private    PanelClosed: Boolean;  end; var  Form1: TForm1; implementation procedure TForm1.PanelCloseBitBtnMouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);begin  if Sender is TBitBtn then begin    PanelCloseBitBtn.Enabled:=False;    PanelClosed:=True;    Panel1.Visible:=False;  end;end; procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);begin  CanClose:=not PanelCloseBitBtn.Enabled or not PanelClosed;  PanelClosed:=False;end;


[0] Message Index

[#] Next page

[*] Previous page

Go to full version