Calls to Form from outside

Hi,

Could someone please tell me how I would go about setting a value in a windows form from the "outside".

For example, if I want to use some function that is outside of the form, do some processing, and then display the result in the form. I do not want to call the function within the form code.

Many thanks in advance!

Ljubica



Answer this question

Calls to Form from outside

  • infoworx

    Hi,
    if you want to set a property in the form you can do something like this in the form:

    public: property bool Running
    {
    bool get() { return m_running; }
    void set(bool value) { m_running = value; }
    }

    and then in your other function do:

    form->Running = true;

    if you want to call some function you have some ways of doing this:
    - you can declare a static method (remember that if you do this and you want to access attributes/objects in your class, you'll have to declare them static as well.) In this case you don't need a reference to your form:

    ref class MyForm : public Form
    {
    public: static void Test()
    {
    // do something
    }
    }

    // and to call the method:
    MyForm::Test();

    - you can declare a public method and call it from anywhere if you have a reference to that form:

    ref class MyForm : public Form
    {
    public: void Test()
    {
    // do something
    }
    }

    // and to call the method:
    form->Test();

    Hope this answers your question.


  • Koalakk

    Looks like an idea! I'll give it a punt.

    On a quick note, what I'm gunning at is to change the picturebox picture outside of the form, I assume the above will work for it, right


  • Planky

    Yes, the best way for you to change it is to make a public method in the form and have a reference to that form (where you want to call the method), then all you have to do is call that method with any parameters you may need to pass:

    ref class MyForm
    {
    public: void ChangePictureBox(String imageStr)
    {
    // do your stuff
    }
    };

    then when you want to call it:
    form->ChangePictureBox("myimage.jpg");

    This should work.


  • Calls to Form from outside