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++:
-
TModalResult CheckMessageDialog(AnsiString Message, AnsiString Caption,
-
AnsiString CheckBoxCaption, bool *Checked,
-
TMsgDlgType DlgType, TMsgDlgButtons Buttons)
-
{
-
TForm *Dialog=CreateMessageDialog(Message, DlgType, Buttons);
-
if (!Caption.IsEmpty()) Dialog->Caption = Caption;
-
-
int LeftEdge=Dialog->ClientWidth;
-
int TopEdge =Dialog->ClientHeight;
-
if (Dialog->ControlCount>0) {
-
for (int i = 1; i <Dialog->ControlCount; i++) {
-
TControl *Ctrl=Dialog->Controls[i];
-
if (Ctrl->Left <LeftEdge) LeftEdge=Ctrl->Left;
-
if (Ctrl->Top <TopEdge) TopEdge =Ctrl->Top;
-
}
-
} else {
-
LeftEdge=10;
-
TopEdge=10;
-
}
-
-
TCheckBox * CheckBox = new TCheckBox(Dialog);
-
CheckBox->Parent = Dialog;
-
CheckBox->Caption = CheckBoxCaption;
-
CheckBox->Checked = *Checked;
-
CheckBox->Left = LeftEdge;
-
CheckBox->Top = Dialog->ClientHeight;
-
CheckBox->Width = Dialog->Canvas->TextWidth(CheckBox->Caption)+30;
-
-
Dialog->ClientHeight = Dialog->ClientHeight + CheckBox->Height + TopEdge;
-
-
if (CheckBox->Width + LeftEdge*2> Dialog->ClientWidth)
-
Dialog->ClientWidth = CheckBox->Width + LeftEdge*2;
-
-
TModalResult Result = Dialog->ShowModal();
-
*Checked = CheckBox->Checked;
-
-
delete Dialog;
-
Dialog=NULL;
-
-
return Result;
-
}
Using it is simple :
C++:
-
bool checkv=true; //checkbox value
-
TModalResult res= CheckMessageDialog("Are you sure you want to do whatever you were supposed to do?",
-
"The caption","Don't show this message again",&checkv,mtConfirmation,
-
TMsgDlgButtons() <<mbYes <<mbNo <<mbIgnore);
-
-
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.
Spammers: Beware of » my Dog! «





2 Responses to “Message box with check box”
September 26th, 2007 at 8:35 pm
delete Dialog;Hi do we not need to delete the Checkbox before the above line?delete CheckBox;
delete Dialog;
Cheers Stu
November 20th, 2008 at 6:50 pm
just meandered by....
Love that google, great site. Take care....