public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton) { //Create a new instance of the FlexibleMessageBox form MaterialFlexibleForm MaterialFlexibleForm = new MaterialFlexibleForm { ShowInTaskbar = false, //Bind the caption and the message text CaptionText = caption, MessageText = text }; MaterialFlexibleForm.MaterialFlexibleFormBindingSource.DataSource = MaterialFlexibleForm; //Set the buttons visibilities and texts. Also set a default button. SetDialogButtons(MaterialFlexibleForm, buttons, defaultButton); //Set the dialogs icon. When no icon is used: Correct placement and width of rich text box. SetDialogIcon(MaterialFlexibleForm, icon); //Set the font for all controls MaterialFlexibleForm.Font = FONT; MaterialFlexibleForm.richTextBoxMessage.Font = FONT; //Calculate the dialogs start size (Try to auto-size width to show longest text row). Also set the maximum dialog size. SetDialogSizes(MaterialFlexibleForm, text, caption); //Set the dialogs start position when given. Otherwise center the dialog on the current screen. SetDialogStartPosition(MaterialFlexibleForm, owner); //Show the dialog return(MaterialFlexibleForm.ShowDialog(owner)); }
private static void SetDialogSizes(MaterialFlexibleForm MaterialFlexibleForm, string text, string caption) { //First set the bounds for the maximum dialog size MaterialFlexibleForm.MaximumSize = new Size(Convert.ToInt32(SystemInformation.WorkingArea.Width * GetCorrectedWorkingAreaFactor(MAX_WIDTH_FACTOR)), Convert.ToInt32(SystemInformation.WorkingArea.Height * GetCorrectedWorkingAreaFactor(MAX_HEIGHT_FACTOR))); //Get rows. Exit if there are no rows to render... string[] stringRows = GetStringRows(text); if (stringRows == null) { return; } //Calculate whole text height int textHeight = Math.Min(TextRenderer.MeasureText(text, FONT).Height, 600); //Calculate width for longest text line const int SCROLLBAR_WIDTH_OFFSET = 15; int longestTextRowWidth = stringRows.Max(textForRow => TextRenderer.MeasureText(textForRow, FONT).Width); int captionWidth = TextRenderer.MeasureText(caption, SystemFonts.CaptionFont).Width; int textWidth = Math.Max(longestTextRowWidth + SCROLLBAR_WIDTH_OFFSET, captionWidth); //Calculate margins int marginWidth = MaterialFlexibleForm.Width - MaterialFlexibleForm.richTextBoxMessage.Width; int marginHeight = MaterialFlexibleForm.Height - MaterialFlexibleForm.richTextBoxMessage.Height; //Set calculated dialog size (if the calculated values exceed the maximums, they were cut by windows forms automatically) MaterialFlexibleForm.Size = new Size(textWidth + marginWidth, textHeight + marginHeight); }
private static void SetDialogIcon(MaterialFlexibleForm MaterialFlexibleForm, MessageBoxIcon icon) { switch (icon) { case MessageBoxIcon.Information: MaterialFlexibleForm.pictureBoxForIcon.Image = SystemIcons.Information.ToBitmap(); break; case MessageBoxIcon.Warning: MaterialFlexibleForm.pictureBoxForIcon.Image = SystemIcons.Warning.ToBitmap(); break; case MessageBoxIcon.Error: MaterialFlexibleForm.pictureBoxForIcon.Image = SystemIcons.Error.ToBitmap(); break; case MessageBoxIcon.Question: MaterialFlexibleForm.pictureBoxForIcon.Image = SystemIcons.Question.ToBitmap(); break; default: //When no icon is used: Correct placement and width of rich text box. MaterialFlexibleForm.pictureBoxForIcon.Visible = false; MaterialFlexibleForm.richTextBoxMessage.Left -= MaterialFlexibleForm.pictureBoxForIcon.Width; MaterialFlexibleForm.richTextBoxMessage.Width += MaterialFlexibleForm.pictureBoxForIcon.Width; break; } }
private static void SetDialogStartPosition(MaterialFlexibleForm MaterialFlexibleForm, IWin32Window owner) { //If no owner given: Center on current screen if (owner == null) { Screen screen = Screen.FromPoint(Cursor.Position); MaterialFlexibleForm.StartPosition = FormStartPosition.Manual; MaterialFlexibleForm.Left = screen.Bounds.Left + screen.Bounds.Width / 2 - MaterialFlexibleForm.Width / 2; MaterialFlexibleForm.Top = screen.Bounds.Top + screen.Bounds.Height / 2 - MaterialFlexibleForm.Height / 2; } }
private static void SetDialogButtons(MaterialFlexibleForm MaterialFlexibleForm, MessageBoxButtons buttons, MessageBoxDefaultButton defaultButton) { //Set the buttons visibilities and texts switch (buttons) { case MessageBoxButtons.AbortRetryIgnore: MaterialFlexibleForm.visibleButtonsCount = 3; MaterialFlexibleForm.leftButton.Visible = true; MaterialFlexibleForm.leftButton.Text = MaterialFlexibleForm.GetButtonText(ButtonID.ABORT); MaterialFlexibleForm.leftButton.DialogResult = DialogResult.Abort; MaterialFlexibleForm.middleButton.Visible = true; MaterialFlexibleForm.middleButton.Text = MaterialFlexibleForm.GetButtonText(ButtonID.RETRY); MaterialFlexibleForm.middleButton.DialogResult = DialogResult.Retry; MaterialFlexibleForm.rightButton.Visible = true; MaterialFlexibleForm.rightButton.Text = MaterialFlexibleForm.GetButtonText(ButtonID.IGNORE); MaterialFlexibleForm.rightButton.DialogResult = DialogResult.Ignore; MaterialFlexibleForm.ControlBox = false; break; case MessageBoxButtons.OKCancel: MaterialFlexibleForm.visibleButtonsCount = 2; MaterialFlexibleForm.middleButton.Visible = true; MaterialFlexibleForm.middleButton.Text = MaterialFlexibleForm.GetButtonText(ButtonID.CANCEL); MaterialFlexibleForm.middleButton.DialogResult = DialogResult.Cancel; MaterialFlexibleForm.rightButton.Visible = true; MaterialFlexibleForm.rightButton.Text = MaterialFlexibleForm.GetButtonText(ButtonID.OK); MaterialFlexibleForm.rightButton.DialogResult = DialogResult.OK; MaterialFlexibleForm.CancelButton = MaterialFlexibleForm.middleButton; break; case MessageBoxButtons.RetryCancel: MaterialFlexibleForm.visibleButtonsCount = 2; MaterialFlexibleForm.middleButton.Visible = true; MaterialFlexibleForm.middleButton.Text = MaterialFlexibleForm.GetButtonText(ButtonID.CANCEL); MaterialFlexibleForm.middleButton.DialogResult = DialogResult.Cancel; MaterialFlexibleForm.rightButton.Visible = true; MaterialFlexibleForm.rightButton.Text = MaterialFlexibleForm.GetButtonText(ButtonID.RETRY); MaterialFlexibleForm.rightButton.DialogResult = DialogResult.Retry; MaterialFlexibleForm.CancelButton = MaterialFlexibleForm.middleButton; break; case MessageBoxButtons.YesNo: MaterialFlexibleForm.visibleButtonsCount = 2; MaterialFlexibleForm.middleButton.Visible = true; MaterialFlexibleForm.middleButton.Text = MaterialFlexibleForm.GetButtonText(ButtonID.NO); MaterialFlexibleForm.middleButton.DialogResult = DialogResult.No; MaterialFlexibleForm.rightButton.Visible = true; MaterialFlexibleForm.rightButton.Text = MaterialFlexibleForm.GetButtonText(ButtonID.YES); MaterialFlexibleForm.rightButton.DialogResult = DialogResult.Yes; MaterialFlexibleForm.ControlBox = false; break; case MessageBoxButtons.YesNoCancel: MaterialFlexibleForm.visibleButtonsCount = 3; MaterialFlexibleForm.rightButton.Visible = true; MaterialFlexibleForm.rightButton.Text = MaterialFlexibleForm.GetButtonText(ButtonID.YES); MaterialFlexibleForm.rightButton.DialogResult = DialogResult.Yes; MaterialFlexibleForm.middleButton.Visible = true; MaterialFlexibleForm.middleButton.Text = MaterialFlexibleForm.GetButtonText(ButtonID.NO); MaterialFlexibleForm.middleButton.DialogResult = DialogResult.No; MaterialFlexibleForm.leftButton.Visible = true; MaterialFlexibleForm.leftButton.Text = MaterialFlexibleForm.GetButtonText(ButtonID.CANCEL); MaterialFlexibleForm.leftButton.DialogResult = DialogResult.Cancel; MaterialFlexibleForm.CancelButton = MaterialFlexibleForm.leftButton; break; case MessageBoxButtons.OK: default: MaterialFlexibleForm.visibleButtonsCount = 1; MaterialFlexibleForm.rightButton.Visible = true; MaterialFlexibleForm.rightButton.Text = MaterialFlexibleForm.GetButtonText(ButtonID.OK); MaterialFlexibleForm.rightButton.DialogResult = DialogResult.OK; MaterialFlexibleForm.CancelButton = MaterialFlexibleForm.rightButton; break; } //Set default button (used in MaterialFlexibleForm_Shown) MaterialFlexibleForm.defaultButton = defaultButton; }