Raz-Soft » Blog Archive » Message box with check box Buy Cheap Software
Imagini live din… metrou »
« CUCU

Message box with check box

  Here is an easy way to make a custom message box with a check box bellow the buttons. No need for MessageBox hooks or any other "magics", just plain VCL:

C++:
  1. TModalResult CheckMessageDialog(AnsiString Message, AnsiString Caption,
  2.   AnsiString CheckBoxCaption, bool *Checked,
  3.   TMsgDlgType DlgType, TMsgDlgButtons Buttons)
  4. {
  5.    TForm *Dialog=CreateMessageDialog(Message, DlgType, Buttons);
  6.    if (!Caption.IsEmpty()) Dialog->Caption = Caption;
  7.  
  8.    int LeftEdge=Dialog->ClientWidth;
  9.    int TopEdge =Dialog->ClientHeight;
  10.    if (Dialog->ControlCount>0) {
  11.      for (int i = 1; i <Dialog->ControlCount; i++) {
  12.        TControl *Ctrl=Dialog->Controls[i];
  13.        if (Ctrl->Left <LeftEdge) LeftEdge=Ctrl->Left;
  14.        if (Ctrl->Top  <TopEdge)  TopEdge =Ctrl->Top;
  15.      }
  16.    } else {
  17.      LeftEdge=10;
  18.      TopEdge=10;
  19.    }
  20.  
  21.    TCheckBox * CheckBox = new TCheckBox(Dialog);
  22.    CheckBox->Parent  = Dialog;
  23.    CheckBox->Caption = CheckBoxCaption;
  24.    CheckBox->Checked = *Checked;
  25.    CheckBox->Left    = LeftEdge;
  26.    CheckBox->Top     = Dialog->ClientHeight;
  27.    CheckBox->Width   = Dialog->Canvas->TextWidth(CheckBox->Caption)+30;
  28.  
  29.    Dialog->ClientHeight = Dialog->ClientHeight + CheckBox->Height + TopEdge;
  30.  
  31.    if (CheckBox->Width + LeftEdge*2> Dialog->ClientWidth)
  32.      Dialog->ClientWidth = CheckBox->Width + LeftEdge*2;
  33.  
  34.    TModalResult Result = Dialog->ShowModal();
  35.    *Checked = CheckBox->Checked;
  36.  
  37.    delete Dialog;
  38.    Dialog=NULL;
  39.    
  40.    return Result;
  41. }

Using it is simple :

C++:
  1. bool checkv=true//checkbox  value
  2. TModalResult res= CheckMessageDialog("Are you sure you want to do whatever you were supposed to do?",
  3.                 "The caption","Don't show this message again",&checkv,mtConfirmation,
  4.                 TMsgDlgButtons() <<mbYes <<mbNo  <<mbIgnore);
  5.  
  6. ShowMessage("Check box " + AnsiString(checkv ? "checked":"not cheked") + "\n dialog result:" + AnsiString(res));

related things:

Author: Raz | On January 21st, 2007 | C++ Builder, [ En ] | 2 Comments

Q: So what can I do next?
A: You can Buy me a Beer or Coffee. You can say Hi! or peek on the related stuff. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

    2 Responses to “Message box with check box”

  1. Stuart UNITED KINGDOM Says:

    delete Dialog;Hi do we not need to delete the Checkbox before the above line?
    delete CheckBox;
    delete Dialog;

    Cheers Stu


  2. Jamie UNITED STATES Says:

    just meandered by....

    Love that google, great site. Take care....


Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> and all YM emotions