Thread: Message handling for an array of buttons

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    13

    Message handling for an array of buttons

    I'm sure this has a trivial answer ...

    I have a set of buttons in a program that have related functions.

    Working code:
    Code:
    #define IDC_BUTTON_S1 1501
    #define IDC_BUTTON_S2 1502
    #define IDC_BUTTON_S3 1503
    ...
    #define IDC_BUTTON_S10 1510
    
    
    
    ... elsewhere in program ...
    
    case WM_COMMAND:
    {
      switch(LOWORD(wParam)) {
        case IDC_BUTTON_S1:
        {
          if( HIWORD(wParam) == BN_CLICKED ) {
            // do stuff
            return 0;
          }
          break;
        }
        case IDC_BUTTON_S2:
        {
          if( HIWORD(wParam) == BN_CLICKED ) {
            // do stuff
            return 0;
          }
          break;
        }
    
    ...

    For a few reasons, having ten separate IDC_BUTTON_S# entries is a pain, not the least of which is an inability to address them neatly in loops or otherwise by their #.

    So what I want, and what I attempted, was this:

    Code:
    const int IDC_BUTTON_S[10] = { 1501, 1502, 1503, 1504, 1505, 1506, 1507, 1508, 1509, 1510 };
    
    ...
    
    case WM_COMMAND:
    {
      switch(LOWORD(wParam)) {
        case IDC_BUTTON_S[0]:
        {
          if( HIWORD(wParam) == BN_CLICKED ) {
            // do stuff
            return 0;
          }
          break;
        }
        case IDC_BUTTON_S[1]:
        {
          if( HIWORD(wParam) == BN_CLICKED ) {
            // do stuff
            return 0;
          }
          break;
        }
    
    ...

    But my compiler won't play that game. It complains:

    error: case label does not reduce to an integer constant


    I very much want to be able to do things like
    Code:
    for( i = 0; i < 10; i++ )
      EnableWindow(GetDlgItem(hWnd,IDC_BUTTON_S[i]),TRUE);
    or change the bitmap on one or more buttons without resorting to ugly switch/case statements and having to explicitly address them as IDC_BUTTON_S1, IDC_BUTTON_S2 ...

    Should I be declaring my const int ICD_BUTTON_S[10] array differently?

    Thank you.

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    13
    I should add that I have been successful with keeping this portion as is
    Code:
    #define IDC_BUTTON_S1 1501
    #define IDC_BUTTON_S2 1502
    #define IDC_BUTTON_S3 1503
    ...
    #define IDC_BUTTON_S10 1510
     
     
     
    ... elsewhere in program ...
     
    case WM_COMMAND:
    {
      switch(LOWORD(wParam)) {
        case IDC_BUTTON_S1:
        {
          if( HIWORD(wParam) == BN_CLICKED ) {
            // do stuff
            return 0;
          }
          break;
        }
        case IDC_BUTTON_S2:
        {
          if( HIWORD(wParam) == BN_CLICKED ) {
            // do stuff
            return 0;
          }
          break;
        }
     
    ...
    but ALSO declaring the const array with the same 1501-1510 values and using that array in loops and other bits:
    Code:
    const int IDC_BUTTON_S[10] = { 1501, 1502, 1503, 1504, 1505, 1506, 1507, 1508, 1509, 1510 };
    
    ...
    for( i = 0; i < 10; i++ )
      EnableWindow(GetDlgItem(hWnd,IDC_BUTTON_S[i]),TRUE);


    This strikes me as ugly inelegant and maybe unsafe. I'd like to know the right way to do it.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It's not possible to accomplish this the way you're trying to. Realize that the poorly named "const" qualifier really just makes variables "read only" (not constant), and you'll understand why the technique you're trying to implement isn't working.

    In your last example, you can make it a bit safer by defining the array as such:

    Code:
    const int IDC_BUTTON_S[10] = {
        IDC_BUTTON_S1,IDC_BUTTON_S2,IDC_BUTTON_S3,IDC_BUTTON_S4,IDC_BUTTON_S5,
        IDC_BUTTON_S6,IDC_BUTTON_S7,IDC_BUTTON_S8,IDC_BUTTON_S9,IDC_BUTTON_S10
        };
    However, you should be able to get around needing an array for looping purposes simply by thinking cleverly:

    Code:
    #define IDC_BUTTON_S1 1501
    #define IDC_BUTTON_S2 1502
    #define IDC_BUTTON_S3 1503
    ...
    #define IDC_BUTTON_S10 1510
    
    // ...
    
    for( i = IDC_BUTTON_S1; i <= IDC_BUTTON_10; i++ )
      EnableWindow(GetDlgItem(hWnd,i),TRUE);

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Something like...


    Code:
    case WM_COMMAND:
    {
       int iButtonClickedID = LOWORD(wParam);
      switch(iButtonClickedID) {
        case IDC_BUTTON_S1:
        case IDC_BUTTON_S2:
        case IDC_BUTTON_S3:
        case IDC_BUTTON_S4:
        //etc
        case IDC_BUTTON_S10:
        {
          if( HIWORD(wParam) == BN_CLICKED ) {
            // do stuff
            return ProcessButtonClick(iButtonClickedID);//pass in the button ID or button HWND to your function that handles the button clicks
          }
          break;
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    13
    Thanks, very helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Handling controls (buttons, edit boxes) width WndProc on a custom class.
    By Antony Kossoski in forum Windows Programming
    Replies: 0
    Last Post: 04-13-2012, 09:10 PM
  2. Design questions on prioriterized message handling
    By yixings in forum C Programming
    Replies: 1
    Last Post: 03-09-2012, 07:29 PM
  3. Problem in message handling VC++
    By 02mca31 in forum Windows Programming
    Replies: 5
    Last Post: 01-16-2009, 09:22 PM
  4. Message Handling
    By sethjackson in forum Windows Programming
    Replies: 14
    Last Post: 07-11-2006, 01:01 PM
  5. Control message handling
    By Homunculus in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2006, 05:56 PM