Common types: there is no implicit link conversion from ToolStripStatusLabel to Control

I want to update the SerialPort DataReceived event handler user interface. I found the problem because the event handler was implicitly started on another thread in the form, so instead of just updating the user interface ...

myLabel.Text = "Some text";

... I had to take the following approach:

    InvokeControlAction<Label>(myLabel, lbl=> lbl.Text= "Some text");
...
    public static void InvokeControlAction<t>(t cont, Action<t> action) where t : Control
    {
        if (cont.InvokeRequired)
        {
            cont.Invoke(new Action<t, Action<t>>(InvokeControlAction),
                          new object[] { cont, action });
        }
        else
        { 
            action(cont); 
        }
    }

So far so good ... However, now I want to update ToolStripStatusLabel - using the same approach, it gives "there is no implicit link conversion between errors between ToolStripStatusLabel and Forms.Control".

From what I read, problems arise because you cannot call ToolStripStatusLabel.

So how do I deal better?

: .. , .

1:. , InvokeControlAction ToolStripStatusLabel, , .

. , .

ExtensionMethod ( ExtensionMethods), InvokeOnToolStripItem, 'using ExtensionMethods;' :

tsStatusValue.InvokeOnToolStripItem(ts => ts.Text = "ALARM signal received");
+3
2

ToolStripStatusLabel Control, , .

, ToolStripStatusLabel ( ToolStripItem ) Invoke. , ToolStrip , GetCurrentParent.

, ToolStripItem:

public static void InvokeOnToolStripItem<T>(this T item, Action<T> action)
    where T : ToolStripItem
{
    ToolStrip parent = item.GetCurrentParent();
    if (parent.InvokeRequired)
    {
        parent.Invoke((Delegate)action, new object[] { item });
    }
    else
    {
        action(item);
    }
}

, :

myToolStripLabel.InvokeOnToolStripItem(label => label.Text = "Updated!");
myToolStripProgressBar.InvokeOnToolStripItem(bar => bar.PerformStep());
+2

,

where t : Control

ToolStripStatusLabel Control.

, : (

0

All Articles