T
T
tex6202017-10-30 09:42:46
C++ / C#
tex620, 2017-10-30 09:42:46

Calling a Windows Forms function outside of the Forms class?

There is a need to display text on the form from the program flow, located in another class, but as far as I know, the form functions are available only from the form class, how to get around this, please tell me.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
Griboks, 2017-10-30
@Griboks

Invoke

P
Peter, 2017-10-30
@petermzg

After all, this is your class inherited from Form, in it you can write any of your methods and set them as public.
It remains only to pass to the class you are interested in a pointer to an instance of the form class.

C
cicatrix, 2017-10-30
@cicatrix

Here your problem is described in detail.
In short: to call a form method from another thread, you need to call it through the Invoke mechanism. Here's an example from there:

public static void InvokeIfRequired(this ISynchronizeInvoke obj,
                                         MethodInvoker action)
{
    if (obj.InvokeRequired) {
        var args = new object[0];
        obj.Invoke(action, args);
    } else {
        action();
    }
}

where action() is your method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question