diff --git a/VAR.Toolbox/Controls/ControlsUtils.cs b/VAR.Toolbox/Controls/ControlsUtils.cs index 64a0935..3ece0f2 100644 --- a/VAR.Toolbox/Controls/ControlsUtils.cs +++ b/VAR.Toolbox/Controls/ControlsUtils.cs @@ -1,4 +1,5 @@ -using System.Windows.Forms; +using System.Reflection; +using System.Windows.Forms; namespace VAR.Toolbox.Controls { @@ -8,5 +9,32 @@ namespace VAR.Toolbox.Controls { return size * 96f / ctrl.CreateGraphics().DpiX; } + + private delegate void SetControlPropertyThreadSafeDelegate( + Control control, + string propertyName, + object propertyValue); + + public static void SetControlPropertyThreadSafe( + Control control, + string propertyName, + object propertyValue) + { + if (control.InvokeRequired) + { + control.Invoke(new SetControlPropertyThreadSafeDelegate + (SetControlPropertyThreadSafe), + new object[] { control, propertyName, propertyValue }); + } + else + { + control.GetType().InvokeMember( + propertyName, + BindingFlags.SetProperty, + null, + control, + new object[] { propertyValue }); + } + } } }