Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ordinarily, when I want to change the image displayed by a pictureBox from within a Windows form, I fire an event like this:

C++
private: System::Void PicBoxMonton1_DoubleClick(System::Object^  sender, System::EventArgs^  e)
{
    if (this->PicBoxMonton1->Image != Image::FromFile(Glo::m_Empty))
    {
        this->PicBoxMonton1->Image = Image::FromFile(Glo::m_Empty);
        ....
        ....
    }
}

This syntax (this->PicBoxMonton1->Image) doesn't work when I try to execute the if statement from within a function written in a separate .cpp file where all my function are.
I am a beginner.
Which parameters do I need to pass to the function?
How do I change the instruction within the if statement?

What I have tried:

Everything within my limited experience. And I haven't found a single example to illustrate me.
Posted
Updated 4-Mar-17 6:45am
Comments
Richard MacCutchan 4-Mar-17 11:25am    
That is because the this keyword refers to the enclosing class. In this case the Form that contains the event handler. You need to get a reference to the Form in order to call that method.
Terence Moore 4-Mar-17 19:06pm    
Exactly. That has been my understanding all along. But how do I construct a reference to the Form? I have tried things like namespace::MyForm->pictureBox->Image but no luck. Thank you.
Richard MacCutchan 5-Mar-17 2:33am    
You don't construct it. When you create the form in the first place you need to save the reference somewhere.

1 solution

To solve the problem you should declare a public function which you call from the outside. For that you need a valid pointer to your form class instance.

Another issue is, that the call must be in the main thread of your app. Only if it is not in the main thread take a look at this answers at stackoverflow.
 
Share this answer
 
Comments
Terence Moore 4-Mar-17 19:00pm    
Thanks for the input. The function is already declared public.
How do I construct a valid pointer to my form class instance?.
THANK YOU.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900