Forum > Beginners

Improving custom message box

(1/3) > >>

pascal111:
Hi! everyone! this is a unit has a procedure taken from another page in the forum for making a custom message box. And this is the code with my small changes. I would like to add "mtInformation" icon and don't know how to do it, and you can tell me with any more improvements for this custom message box, and I'll appreciate that.

the unit with my changes:

https://github.com/pascal111-fra/Lazarus-programs/blob/main/helper1.pas

Original code here:
https://forum.lazarus.freepascal.org/index.php?topic=34704.0

wp:
I reworked your code, added the "Information" icon from the Lazarus resource, removed memory leaks and the crash when other_form is nil, and auto-aligned the controls in the form. Set the BiDiMode of your main form the bdRightToLeft to get right-to-left text direction.


--- 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";}};} ---uses  LCLType; procedure MessageBox2(title_string, msg_string:string; other_form:tform);var  frmDialog : TForm;  lblMsg : TLabel;  btnClose: TBitBtn;  panel: TPanel;  img: TImage;begin   // Original code is in:  // https://forum.lazarus.freepascal.org/index.php?topic=34704.0   frmDialog:=TForm.CreateNew(nil);  try    with frmDialog do    begin      panel := TPanel.Create(frmDialog);      panel.Align := alClient;      panel.caption := '';      panel.BevelOuter := bvNone;      panel.Parent := frmDialog;      panel.AutoSize := true;      panel.Constraints.MinWidth := 300;            img := TImage.Create(frmDialog);      img.Align := alLeft;      img.BorderSpacing.Around := 12;      img.AutoSize := true;      img.Parent := panel;      img.Picture.Assign(GetDialogIcon(idDialogInfo));      // Other options to be used by GetDialogIcon are      //  idDialogWarning, idDialogError, idDialogConfirm, idDialogShield       lblMsg := TLabel.Create(frmDialog);      lblMsg.AutoSize:=True;      lblMsg.Align:=alClient;      lblMsg.BorderSpacing.Around := 12;      lblMsg.Caption:=msg_string;      lblMsg.Wordwrap := true;      lblMsg.Parent:=panel;       btnClose := TBitBtn.Create(frmDialog);      btnClose.Parent:=frmDialog;      btnClose.Kind:=bkOK;      btnClose.Align:=alBottom;      btnClose.AutoSize := true;      btnClose.BorderSpacing.Around := 6;       AutoSize := True;      BorderStyle:=bsDialog;      BiDiMode := Application.MainForm.BiDiMode;      caption:=title_string;      if other_form <> nil then      begin        top:=other_form.Top+(other_form.Width div 3);        left:=other_form.Left+(other_form.height div 2);      end else        Position := poMainFormCenter;      ShowModal;    end;  finally    frmDialog.Free;  end;end;

pascal111:
Good job! @wp,
I can say it's perfect if I could have the power of moving the icon as I want little to right or bottom, also if I can move the label little to the left or to bottom, also if  I can make the OK button narrower. How can I do that if I liked? I tried to modify some properties and faild to do it.

I made small changes in your code and made some tests:

https://i.postimg.cc/MKtTKbc5/pas2-Untitled-1-copy.jpg


--- 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";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,  LCLIntf, Buttons, helper1, LCLType, ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Label1: TLabel;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private   public   end; var  Form1: TForm1;  procedure MessageBox3(title_string, msg_string:string; other_form:tform); implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);   begin   end; procedure TForm1.Button1Click(Sender: TObject);begin    MessageBox3('أهلاً','بسم الله الرحمن الرحيم'#13#10'الحمد لله'#13#10'كيف حالكم' ,form1);     //MessageBox3('أهلاً','عُذراً' ,form1);end; procedure MessageBox3(title_string, msg_string:string; other_form:tform);var  frmDialog : TForm;  lblMsg : TLabel;  btnClose: TBitBtn;  panel: TPanel;  img: TImage;begin   // Original code is in:  // https://forum.lazarus.freepascal.org/index.php?topic=34704.0   frmDialog:=TForm.CreateNew(nil);  try    with frmDialog do    begin      panel := TPanel.Create(frmDialog);      panel.Align := alClient;      panel.caption := '';      panel.BevelOuter := bvNone;      panel.Parent := frmDialog;      panel.AutoSize := true;      panel.Constraints.MinWidth := 300;       img := TImage.Create(frmDialog);      img.Align := alLeft;      img.BorderSpacing.Around := 12;      img.AutoSize := true;      img.Parent := panel;      img.Picture.Assign(GetDialogIcon(idDialogInfo));      // Other options to be used by GetDialogIcon are      //  idDialogWarning, idDialogError, idDialogConfirm, idDialogShield       lblMsg := TLabel.Create(frmDialog);      lblMsg.AutoSize:=True;      lblMsg.Align:=alRight;      lblMsg.BorderSpacing.Around := 12;      lblMsg.Caption:=msg_string;      lblMsg.Wordwrap := true;      lblMsg.Parent:=panel;       btnClose := TBitBtn.Create(frmDialog);      btnClose.Parent:=frmDialog;      btnClose.Kind:=bkOK;      btnClose.Align:=alBottom;      btnClose.AutoSize := true;      btnClose.BorderSpacing.Around := 6;      btnClose.Caption:='حسناً';       AutoSize := True;      BorderStyle:=bsDialog;      BiDiMode := Application.MainForm.BiDiMode;      caption:=title_string;      if other_form <> nil then      begin        top:=other_form.Top+(other_form.Width div 3);        left:=other_form.Left+(other_form.height div 2);      end else        Position := poMainFormCenter;      ShowModal;    end;  finally    frmDialog.Free;  end;end;   end.  

