using System;
using System.Drawing;
using System.Windows.Forms;
namespace Xylem.Common.Utils.UiInvoker
{
///
/// Helper class for UI invoking
///
public static class UiInvoker
{
///
/// Control invoker for process bar.
///
///
/// - Initial
///
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();
}
}
///
/// Control invoker for enable and disable. The control remains visible.
/// The last element gets always the focus.
///
///
///
///
/// - Initial
///
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();
}
}
///
/// Control invoker to hide or display control.
///
///
///
///
/// - Initial
///
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();
}
}
///
/// Control invoker with message, visibility on and new color.
///
///
/// - Initial
///
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();
}
}
///
/// Tool strip invoker with message, visibility on and new color.
///
///
/// - Initial
///
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}";
}
}
///
/// Tool strip invoker with message, visibility on and new color.
///
///
/// - Initial
///
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();
}
}
}
}