CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2009
    Posts
    11

    multiple buttons using the same OnBnClicked

    I've got many (many) buttons on screen and I'd like to write just one OnClick event for all of them.
    The question is for my Onclick function, is it possible to tell which button has called the onclick ?

    I'd like to achieve the following:

    void CTestDlg::OnBnClicked()
    {
    int i = **Determine which button was clicked, numbered one to 32**

    ProcessButton(i);
    }

    Before anyone comments on "why have I got 32 buttons on screen" its because its realtime, and yes it does look awful and the client has asked for it to look so bad specifically :-)

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: multiple buttons using the same OnBnClicked

    Quote Originally Posted by MJBritton25 View Post
    Before anyone comments on "why have I got 32 buttons on screen" its because its realtime, and yes it does look awful and the client has asked for it to look so bad specifically :-)
    LOL. Check out the ON_COMMAND_RANGE macro DDX entry.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: multiple buttons using the same OnBnClicked

    I don't know of a way to determine which button was clicked the way you're asking. You could

    Set up a separate event handler for each button that would call a common function and pass in the button ID

    You could override PreTranslateMessage and intercept the button clicks there.

    I would assume the last button clicked would have the focus. You could check for the button with the focus to determine which one was clicked.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: multiple buttons using the same OnBnClicked

    Sorry, I said ON_COMMAND_RANGE, it's ON_CONTROL_RANGE.

    Code:
     
    ON_CONTROL_RANGE(BN_CLICKED, IDC_BUTTON1, IDC_BUTTON10, OnButtonClicked)
    Code:
     
    void CMyDialog::OnButtonClicked( UINT nID )
    {
        int nButton = nID - IDC_BUTTON1;
        ASSERT( nButton >= 0 && nButton < 10 );
        // ...
    }

  5. #5
    Join Date
    Aug 2009
    Posts
    11

    Re: multiple buttons using the same OnBnClicked

    works great - thanks very much

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: multiple buttons using the same OnBnClicked

    Quote Originally Posted by MJBritton25 View Post
    works great - thanks very much
    Your welcome. Sure makes the code cleaner, doesn't it?

  7. #7
    Join Date
    Feb 2003
    Location
    Germany
    Posts
    89

    Re: multiple buttons using the same OnBnClicked

    and make sure that the ID's are consecutive - sometimes they tend not too, esp. when generated by the resource wizard.
    prepare that the light at the end of your tunnel is just a freight train coming your way - metallica

Tags for this Thread

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