InvokeRequired and ToolStripStatusLabel
Asked Answered
B

3

9

In my application I have class that is responsible for all database actions. It is called from main class and uses delegates to call methods after action is complete. Because it is asynchronous I must use invoke on my GUI, so I've created a simple extensions method:

 public static void InvokeIfRequired<T>(this T c, Action<T> action)
            where T: Control
        {
            if (c.InvokeRequired)
            {
                c.Invoke(new Action(() => action(c)));
            }
            else
            {
                action(c);
            }
        }

This works fine when I try to call it on textBox:

textBox1.InvokeIfRequired(c => { c.Text = "it works!"; });

but when I try to call it on ToolStripStatusLabel or ToolStripProgressBar I get an error:

The type 'System.Windows.Forms.ToolStripStatusLabel' cannot be used as type parameter 'T' in the generic type or method 'SimpleApp.Helpers.InvokeIfRequired(T, System.Action)'. There is no implicit reference conversion from 'System.Windows.Forms.ToolStripStatusLabel' to 'System.Windows.Forms.Control'.

I know that this is probably a simple fix, but I just can handle it :/

Bushido answered 14/9, 2012 at 6:30 Comment(1)
Maybe this similar question will help you. #1128473Huron
H
10

This is because ToolStripItem (base for those two causing an error) is a Component and not a Control. Try calling your extension method on the tool strip that owns them and adjust your delegate methods.

Highflown answered 14/9, 2012 at 6:49 Comment(5)
I fount just a second ago the same info on Google :) But how I should access ToolStripStatusLable from statusStrip? So I can pass statusStrip to my method?Bushido
msdn.microsoft.com/en-us/library/… MSDN is your friend :)Highflown
You're fast :) but how to access Items? only by index or can I do it by name? For example if I name ToolStripStatusLabel 'status' how can I access it in my delegate? only like statusStrip1.Items[0]? I'm asking because I will be adding controls to statusStrip dynamically and I can mess up index of my control.Bushido
msdn.microsoft.com/en-us/library/25b5ws77.aspx It was just 2 click away... please read documentationHighflown
Don't ya love this place? :)Snider
N
3

I'd like to add up to the accepted solution. You can get the control from the component by using the GetCurrentParent method of the ToolStripStatusLabel.

Instead of doing toolStripStatusLabel1.InvokeIfRequired, do toolStripStatusLabel1.GetCurrentParent().InvokeIfRequired

Nad answered 25/7, 2018 at 20:48 Comment(1)
Thanks for the answer. I have been working with Control, but after getting your answer, I just finished my work.Ceaseless
C
2

Extension method using GetCurrentParent().InvokeRequired

public static void ToolStripStatusInvokeAction<TControlType>(this TControlType control, Action<TControlType> del)
    where TControlType : ToolStripStatusLabel
    {
        if (control.GetCurrentParent().InvokeRequired)
            control.GetCurrentParent().Invoke(new Action(() => del(control)));
        else
            del(control);
    }

Calling the ToolStripStatusInvokeAction Extension:

toolStripAppStatus.ToolStripStatusInvokeAction(t =>
{ 
    t.Text= "it works!";
    t.ForeColor = Color.Red;
});
Ceaseless answered 10/10, 2019 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.