CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 1999
    Posts
    21

    How do I put 30 buttons in a Dialogbox with all button presses handled by the same function

    I want to put 30 buttons on a Dialog box. I need to know how to create an array of CButtons so that I can access all the controls with the same member variable. I also need to be able to process button presses using the same function

    Similar to the way VB does it.

    Centurion Software Australia

  2. #2
    Guest

    Re: How do I put 30 buttons in a Dialogbox with all button presses handled by the same function

    Hi!
    1. Put in the 30 buttons on to your dialog box ensuring in the process that the IDs are contiguous.
    2. Add the following message map entry in your dialog
    ON_CONTROL_RANGE(BN_CLICKED,IDC_BUTTON1,IDC_BUTTON30,HandlerForBtnPress)
    3. Add the prototype and the implementation of the BN_CLICKED Handler
    //Prototype in the header file
    afx_msg void HandlerForBtnPress(int nID);
    //Sample implementation in the cpp file
    void CTrialDialog::HandlerForBtnPress(int nID)
    {
    int nButton = nID - IDC_BUTTON1;
    CString strMessage;
    strMessage.Format("Button %d Clicked !!!",nButton);
    AfxMessageBox(strMessage);
    }
    This ensures that the same function handles all button presses.

    Incase you need to create an array of CButtons, here is the code for it :
    //Header File
    const int nradio_buttons = 30; //Number of buttons
    //Class Definition
    CButton* m_ButtonArray[nradio_buttons];
    //OnInitDialog
    for(int i=0;i<30;i++)
    {
    m_ButtonArray[i] = (CButton*)GetDlgItem(IDC_BUTTON1+i);
    }
    //Make use of m_ButtonArray[i] for further processing as U need.

    Regards,
    Jose Wilson
    Software Engineer
    Aditi Technologies
    Blore - 80
    Email :[email protected]



  3. #3
    Guest

    Re: How do I put 30 buttons in a Dialogbox with all button presses handled by the same function

    Try the following
    ON_CONTROL_RANGE( BN_CLICKED,ID_BUTTON_01,ID_BUTTON30,ButtonCatcher )

    MyClass::ButtonCatcher()
    {
    // TODO: add logic (maybe a switch statement)
    }



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