common/Utils/UiInvoker/UiInvoker.cs
2026-04-23 17:50:07 +02:00

176 lines
5.4 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
namespace Xylem.Common.Utils.UiInvoker
{
/// <summary>
/// Helper class for UI invoking
/// </summary>
public static class UiInvoker
{
/// <summary>
/// Control invoker for process bar.
/// </summary>
/// <remarks date="2020-Dec-22" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public static void ProgressBarInvoker(ProgressBar progressBar, Int32 value = 0, Boolean visible = true)
{
if (progressBar == null) return;
if (progressBar.InvokeRequired)
{
progressBar.Invoke(new Action(() =>
{
progressBar.Visible = visible;
progressBar.Value = value;
progressBar.Update();
}));
}
else
{
progressBar.Visible = visible;
progressBar.Value = value;
progressBar.Update();
}
}
/// <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>
public static void ControlEnableInvoker(Control ctrl, Boolean enabled)
{
if (ctrl == null) return;
if (ctrl.InvokeRequired)
{
ctrl.Invoke(new Action(() =>
{
ctrl.Enabled = enabled;
ctrl.Update();
ctrl.Focus();
}));
}
else
{
ctrl.Enabled = enabled;
ctrl.Update();
ctrl.Focus();
}
}
/// <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>
public static void ControlViewInvoker(Control ctrl, Boolean visible)
{
if (ctrl == null) return;
if (ctrl.InvokeRequired)
{
ctrl.Invoke(new Action(() =>
{
ctrl.Visible = visible;
ctrl.Update();
}));
}
else
{
ctrl.Visible = visible;
ctrl.Update();
}
}
/// <summary>
/// Control invoker with message, visibility on and new color.
/// </summary>
/// <remarks date="2021-Feb-25" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public static void ControlInvoker(Control ctrl, Color color, String msg = "", Boolean visible = true)
{
if (ctrl == null) return;
if (ctrl.InvokeRequired)
{
ctrl.Invoke(new Action(() =>
{
ctrl.Visible = visible;
ctrl.ForeColor = color;
ctrl.Text = $@"{msg}";
ctrl.Update();
}));
}
else
{
ctrl.Visible = visible;
ctrl.ForeColor = color;
ctrl.Text = $@"{msg}";
ctrl.Update();
}
}
/// <summary>
/// Tool strip invoker with message, visibility on and new color.
/// </summary>
/// <remarks date="2021-Feb-25" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public static void ToolStripLabelInvoker(ToolStripStatusLabel ctrl, Color color, String msg = "", Boolean visible = true)
{
if (ctrl == null) return;
if (ctrl.GetCurrentParent().InvokeRequired)
{
ctrl.GetCurrentParent().Invoke(new Action(() =>
{
ctrl.Visible = visible;
ctrl.ForeColor = color;
ctrl.Text = $@"{msg}";
}));
}
else
{
ctrl.Visible = visible;
ctrl.ForeColor = color;
ctrl.Text = $@"{msg}";
}
}
/// <summary>
/// Tool strip invoker with message, visibility on and new color.
/// </summary>
/// <remarks date="2021-Feb-25" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public static void ToolStripInvoker(ToolStrip ctrl, Color color, String msg = "", Boolean visible = true)
{
if (ctrl == null) return;
if (ctrl.InvokeRequired)
{
ctrl.Invoke(new Action(() =>
{
ctrl.Visible = visible;
ctrl.ForeColor = color;
ctrl.Text = $@"{msg}";
ctrl.Update();
}));
}
else
{
ctrl.Visible = visible;
ctrl.ForeColor = color;
ctrl.Text = $@"{msg}";
ctrl.Update();
}
}
}
}