Search code examples
event-handlingcursorc++-climouseeventclr

Returning a value from a panel in C++?


Let's say I've the following:

private: System::Int64 panel1_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)
{
    int x; 
    mouse event gives back the value 5.
    x = 5; 
    return x
}

How do i call the panel such that it can return 5?? Is there any way possible to do it? Thanks.

P.S: I'm using C++/CLI.


Solution

  • I never used, System::Windows and the like, and I think your actual problem is some misunderstanding of how to handle the events. However...

    "How do i call the panel such that it can return 5??"

    From your question I assume that this function is inline in the header of some MyPanel header. I.e. something like:

    class MyPanel{
        // ... //
        private: 
            System::Int64 panel1_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)
            {
                int x; 
                //mouse event gives back the value 5.
                x = 5; 
                return x
            }
    };
    

    This function does return 5 when it is called. However, it is private and can only be called from member functions (or friends).

    EDIT: After reading more comments, I think I know what is your problem.. The correct return type of this function is "void" not "int". If you want to process the event further, do it in another function. For example:

    void myEventHandler(int x){
        std::cout << "Button " << x << " was pressed" << std::endl;
    }
    void panel1_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)
    {
        myEventHandler(5);
    }
    

    MouseDown is the function that is called when the mouse is hold down. You dont call it, you just handle your event in it. Hope this helps a bit...