Forum > Database
Showmodal affects SQLQuery behavior
mikeosullivan:
My application is running on macOS Monterey 12.6.8 and I'm using Lazarus 2.2.4 with FPC 3.2.2
The application has two forms, unit1 and unit2. Unit 2 is designed as a message dialog with two buttons "Yes" and "No". Its purpose is to allow/disallow record deletion. The reason I'm using a modal form rather than a message dialog (which works perfectly) is for language customisation.
I don't understand why this code does not work:
Excerpt from unit2:
procedure TForm2.btnYesClick(Sender: TObject);
begin
unit1.canDelete:= True;
Form2.Close;
end;
procedure TForm2.btnNoClick(Sender: TObject);
begin
unit1.canDelete:= False;
Form2.Close;
end;
Excerpt from unit1:
procedure TForm1.btnDeleteClick(Sender: TObject);
.......
unit3.Form3.ShowModal;
if canDelete then
begin
SQLQuery1.Delete;
SQLQuery1.ApplyUpdates;
SQLTransaction1.Commit;
end;
If I remove the showModal, it all works fine.
I'm mystified because this code woekd perfectly for years and it has suddenly stopped after recompiling.
Any ideas?
dseligo:
You said that that you have two forms, unit1 and unit2. What is unit3.Form3 then?
You call ShowModal - it will wait there until modal result is returned.
How and when do you return from Form3?
rvk:
--- Quote from: mikeosullivan on August 16, 2023, 08:49:27 am ---procedure TForm2.btnYesClick(Sender: TObject);
begin
unit1.canDelete:= True;
Form2.Close;
end;
--- End quote ---
And don't use Close to close a ShowModal form. Use ModalResult := mrOk (or something else).
Also don't use Form2.Close if you cant to close in TForm2 itself (You don't know if the current instance is actually called Form2).
If you want to close the form in class TForm2 you just call Close or Self.Close. That way any instance/variable with a TForm2 in it will work.
mikeosullivan:
I'm sorry - that was a typo. The last bit should read:
procedure TForm1.btnDeleteClick(Sender: TObject);
.......
unit2.Form2.ShowModal;
if canDelete then
begin
SQLQuery1.Delete;
SQLQuery1.ApplyUpdates;
SQLTransaction1.Commit;
end;
rvk:
With QuestionDlg you can show the dialog in your own language. No need for separate form.
--- 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";}};} ---res := QuestionDlg('Title','Question?', mtCustom, // removes the bitmap [mrYes,'YesOnYourLanguage',mrNo,'NoOnYourLanguage'],0);
Other examples see https://lazarus-ccr.sourceforge.io/docs/lcl/dialogs/questiondlg.html
Like
--- 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";}};} ---// custom modal result values and captionsres := QuestionDlg('Question', 'Is it Okay?', mtConfirmation, [400, 'Yes!!!', 401, 'Not cool', 402, 'My mistake'], 0);
But what stops withing with your code?
At least the Form2.Close is wrong if called via ShowModal.
Try setting ModalResult like I suggested.
https://lazarus-ccr.sourceforge.io/docs/lcl/forms/tcustomform.showmodal.html
Navigation
[0] Message Index
[#] Next page