📅  最后修改于: 2023-12-03 14:39:33.244000             🧑  作者: Mango
In this guide, we will explore how to implement a form modal popup using Bootstrap 4 in Delphi. A modal popup is a dialog window that appears on top of the main content and requires user interaction to continue. This is a commonly used UI pattern for displaying additional information or capturing user input.
To follow along with this tutorial, you will need the following:
To use Bootstrap 4 in your Delphi project, you need to add the necessary CSS and JavaScript files.
bootstrap.min.css
file to your Delphi project's folder.bootstrap.min.css
file as a resource.BorderStyle
property to bsNone
to give it a modal-like appearance.TButton
to the form for triggering the modal popup.OnClick
event handler.procedure TForm1.Button1Click(Sender: TObject);
begin
// Show the modal popup
WebBrowser1.Navigate('about:blank');
WebBrowser1.Document.Write('<html><head><link rel="stylesheet" type="text/css" href="bootstrap.min.css" /></head><body>');
WebBrowser1.Document.Write('<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">');
WebBrowser1.Document.Write('<div class="modal-dialog" role="document">');
WebBrowser1.Document.Write('<div class="modal-content">');
WebBrowser1.Document.Write('<div class="modal-header">');
WebBrowser1.Document.Write('<h5 class="modal-title" id="myModalLabel">Modal Title</h5>');
WebBrowser1.Document.Write('<button type="button" class="close" data-dismiss="modal" aria-label="Close">');
WebBrowser1.Document.Write('<span aria-hidden="true">×</span>');
WebBrowser1.Document.Write('</button>');
WebBrowser1.Document.Write('</div>');
WebBrowser1.Document.Write('<div class="modal-body">');
WebBrowser1.Document.Write('Modal body content goes here.');
WebBrowser1.Document.Write('</div>');
WebBrowser1.Document.Write('<div class="modal-footer">');
WebBrowser1.Document.Write('<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>');
WebBrowser1.Document.Write('<button type="button" class="btn btn-primary">Save changes</button>');
WebBrowser1.Document.Write('</div>');
WebBrowser1.Document.Write('</div>');
WebBrowser1.Document.Write('</div>');
WebBrowser1.Document.Write('</div>');
WebBrowser1.Document.Write('</body></html>');
WebBrowser1.Document.ParentWindow.ExecScript('$("#myModal").modal();', 'JavaScript');
end;
Make sure to replace 'bootstrap.min.css'
with the correct path of the CSS file if necessary.
Congratulations! You have successfully implemented a form modal popup using Bootstrap 4 in Delphi. Modal popups are a great way to enhance user experience by displaying additional information or capturing user input without leaving the current page. Feel free to customize the modal as per your project requirements.