Common : QuitAppException class added, ModelessForm troubleshooted

This commit is contained in:
Milan Hanajik 2022-01-06 18:47:23 +01:00
parent 3c93e56b67
commit 6150c5b808
4 changed files with 55 additions and 59 deletions

View File

@ -74,6 +74,7 @@
<SubType>Component</SubType>
</Compile>
<Compile Include="ProcedureInfo.cs" />
<Compile Include="QuitAppException.cs" />
<Compile Include="SerializableDictionary.cs" />
<Compile Include="Telegram.cs" />
<Compile Include="UIControls\CoolButtonCtrl.cs">

View File

@ -51,7 +51,6 @@
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "ModelessForm";
this.Load += new System.EventHandler(this.ModelessForm_Load);
this.ResumeLayout(false);
this.PerformLayout();

View File

@ -6,62 +6,27 @@ namespace Common.Forms
{
public partial class ModelessForm : Form
{
/// <summary>
/// Called from the state machine when an operation forces a modeless dialog close.
/// </summary>
public static void UpdateMessage(MessageEventArgs args)
{
if (UpdateMessageHandler == null) return;
try { UpdateMessageHandler(null, args); }
catch (Exception) { }
}
public static event EventHandler<MessageEventArgs> UpdateMessageHandler;
void OnUpdateMessage(object sender, MessageEventArgs args)
{
Message = args.Message;
if (Message != null) label1.Text = Message;
}
/// <summary>
/// Called from the state machine when an operation forces a modeless dialog close.
/// </summary>
public static void CloseForm()
{
if (CloseFormHandler == null) return;
try { CloseFormHandler(null, null); }
catch (Exception) { }
}
public static event EventHandler<EventArgs> CloseFormHandler;
void OnCloseForm(object sender, EventArgs args)
{
DialogResult = DialogResult.Cancel;
Close();
}
public string Title;
public string Message;
public Color BackgroundColor;
public Color TextColor;
public Font TextFont;
public string FontFamily;
public int FontSize;
public FontStyle FontStyle;
/// <summary>
/// Constructor to be used by the application
/// </summary>
/// <param name="title">Window title</param>
/// <param name="message">Displayed message</param>
public ModelessForm()
public ModelessForm(string message, int backArgb = 0, int textArgb = 0, Font textFont = null, string title = null)
{
InitializeComponent();
ControlBox = false;
label1.Text = (message != null) ? message : "Please wait";
BackColor = (backArgb != 0) ? Color.FromArgb(backArgb) : Color.LightGreen;
ForeColor = (textArgb != 0) ? Color.FromArgb(textArgb) : Color.Black;
label1.Font = (textFont != null) ? textFont : new Font("Arial", 14, FontStyle.Regular);
Text = (title != null) ? title : string.Empty;
float textWidth = Graphics.FromImage(new Bitmap(1, 1)).MeasureString(label1.Text, label1.Font).Width;
float textHeight = Graphics.FromImage(new Bitmap(1, 1)).MeasureString(label1.Text, label1.Font).Height;
Width = (int)textWidth + 120; /// Form width calculated from the text width
Height += (int)textHeight; /// Form height calculated from the text height
UpdateMessageHandler += delegate(object sender, MessageEventArgs args)
{
if (InvokeRequired) { Invoke(new EventHandler<MessageEventArgs>(OnUpdateMessage), sender, args); }
@ -75,18 +40,37 @@ namespace Common.Forms
};
}
private void ModelessForm_Load(object sender, EventArgs e)
{
if (Title != null) Text = Title;
if (Message != null) label1.Text = Message;
if (BackgroundColor != null) BackColor = BackgroundColor;
if (TextColor != null) ForeColor = TextColor;
if (FontFamily != null) label1.Font = new Font(FontFamily, FontSize, FontStyle);
float textWidth = Graphics.FromImage(new Bitmap(1, 1)).MeasureString(label1.Text, label1.Font).Width;
float textHeight = Graphics.FromImage(new Bitmap(1, 1)).MeasureString(label1.Text, label1.Font).Height;
Width = (int)textWidth + 120; /// Form width calculated from the text width
Height += (int)textHeight; /// Form height calculated from the text height
/// <summary>
/// Called from the state machine when an operation forces a modeless dialog close.
/// </summary>
public void UpdateMessage(MessageEventArgs args)
{
if (UpdateMessageHandler == null) return;
try { UpdateMessageHandler(null, args); }
catch (Exception) { }
}
public event EventHandler<MessageEventArgs> UpdateMessageHandler;
void OnUpdateMessage(object sender, MessageEventArgs args)
{
if (args.Message != null) label1.Text = args.Message;
}
/// <summary>
/// Called from the state machine when an operation forces a modeless dialog close.
/// </summary>
public static void CloseForm()
{
if (CloseFormHandler == null) return;
try { CloseFormHandler(null, null); }
catch (Exception) { }
}
static public event EventHandler<EventArgs> CloseFormHandler;
void OnCloseForm(object sender, EventArgs args)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}

View File

@ -0,0 +1,12 @@
using System;
namespace Common
{
public class QuitAppException : Exception
{
public QuitAppException(string message)
: base(message)
{
}
}
}