/// <summary> /// show form as dialog with given parameters /// </summary> /// <param name="AMessage">Message to be displayed to the user</param> /// <param name="ACaption">Caption of the dialog window</param> /// <param name="AChkOptionText">Text to be shown with check box (check box hidden if text empty)</param> /// <param name="AButtons">Button set to be displayed</param> /// <param name="ADefaultButton">The button with a default action</param> /// <param name="AIcon">Icon to be displayed</param> /// <param name="AOptionSelected">initial value for option check box</param> /// <returns></returns> public TFrmExtendedMessageBox.TResult ShowDialog(string AMessage, string ACaption, string AChkOptionText, TFrmExtendedMessageBox.TButtons AButtons, TFrmExtendedMessageBox.TDefaultButton ADefaultButton, TFrmExtendedMessageBox.TIcon AIcon, bool AOptionSelected) { string ResourceDirectory; string IconFileName; ToolTip ButtonTooltip = new ToolTip(); // initialize return values FResult = TResult.embrUndefined; FOptionSelected = AOptionSelected; txtMessage.Text = AMessage; txtMessage.BorderStyle = BorderStyle.FixedSingle; txtMessage.HideSelection = true; txtMessage.SelectionStart = 0; txtMessage.SelectionLength = 0; txtMessage.Font = new System.Drawing.Font(txtMessage.Font, FontStyle.Regular); pnlLeftButtons.MinimumSize = new Size(btnHelp.Width + btnCopy.Width + 10, pnlLeftButtons.Height); ButtonTooltip.SetToolTip(btnHelp, "Help"); ButtonTooltip.SetToolTip(btnCopy, "Copy to Clipboard"); this.Text = ACaption; chkOption.Text = AChkOptionText; if (AChkOptionText.Length == 0) { chkOption.Visible = false; } chkOption.Checked = AOptionSelected; this.MinimumSize = new System.Drawing.Size(pnlLeftButtons.Width + pnlRightButtons.Width, 250); btnYes.Visible = false; btnYesToAll.Visible = false; btnNo.Visible = false; btnNoToAll.Visible = false; btnOK.Visible = false; btnCancel.Visible = false; FDefaultButton = ADefaultButton; switch (AButtons) { case TButtons.embbYesYesToAllNoCancel: btnYes.Visible = true; btnYesToAll.Visible = true; btnNo.Visible = true; btnCancel.Visible = true; break; case TButtons.embbYesYesToAllNoNoToAllCancel: btnYes.Visible = true; btnYesToAll.Visible = true; btnNo.Visible = true; btnNoToAll.Visible = true; btnCancel.Visible = true; break; case TButtons.embbYesYesToAllNoNoToAll: btnYes.Visible = true; btnYesToAll.Visible = true; btnNo.Visible = true; btnNoToAll.Visible = true; break; case TButtons.embbYesNo: btnYes.Visible = true; btnNo.Visible = true; break; case TButtons.embbYesNoCancel: btnYes.Visible = true; btnNo.Visible = true; btnCancel.Visible = true; break; case TButtons.embbOK: btnOK.Visible = true; break; case TButtons.embbOKCancel: btnOK.Visible = true; btnCancel.Visible = true; break; default: break; } // dispose of items in case they were used already earlier if (FBitmap != null) { FBitmap.Dispose(); } // find the right icon name switch (AIcon) { case TIcon.embiQuestion: IconFileName = "Help.ico"; break; case TIcon.embiInformation: IconFileName = "PetraInformation.ico"; break; case TIcon.embiWarning: IconFileName = "Warning.ico"; break; case TIcon.embiError: IconFileName = "Error.ico"; break; default: IconFileName = ""; break; } if (FIconControl == null) { FIconControl = new PictureBox(); // Stretches the image to fit the pictureBox. FIconControl.SizeMode = PictureBoxSizeMode.StretchImage; FIconControl.ClientSize = new Size(30, 30); pnlIcon.Padding = new Padding(3, 3, 3, 3); } // load and set the image ResourceDirectory = TAppSettingsManager.GetValue("Resource.Dir"); if ((AIcon != TIcon.embiNone) && System.IO.File.Exists(ResourceDirectory + System.IO.Path.DirectorySeparatorChar + IconFileName)) { pnlIcon.Visible = true; FBitmap = new System.Drawing.Bitmap(ResourceDirectory + System.IO.Path.DirectorySeparatorChar + IconFileName); FIconControl.Image = (Image)FBitmap; if (!pnlIcon.Controls.Contains(FIconControl)) { pnlIcon.Controls.Add(FIconControl); } } else { // remove icon panel if it already exists if (pnlIcon.Controls.Contains(FIconControl)) { pnlIcon.Controls.Remove(FIconControl); } pnlIcon.Visible = false; } // remove the controlbox as we do not need these options (min, max and close_ this.ControlBox = false; // now show the actual dialog this.StartPosition = FormStartPosition.CenterScreen; this.ShowDialog(); // FResult is initialized when buttons are pressed return(FResult); }
void TFrmExtendedMessageBox_Shown(object sender, EventArgs e) { // Set the button positions based on their visibilty // Note - we have to do this in _Shown because before then we cannot check for Visible // We treat OK Cancel as being in a separate group so they get a bit more space between them and the others. // We rely on the fact that not all the buttons will be visible - true because we do not offer OK at the same time as Yes(ToAll) No(ToAll) int distance = btnYesToAll.Left - btnYes.Left; bool bDoneSmallDistance = false; bool bHasOkOrCancel = (btnOK.Visible || btnCancel.Visible); // We ignore btnApply - it was set to invisible at design time so never was placed on the button panel Button[] buttons = { btnYes, btnYesToAll, btnNo, btnNoToAll, btnOK, btnCancel }; // Go through the buttons starting on the right for (int btnID = 5; btnID >= 0; btnID--) { if (!buttons[btnID].Visible) { if (bDoneSmallDistance || !bHasOkOrCancel) { // Move the button a standard distance to the right for (int k = 0; k < btnID; k++) { buttons[k].Left += distance; } } else { // We need to move the left group of buttons a smaller distance to the right because we want to get a gap between the two groups for (int k = 0; (k < btnID) && (k < 4); k++) { buttons[k].Left += (distance - 20); } for (int k = 4; k < btnID; k++) { buttons[k].Left += distance; } // set the flag because we only want to do this smaller distance once bDoneSmallDistance = true; } } } // Set up the default button if one was specified. if (FDefaultButton != TDefaultButton.embdDefButtonNone) { // This is a 'counter' TDefaultButton currentDefID = TDefaultButton.embdDefButtonNone; for (int btnID = 0; btnID < buttons.Length; btnID++) { if (buttons[btnID].Visible) { // increment the counter and see if we have reached the one we are looking for currentDefID++; if (currentDefID == FDefaultButton) { // Set the accept button for the form and focus it. this.FindForm().AcceptButton = buttons[btnID]; buttons[btnID].Focus(); break; } } } } // Calculate the size required for the message, assuming the width is as it is SizeF size = txtMessage.CreateGraphics().MeasureString(txtMessage.Text, txtMessage.Font, pnlMessage.Width - 20); // Will it fit on the screen?? int screenHeight = Screen.PrimaryScreen.WorkingArea.Height; int maxHeight = Convert.ToInt32(0.8 * screenHeight); if (size.Height > maxHeight) { size.Height = Convert.ToInt32(0.75 * maxHeight); this.txtMessage.ScrollBars = ScrollBars.Vertical; } // Now set the height of the form based on allowing enough height for the message Form myForm = this.FindForm(); int checkBoxAllowance = (chkOption.Visible) ? chkOption.Height + 5 : 0; myForm.Size = new Size(this.FindForm().Width, Convert.ToInt32(size.Height) + pnlLeftButtons.Height + 120 + checkBoxAllowance); // Centre the form vertically myForm.Location = new Point(myForm.Location.X, (screenHeight - myForm.Height) / 2); // Now center the message in its panel and anchor it txtMessage.Size = new Size(pnlMessage.Width - 20, pnlMessage.Height - 20); txtMessage.Location = new Point((pnlMessage.Width - txtMessage.Width) / 2, (pnlMessage.Height - txtMessage.Height) / 2); txtMessage.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right; }
/// <summary> /// show form as dialog with given parameters /// </summary> /// <param name="AMessage">Message to be displayed to the user</param> /// <param name="ACaption">Caption of the dialog window</param> /// <param name="AChkOptionText">Text to be shown with check box (check box hidden if text empty)</param> /// <param name="AButtons">Button set to be displayed</param> /// <param name="ADefaultButton">The button with a default action</param> /// <param name="AIcon">Icon to be displayed</param> /// <param name="AOptionSelected">initial value for option check box</param> /// <returns></returns> public TFrmExtendedMessageBox.TResult ShowDialog(string AMessage, string ACaption, string AChkOptionText, TFrmExtendedMessageBox.TButtons AButtons, TFrmExtendedMessageBox.TDefaultButton ADefaultButton, TFrmExtendedMessageBox.TIcon AIcon, bool AOptionSelected) { string ResourceDirectory; string IconFileName; ToolTip ButtonTooltip = new ToolTip(); // initialize return values FResult = TResult.embrUndefined; FOptionSelected = AOptionSelected; txtMessage.Text = AMessage; txtMessage.BorderStyle = BorderStyle.FixedSingle; txtMessage.HideSelection = true; txtMessage.SelectionStart = 0; txtMessage.SelectionLength = 0; txtMessage.Font = new System.Drawing.Font(txtMessage.Font, FontStyle.Regular); pnlLeftButtons.MinimumSize = new Size(btnHelp.Width + btnCopy.Width + 10, pnlLeftButtons.Height); ButtonTooltip.SetToolTip(btnHelp, "Help"); ButtonTooltip.SetToolTip(btnCopy, "Copy to Clipboard"); this.Text = ACaption; chkOption.Text = AChkOptionText; if (AChkOptionText.Length == 0) { chkOption.Visible = false; } chkOption.Checked = AOptionSelected; this.MinimumSize = new System.Drawing.Size(pnlLeftButtons.Width + pnlRightButtons.Width, 250); btnYes.Visible = false; btnYesToAll.Visible = false; btnNo.Visible = false; btnNoToAll.Visible = false; btnOK.Visible = false; btnCancel.Visible = false; FDefaultButton = ADefaultButton; switch (AButtons) { case TButtons.embbYesYesToAllNoCancel: btnYes.Visible = true; btnYesToAll.Visible = true; btnNo.Visible = true; btnCancel.Visible = true; break; case TButtons.embbYesYesToAllNoNoToAllCancel: btnYes.Visible = true; btnYesToAll.Visible = true; btnNo.Visible = true; btnNoToAll.Visible = true; btnCancel.Visible = true; break; case TButtons.embbYesYesToAllNoNoToAll: btnYes.Visible = true; btnYesToAll.Visible = true; btnNo.Visible = true; btnNoToAll.Visible = true; break; case TButtons.embbYesNo: btnYes.Visible = true; btnNo.Visible = true; break; case TButtons.embbYesNoCancel: btnYes.Visible = true; btnNo.Visible = true; btnCancel.Visible = true; break; case TButtons.embbOK: btnOK.Visible = true; break; case TButtons.embbOKCancel: btnOK.Visible = true; btnCancel.Visible = true; break; default: break; } // dispose of items in case they were used already earlier if (FBitmap != null) { FBitmap.Dispose(); } // find the right icon name switch (AIcon) { case TIcon.embiQuestion: IconFileName = "Help.ico"; break; case TIcon.embiInformation: IconFileName = "PetraInformation.ico"; break; case TIcon.embiWarning: IconFileName = "Warning.ico"; break; case TIcon.embiError: IconFileName = "Error.ico"; break; default: IconFileName = ""; break; } if (FIconControl == null) { FIconControl = new PictureBox(); // Stretches the image to fit the pictureBox. FIconControl.SizeMode = PictureBoxSizeMode.StretchImage; FIconControl.ClientSize = new Size(30, 30); pnlIcon.Padding = new Padding(3, 3, 3, 3); } // load and set the image ResourceDirectory = TAppSettingsManager.GetValue("Resource.Dir"); if ((AIcon != TIcon.embiNone) && System.IO.File.Exists(ResourceDirectory + System.IO.Path.DirectorySeparatorChar + IconFileName)) { pnlIcon.Visible = true; FBitmap = new System.Drawing.Bitmap(ResourceDirectory + System.IO.Path.DirectorySeparatorChar + IconFileName); FIconControl.Image = (Image)FBitmap; if (!pnlIcon.Controls.Contains(FIconControl)) { pnlIcon.Controls.Add(FIconControl); } } else { // remove icon panel if it already exists if (pnlIcon.Controls.Contains(FIconControl)) { pnlIcon.Controls.Remove(FIconControl); } pnlIcon.Visible = false; } // remove the controlbox as we do not need these options (min, max and close_ this.ControlBox = false; // now show the actual dialog this.StartPosition = FormStartPosition.CenterScreen; this.ShowDialog(); // FResult is initialized when buttons are pressed return FResult; }