Files
DP44/Common/DTS.Common.Utilities/ScrollingMessageBox.cs

311 lines
9.5 KiB
C#
Raw Normal View History

2026-04-17 14:55:32 -04:00
/*
* 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
{
/// <summary>
/// Crude form for displaying modal yes/no dialog queries.
/// </summary>
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;
}
/// <summary>
/// Initialize an instance of the <see cref="ScrollingMessageBox"/> class.
/// </summary>
///
/// <param name="message">
/// The message <see cref="string"/> to be displayed in the dialog's scroll-enabled
/// text field. Note that it does not recognize "\n" as newline; rather use
/// Environmenet.Newline.
/// </param>
///
/// <param name="caption">
/// The caption <see cref="string"/> for the dialog.
/// </param>
///
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);
}
}
/// <summary>
/// Initialize an instance of the <see cref="ScrollingMessageBox"/> class.
/// </summary>
///
/// <param name="message">
/// The message <see cref="string"/> to be displayed in the dialog's scroll-enabled
/// text field. Note that it does not recognize "\n" as newline; rather use
/// Environmenet.Newline.
/// </param>
///
/// <param name="caption">
/// The caption <see cref="string"/> for the dialog.
/// </param>
/// <param name="ico">icon</param> 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();
}*/
/// <summary>
/// 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"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
/// <summary>
/// Handler for YesButton click events.
/// </summary>
///
/// <param name="sender">
/// The <see cref="object"/> responsible for generating this event.
/// </param>
///
/// <param name="e">
/// The <see cref="EventArgs"/> for this event.
/// </param>
///
private void YesButton_Click(object sender, EventArgs e)
{
YesButtonClick();
}
/// <summary>
/// Handler for NoButton click events.
/// </summary>
///
/// <param name="sender">
/// The <see cref="object"/> responsible for generating this event.
/// </param>
///
/// <param name="e">
/// The <see cref="EventArgs"/> for this event.
/// </param>
///
private void NoButton_Click(object sender, EventArgs e)
{
NoButtonClick();
}
/// <summary>
/// do yes
/// </summary>
private void YesButtonClick()
{
DialogResult = DialogResult.Yes;
Close();
}
/// <summary>
/// do no
/// </summary>
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<IWin32Window, DialogResult>((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;
}
}
}
}