293 lines
9.4 KiB
C#
293 lines
9.4 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Xylem.Common.Utils.UiInvoker
|
|
{
|
|
/// <summary>
|
|
/// Helper class for UI invoking
|
|
/// </summary>
|
|
[Serializable]
|
|
public static class UiInvoker
|
|
{
|
|
/// <summary>
|
|
/// Control invoker for process bar.
|
|
/// </summary>
|
|
/// <remarks date="2020-Dec-22" author="Thomas Wiedebusch">
|
|
/// - Initial
|
|
/// </remarks>
|
|
/// <remarks date="2024-Apr-08" author="Thomas Wiedebusch">
|
|
/// - Try/catch added.
|
|
/// </remarks>
|
|
public static void ProgressBarInvoker(ProgressBar progressBar, Int32 value = 0, Boolean visible = true)
|
|
{
|
|
try
|
|
{
|
|
if (progressBar == null)
|
|
return;
|
|
if (value > 100)
|
|
value = 100;
|
|
if (value < 0)
|
|
value = 0;
|
|
if (progressBar.InvokeRequired)
|
|
{
|
|
progressBar.Invoke(new Action(() =>
|
|
{
|
|
progressBar.Visible = visible;
|
|
progressBar.Value = value;
|
|
progressBar.Update();
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
progressBar.Visible = visible;
|
|
progressBar.Value = value;
|
|
progressBar.Update();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// ignored
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Control invoker for enable and disable. The control remains visible.
|
|
/// The last element gets always the focus.
|
|
/// </summary>
|
|
/// <param name="ctrl"></param>
|
|
/// <param name="enabled"></param>
|
|
/// <remarks date="2020-Dec-22" author="Thomas Wiedebusch">
|
|
/// - Initial
|
|
/// </remarks>
|
|
/// <remarks date="2021-Apr-09" author="Thomas Wiedebusch">
|
|
/// - Avoid recurrent activation of identical state.
|
|
/// </remarks>
|
|
/// <remarks date="2024-Apr-08" author="Thomas Wiedebusch">
|
|
/// - Try/catch added.
|
|
/// </remarks>
|
|
public static void ControlEnableInvoker(Control ctrl, Boolean enabled)
|
|
{
|
|
try
|
|
{
|
|
if (ctrl == null)
|
|
return;
|
|
if (ctrl.InvokeRequired)
|
|
{
|
|
ctrl.Invoke(new Action(() =>
|
|
{
|
|
if (ctrl.Enabled == enabled)
|
|
return;
|
|
|
|
ctrl.Enabled = enabled;
|
|
ctrl.Update();
|
|
ctrl.Focus();
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
if (ctrl.Enabled == enabled)
|
|
return;
|
|
|
|
ctrl.Enabled = enabled;
|
|
ctrl.Update();
|
|
ctrl.Focus();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// ignored
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Control invoker to hide or display control.
|
|
/// </summary>
|
|
/// <param name="ctrl"></param>
|
|
/// <param name="visible"></param>
|
|
/// <remarks date="2020-Dec-23" author="Thomas Wiedebusch">
|
|
/// - Initial
|
|
/// </remarks>
|
|
/// <remarks date="2021-Apr-09" author="Thomas Wiedebusch">
|
|
/// - Avoid recurrent activation of identical state.
|
|
/// </remarks>
|
|
/// <remarks date="2024-Apr-08" author="Thomas Wiedebusch">
|
|
/// - Try/catch added.
|
|
/// </remarks>
|
|
public static void ControlViewInvoker(Control ctrl, Boolean visible)
|
|
{
|
|
try
|
|
{
|
|
if (ctrl == null)
|
|
return;
|
|
if (ctrl.InvokeRequired)
|
|
{
|
|
ctrl.Invoke(new Action(() =>
|
|
{
|
|
if (ctrl.Visible == visible)
|
|
return;
|
|
|
|
ctrl.Visible = visible;
|
|
ctrl.Update();
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
if (ctrl.Visible == visible)
|
|
return;
|
|
|
|
ctrl.Visible = visible;
|
|
ctrl.Update();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// ignored
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Control invoker with message, visibility on and new color.
|
|
/// </summary>
|
|
/// <remarks date="2021-Feb-25" author="Thomas Wiedebusch">
|
|
/// - Initial
|
|
/// </remarks>
|
|
/// <remarks date="2021-Apr-09" author="Thomas Wiedebusch">
|
|
/// - Avoid recurrent activation of identical state.
|
|
/// </remarks>
|
|
/// <remarks date="2024-Apr-08" author="Thomas Wiedebusch">
|
|
/// - Try/catch added.
|
|
/// </remarks>
|
|
public static void ControlInvoker(Control ctrl, Color color, String msg = "", Boolean visible = true)
|
|
{
|
|
try
|
|
{
|
|
if (ctrl == null)
|
|
return;
|
|
if (ctrl.InvokeRequired)
|
|
{
|
|
ctrl.Invoke(new Action(() =>
|
|
{
|
|
if (ctrl.Visible == visible && ctrl.Text == msg && ctrl.ForeColor == color)
|
|
return;
|
|
|
|
ctrl.Visible = visible;
|
|
ctrl.ForeColor = color;
|
|
ctrl.Text = $@"{msg}";
|
|
ctrl.Update();
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
if (ctrl.Visible == visible && ctrl.Text == msg && ctrl.ForeColor == color)
|
|
return;
|
|
|
|
ctrl.Visible = visible;
|
|
ctrl.ForeColor = color;
|
|
ctrl.Text = $@"{msg}";
|
|
ctrl.Update();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// ignored
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tool strip invoker with message, visibility on and new color.
|
|
/// </summary>
|
|
/// <remarks date="2021-Feb-25" author="Thomas Wiedebusch">
|
|
/// - Initial
|
|
/// </remarks>
|
|
/// <remarks date="2021-Apr-09" author="Thomas Wiedebusch">
|
|
/// - Avoid recurrent activation of identical state.
|
|
/// </remarks>
|
|
/// <remarks date="2024-Apr-08" author="Thomas Wiedebusch">
|
|
/// - Try/catch added.
|
|
/// </remarks>
|
|
public static void ToolStripLabelInvoker(ToolStripStatusLabel ctrl, Color color, String msg = "",
|
|
Boolean visible = true)
|
|
{
|
|
try
|
|
{
|
|
if (ctrl?.GetCurrentParent() == null)
|
|
return;
|
|
if (ctrl.GetCurrentParent().InvokeRequired)
|
|
{
|
|
ctrl.GetCurrentParent().Invoke(new Action(() =>
|
|
{
|
|
if (ctrl.Visible == visible && ctrl.Text == msg && ctrl.ForeColor == color)
|
|
return;
|
|
|
|
ctrl.Visible = visible;
|
|
ctrl.ForeColor = color;
|
|
ctrl.Text = $@"{msg}";
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
if (ctrl.Visible == visible && ctrl.Text == msg && ctrl.ForeColor == color)
|
|
return;
|
|
|
|
ctrl.Visible = visible;
|
|
ctrl.ForeColor = color;
|
|
ctrl.Text = $@"{msg}";
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// ignored
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tool strip invoker with message, visibility on and new color.
|
|
/// </summary>
|
|
/// <remarks date="2021-Feb-25" author="Thomas Wiedebusch">
|
|
/// - Initial
|
|
/// </remarks>
|
|
/// <remarks date="2021-Apr-09" author="Thomas Wiedebusch">
|
|
/// - Avoid recurrent activation of identical state.
|
|
/// </remarks>
|
|
/// <remarks date="2024-Apr-08" author="Thomas Wiedebusch">
|
|
/// - Try/catch added.
|
|
/// </remarks>
|
|
public static void ToolStripInvoker(ToolStrip ctrl, Color color, String msg = "", Boolean visible = true)
|
|
{
|
|
try
|
|
{
|
|
if (ctrl == null)
|
|
return;
|
|
if (ctrl.InvokeRequired)
|
|
{
|
|
ctrl.Invoke(new Action(() =>
|
|
{
|
|
if (ctrl.Visible == visible && ctrl.Text == msg && ctrl.ForeColor == color)
|
|
return;
|
|
|
|
ctrl.Visible = visible;
|
|
ctrl.ForeColor = color;
|
|
ctrl.Text = $@"{msg}";
|
|
ctrl.Update();
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
if (ctrl.Visible == visible && ctrl.Text == msg && ctrl.ForeColor == color)
|
|
return;
|
|
|
|
ctrl.Visible = visible;
|
|
ctrl.ForeColor = color;
|
|
ctrl.Text = $@"{msg}";
|
|
ctrl.Update();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// ignored
|
|
}
|
|
}
|
|
}
|
|
}
|