CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2012
    Posts
    16

    Question array of buttons... click event ?

    Hi I have successfully compiled a code where I created an array of buttons.

    I have added a click event to it like this :

    Code:
    buttons[indexofhashis2]->Click += gcnew System::EventHandler(this, &Execute::GOBUT_Click);
    I have a standard event like this
    Code:
    private: System::Void GOBUT_Click(System::Object^  sender, System::EventArgs^  e) {
    			int x; 
    x = sender->Location.X;
    
    			 }
    returns error saying Location is not a member of Object^.

    If you don't understand what i want to do :
    I'm trying to create an array of button (user/runtime created buttons) and get different interpretations of the click event depending of wich button is clicked.

    For example, if we create buttons[0] I wish to get the 0 or something relevant into the GOBUT_Click event.

    Any one knows how to do that?

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: array of buttons... click event ?

    Bad forum. You need to switch to Managed C++.
    Best regards,
    Igor

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: array of buttons... click event ?

    Igor is right about that you posted this in the wrong place. However, the anser is short, so I'll post it here right away:

    Quote Originally Posted by FenixEden View Post
    [...] returns error saying Location is not a member of Object^.
    Correct: Is isn't. You need to cast the parameter to Control ^ (or a more specific class like Button ^) in order to access control members:

    Code:
    x = safe_cast<Control ^>(sender)->Location.X;
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  4. #4
    Join Date
    Sep 2012
    Posts
    16

    Re: array of buttons... click event ?

    I think you're right for Managed C++,
    however this forum is mostly empty, I think I would not get an answer there.

    And so...

    Okay!!!
    I need to associate the click on those buttons arrays to the correct interpretation in the GO_BUT::Click event.

    So how I do that?


    Is it like this or something like that?
    Code:
    private: System::Void GOBUT_Click(System::Object^  sender, System::EventArgs^  e) {
    int ID; 
    Button^ IDBut;
    IDBut = sender;
    EDIT : Returns conversion error. Object^ to Button^
    How can I associate the right index of my button array to it?
    Last edited by FenixEden; October 15th, 2012 at 10:17 AM. Reason: code doesn't work

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: array of buttons... click event ?

    Quote Originally Posted by FenixEden View Post
    I think you're right for Managed C++,
    however this forum is mostly empty, I think I would not get an answer there.
    Sure you will. Trust me...

    EDIT : Returns conversion error. Object^ to Button^
    How can I associate the right index of my button array to it?
    Not really surprising: You're trying to do a plain assignment instead of casting (see my example in post #3). In the context of your latest snippet, you need to change the offending line to this:

    Code:
    IDBut = safe_cast<Button ^>(sender);
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured