tbf/TBF/BenchControl/Elde/CoverTest/CoverTestForm.cs

114 lines
2.7 KiB
C#

///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.Drawing;
using System.Windows.Forms;
using log4net;
using TBF.Resources;
namespace TBF.BenchControl.Elde.CoverTest
{
public partial class CoverTestForm : Form
{
ILog bypassLog;
string message;
/// Set to 'true' when the form closes
public bool Completed { get { return completed; } }
bool completed;
Users.Grp.GID[] bypassLevel;
public CoverTestForm(string message, Users.Grp.GID[] bypassLevel, ILog bypassLog)
{
InitializeComponent();
ControlBox = false;
this.message = message;
this.bypassLevel = bypassLevel;
this.bypassLog = bypassLog;
Localize();
messageLabel.Text = this.message;
float textWidth = Graphics.FromImage(new Bitmap(1, 1)).MeasureString(messageLabel.Text, messageLabel.Font).Width;
int rqrdFormWidth = (int)textWidth + 80; /// Form width calculated from the text width
if (rqrdFormWidth > Width)
{
int shift = (rqrdFormWidth - Width) / 2;
Width = rqrdFormWidth;
bypassButton.Left += shift;
}
completed = false;
StartForceCloseHandler();
}
void Localize()
{
Text = Strings.Message;
bypassButton.Text = Strings.Bypass;
}
private void bypassButton_Click(object sender, EventArgs e)
{
if (!Users.GlobalData.CurrentUser.IsMemberOf(bypassLevel))
{
if ((new Users.Forms.LoginDlg(bypassLevel)).ShowDialog() != DialogResult.OK)
{
return;
}
else
{
if (Program.MainWnd != null) Program.MainWnd.UpdateUser();
}
}
bypassLog.InfoFormat("{0} bypassed by {1}", message, Users.GlobalData.CurrentUser.UserName);
completed = true;
Close();
}
#region Forced close handling
/// <summary>
/// Used by CoverTest, AskYesNoOp and MessageBoxOp.
/// </summary>
public void OnCloseThisForm(object sender, EventArgs args)
{
if (CloseThisFormHandler == null) return;
try
{
CloseThisFormHandler(sender, null);
}
catch (Exception e)
{
}
}
public event EventHandler<EventArgs> CloseThisFormHandler;
public void StartForceCloseHandler()
{
CloseThisFormHandler += delegate(object sender, EventArgs args)
{
if (InvokeRequired)
Invoke(new EventHandler<EventArgs>(OnForceClose), sender, args);
else
OnForceClose(sender, args);
};
}
private void OnForceClose(object sender, EventArgs args)
{
DialogResult = DialogResult.Cancel;
Close();
}
#endregion
}
}