/* * ScrollingMessageBox.cs * * Copyright © 2010 * Diversified Technical Systems, Inc. * All Rights Reserved */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace DTS.Common.Utilities { /// /// Crude form for displaying modal yes/no dialog queries. /// public partial class ScrollingMessageBox : Form { public int TimeWait { get; set; } = 0; public ScrollBars ScrollBarStyle { get => messageTextBox.ScrollBars; set => messageTextBox.ScrollBars = value; } public void SetButtonFocus(DialogResult res) { var maxIndex = System.Math.Max(YesButton.TabIndex, NoButton.TabIndex); var minIndex = System.Math.Min(YesButton.TabIndex, NoButton.TabIndex); switch (res) { case DialogResult.No: AcceptButton = NoButton; NoButton.TabIndex = minIndex; YesButton.TabIndex = maxIndex; break; case DialogResult.Yes: default: AcceptButton = YesButton; YesButton.TabIndex = minIndex; NoButton.TabIndex = maxIndex; break; } } public bool ShowYesNo { get => YesButton.Visible && NoButton.Visible; set { YesButton.Visible = value; NoButton.Visible = value; } } public Size FormSize { get => Size; set { if (value == Size) { return; } //Size oldSize = Size; Size = value; //FormResize(oldSize, Size); } } public bool ShowClose { get => btnClose.Visible; set => btnClose.Visible = value; } /// /// Initialize an instance of the class. /// /// /// /// The message to be displayed in the dialog's scroll-enabled /// text field. Note that it does not recognize "\n" as newline; rather use /// Environmenet.Newline. /// /// /// /// The caption for the dialog. /// /// public ScrollingMessageBox(string message, string caption) { try { SuspendLayout(); InitializeComponent(); ResumeLayout(); Text = caption; messageTextBox.Text = message; messageTextBox.Select(0, 0); //this.YesButton.Focus( ); KeyPreview = true; KeyDown += new KeyEventHandler(ScrollingMessageBox_KeyDown); } catch (Exception ex) { throw new ApplicationException("encountered problem constructing " + GetType().FullName, ex); } } /// /// Initialize an instance of the class. /// /// /// /// The message to be displayed in the dialog's scroll-enabled /// text field. Note that it does not recognize "\n" as newline; rather use /// Environmenet.Newline. /// /// /// /// The caption for the dialog. /// /// icon for the dialog /// public ScrollingMessageBox(string message, string caption, Icon ico) { try { SuspendLayout(); InitializeComponent(); ResumeLayout(); Text = caption; messageTextBox.Text = message; messageTextBox.Select(0, 0); //this.YesButton.Focus(); KeyPreview = true; KeyDown += new KeyEventHandler(ScrollingMessageBox_KeyDown); Icon = ico; //this.ResizeBegin += new EventHandler(ScrollingMessageBox_ResizeBegin); //this.ResizeEnd += new EventHandler(ScrollingMessageBox_ResizeEnd); } catch (Exception ex) { throw new ApplicationException("encountered problem constructing " + GetType().FullName, ex); } } /* private Size beforeSize = Size.Empty; void ScrollingMessageBox_ResizeBegin(object sender, EventArgs e) { beforeSize = Size; } void ScrollingMessageBox_ResizeEnd(object sender, EventArgs e) { Size afterSize = Size; FormResize(beforeSize, afterSize); } private void FormResize(Size before, Size after) { int xChange = after.Width - before.Width; int yChange = after.Height - before.Height; Size newSize = messageTextBox.Size; newSize.Height += yChange; newSize.Width += xChange; messageTextBox.Size = newSize; //if (btnClose.Visible) //{ Point loc = btnClose.Location; loc.Y += yChange; loc.X = ((after.Width - btnClose.Width) / 2); btnClose.Location = loc; //} //else //{ loc = YesButton.Location; loc.X += xChange / 2; loc.Y += yChange; YesButton.Location = loc; loc = NoButton.Location; loc.X += xChange / 2; loc.Y += yChange; NoButton.Location = loc; //} NoButton.Focus(); }*/ /// /// handle the key down for yes and no /// note - this isn't i18N'd, so the keycodes might need to change whenever we do change this /// from just "yes", "no" /// /// /// private void ScrollingMessageBox_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Y) { YesButtonClick(); e.Handled = true; return; } else if (e.KeyCode == Keys.N) { NoButtonClick(); e.Handled = true; return; } e.Handled = false; } /// /// Handler for YesButton click events. /// /// /// /// The responsible for generating this event. /// /// /// /// The for this event. /// /// private void YesButton_Click(object sender, EventArgs e) { YesButtonClick(); } /// /// Handler for NoButton click events. /// /// /// /// The responsible for generating this event. /// /// /// /// The for this event. /// /// private void NoButton_Click(object sender, EventArgs e) { NoButtonClick(); } /// /// do yes /// private void YesButtonClick() { DialogResult = DialogResult.Yes; Close(); } /// /// do no /// private void NoButtonClick() { DialogResult = DialogResult.No; Close(); } private void TimerTick(object sender, EventArgs e) { BeginInvoke(new MethodInvoker(delegate () { btnClose.Enabled = true; YesButton.Enabled = true; NoButton.Enabled = true; } )); } public new DialogResult ShowDialog() { CheckTimer(); return base.ShowDialog(); } public new DialogResult ShowDialog(IWin32Window owner) { if (InvokeRequired) { return (DialogResult)Invoke(new Func((DialogResult) => ShowDialog(owner))); } else { CheckTimer(); return base.ShowDialog(owner); } } private void CheckTimer() { if (TimeWait > 0) { _TimeDelay.Interval = TimeWait; _TimeDelay.Start(); btnClose.Enabled = false; YesButton.Enabled = false; NoButton.Enabled = false; } } } }