/// <summary> /// Verifies that all the settings are correct, throwing an exception if not. For all settings left at defaults, /// stores a value deemed appropriate based on the other settings.</summary> private void deduceAndCheckSettings() { if (Buttons == null || Buttons.Length == 0) { Buttons = new[] { DlgMessageForm.DefaultOKCaption } } ; if (Buttons.Length > 4) { throw new ArgumentException("The number of message buttons must not exceed 4. Actual number: {0}".Fmt(Buttons.Length)); } if (Message == null) { Message = ""; } if (Caption == null) { Caption = DlgMessageForm.DefaultCaption[(int)Type]; } if (Image == null && Type != DlgType.Custom) { Image = DlgMessageForm.GetDefaultImage(Type); } if (AcceptButton < 0) { AcceptButton = 0; } if (CancelButton < 0) { CancelButton = Buttons.Length - 1; } if (AcceptButton > Buttons.Length - 1 || CancelButton > Buttons.Length - 1) { throw new ArgumentException("AcceptButton or CancelButton is too large for the specified number of buttons."); } }
/// <summary> /// Creates a message form and fills in the controls using the current settings. Does not verify whether the /// settings are valid, so must only be used internally and only after the settings have been verified.</summary> /// <returns> /// The index of the button pressed.</returns> private int showAsIs() { using (var form = new DlgMessageForm()) { if (Image != null) { form.img.Image = Image; form.img.Visible = true; } form.Text = Caption; form.Font = SystemFonts.MessageBoxFont; if (Font != null) { form.Message.Font = Font; } form.Message.MaximumSize = new Size(Math.Max(600, Screen.PrimaryScreen.WorkingArea.Width / 2), Screen.PrimaryScreen.WorkingArea.Height * 3 / 4); form.Message.Text = Format == DlgMessageFormat.PlainText ? EggsML.Escape(Message) : Message; // --- Buttons - captions, visibility, accept/cancel Button[] Btn = new Button[4]; Btn[0] = form.Btn0; Btn[1] = form.Btn1; Btn[2] = form.Btn2; Btn[3] = form.Btn3; form.AcceptButton = null; form.CancelButton = null; for (int i = Buttons.Length - 1; i >= 0; i--) { Btn[i].Visible = true; Btn[i].Text = Buttons[i]; Btn[i].SendToBack(); // otherwise table layout ordering is messed up } form.AcceptButton = Btn[AcceptButton]; form.CancelButton = Btn[CancelButton]; form.Shown += (dummy1, dummy2) => Btn[AcceptButton].Focus(); // otherwise the AcceptButton has no effect // --- Ding if (Environment.OSVersion.Platform != PlatformID.Unix && Environment.OSVersion.Platform != PlatformID.MacOSX) { switch (Type) { case DlgType.Info: WinAPI.MessageBeep(WinAPI.MessageBeepType.Information); break; case DlgType.Question: WinAPI.MessageBeep(WinAPI.MessageBeepType.Question); break; case DlgType.Warning: WinAPI.MessageBeep(WinAPI.MessageBeepType.Warning); break; case DlgType.Error: WinAPI.MessageBeep(WinAPI.MessageBeepType.Error); break; } } // --- Show form.ShowInTaskbar = ShowInTaskbar; var result = form.ShowDialog(); // --- Return button index switch (result) { case DialogResult.OK: return(0); case DialogResult.Cancel: return(1); case DialogResult.Yes: return(2); case DialogResult.No: return(3); default: // Should be unable to get here throw new Exception("Internal exception in Util: unreachable code"); } } }