wp:
Please attach images to your forum posts instead of some third-party cloud sites which will be gone at some time in the future.

The controls in the messagebox are aligned, anchored and auto-sized to avoid any size-calculations. Therefore, you cannot move them by changing their Left, Top, etc properities. Due due this kind of layout their position is determined by the BorderSpacing properties. I now replace the BorderSpacing.Around (which applies to all sides) by individual parameters BorderSpacing.Left, BorderSpacing.Top, etc. The attached modified code contains comments which explain to which distance parameter the current value of the BorderSpacing applies.

My layout is designed for left-to-right reading direction, such as in Europe. If you want to have right-to-left mode like in the Near-East, you should set the BiDiMode of the form to bdRightToLeft, and this should exchange the left and right coordinates and render text from right to left. In case of the messagebox, the messagebox takes the BiDiMode value of the main form and sets its own BiDiMode accordingly. Test in the attached demo.

pascal111:

--- Quote ---Please attach images to your forum posts instead of some third-party cloud sites which will be gone at some time in the future.

--- End quote ---


I tried some other ways and failed maybe due our internet here in my area in Egypt. This is the best site I have. If you know better way, tell me.

I think I like your last configurations on your code, and can't ask you to do more efforts, it's not my right, and you denoted it to us freely, thanks for that and I hope I can help you too as a reward.

I tested the code under Red Hat platform and it seems so nice:
https://i.postimg.cc/76Jw2pKq/Screenshot-from-2022-06-30-06-29-56.png

So small changes I made in your code:


--- 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";}};} ---procedure MessageBox3(title_string, msg_string:string; other_form:tform);var  frmDialog : TForm;  lblMsg : TLabel;  btnClose: TBitBtn;  panel: TPanel;  img: TImage;begin   // Original code is in:  // https://forum.lazarus.freepascal.org/index.php?topic=34704.0   frmDialog:=TForm.CreateNew(nil);  try    with frmDialog do    begin      BiDiMode := bdRightToLeft;  //Application.MainForm.BiDiMode;       panel := TPanel.Create(frmDialog);      panel.Align := alClient;      panel.caption := '';      panel.BevelOuter := bvNone;      panel.Parent := frmDialog;      panel.AutoSize := true;      panel.Constraints.MinWidth := 300;       img := TImage.Create(frmDialog);      if BiDiMode = bdRightToLeft then      begin        img.Align := alRight;        img.BorderSpacing.Right := 12;  // Distance of image to right side of form      end else       begin        img.Align := alLeft;        img.BorderSpacing.Left := 12;   // Distance of image to left side of form      end;      img.BorderSpacing.Top := 12;      // Distance of image to top side of form      img.AutoSize := true;      img.Parent := panel;      img.Picture.Assign(GetDialogIcon(idDialogInfo));      // Other options to be used by GetDialogIcon are      //  idDialogWarning, idDialogError, idDialogConfirm, idDialogShield       lblMsg := TLabel.Create(frmDialog);      lblMsg.AutoSize:=True;      lblMsg.Align := alClient;      if BiDiMode = bdRightToLeft then      begin        lblMsg.BorderSpacing.Right := 12;  // Distance of label to image        lblMsg.BorderSpacing.Left := 12;   // Distance of label to left side of form      end else      begin        lblMsg.BorderSpacing.Left := 12;   // Distance of label to image        lblMsg.BorderSpacing.Right := 12;  // Distance of label to right side of form      end;      lblMsg.BorderSpacing.Top := 12;      lblMsg.Caption:=msg_string;      lblMsg.Wordwrap := true;      lblMsg.Parent:=panel;       panel := TPanel.Create(frmDialog);      panel.Align := alBottom;      panel.AutoSize := true;      panel.BevelOuter := bvNone;      panel.Caption := '';      panel.Parent := frmDialog;            btnClose := TBitBtn.Create(frmDialog);      btnClose.Parent := panel;      btnClose.Kind:=bkOK;      btnClose.Anchors := [akLeft, akTop];      btnClose.AnchorSideLeft.Control := panel;      btnClose.AnchorSideLeft.Side := asrCenter;       btnClose.AutoSize := true;      btnClose.BorderSpacing.Top := 12;     // Distance of button to image or label, whichever is nearer      btnClose.BorderSpacing.Bottom := 6;   // Distance of button to bottom side of form      btnClose.Caption:='حسناً';       AutoSize := True;      BorderStyle:=bsDialog;      caption:=title_string;      if other_form <> nil then      begin        top:=other_form.Top+(other_form.Width div 3);             // shouldn't be other_form.Height here?        left:=other_form.Left+(other_form.height div 2);          // and other_form.Width here?      end else        Position := poMainFormCenter;      ShowModal;    end;  finally    frmDialog.Free;  end;end;  

Navigation

[0] Message Index

[#] Next page

Go to full version