Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Any Control with huge buttons.

Any Control with huge buttons.

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    phanindra varma
    wrote on last edited by
    #1

    Hi,, In my application I have to use large number of buttons(AROUND 112), all buttons will call the same function " DoMyDuty(int a) ", variable 'a' is different for all buttons. here is sample code for better understand void MyClass::OnB1() { int a; a=1; -----Only this value changed DoMyDuty(a); } void MyClass::OnB2() { int a; a=15; -------Only this value changed DoMyDuty(a); } ..... . . . . ..... void MyClass::OnB112() { int a; a=30; -------Only this value changed DoMyDuty(a); } The CODE SEEMS TO BIG but simple to do... iS THERE ANY ALTERNATIVE LOGIC TO DO THIS......?????? Any other control behaves like this???? thank u. verma

    G 1 Reply Last reply
    0
    • P phanindra varma

      Hi,, In my application I have to use large number of buttons(AROUND 112), all buttons will call the same function " DoMyDuty(int a) ", variable 'a' is different for all buttons. here is sample code for better understand void MyClass::OnB1() { int a; a=1; -----Only this value changed DoMyDuty(a); } void MyClass::OnB2() { int a; a=15; -------Only this value changed DoMyDuty(a); } ..... . . . . ..... void MyClass::OnB112() { int a; a=30; -------Only this value changed DoMyDuty(a); } The CODE SEEMS TO BIG but simple to do... iS THERE ANY ALTERNATIVE LOGIC TO DO THIS......?????? Any other control behaves like this???? thank u. verma

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      If you are using MFC, you can have a single handler respond to a group of controls using ON_CONTROL_RANGE()[^]. This would let you have a single handler, like this:

      // in your message map:
      ON_CONTROL_RANGE(BN_CLICKED,IDC_B1,IDC_B112,OnB)
      // handler:
      void MyClass::OnB(UINT ID)
      {
      static struct {
      UINT ID;
      int a;
      } table[] = {
      { IDC_B1, 1 },
      { IDC_B2, 15 },
      //...
      { IDC_B112, 30 }
      };
      for (int i = 0; i < (sizeof(table) / sizeof(table[0])); i++) {
      if (table[i].ID == ID) {
      DoMyDuty(table[i].a);
      break;
      }
      }
      }

      The handler is called with the resource ID of the button that was clicked. It then uses a simple table to look up the value of 'a' to use for the call to the DoMyDuty() function.

      Software Zen: delete this;
      Fold With Us![^]

      K 1 Reply Last reply
      0
      • G Gary R Wheeler

        If you are using MFC, you can have a single handler respond to a group of controls using ON_CONTROL_RANGE()[^]. This would let you have a single handler, like this:

        // in your message map:
        ON_CONTROL_RANGE(BN_CLICKED,IDC_B1,IDC_B112,OnB)
        // handler:
        void MyClass::OnB(UINT ID)
        {
        static struct {
        UINT ID;
        int a;
        } table[] = {
        { IDC_B1, 1 },
        { IDC_B2, 15 },
        //...
        { IDC_B112, 30 }
        };
        for (int i = 0; i < (sizeof(table) / sizeof(table[0])); i++) {
        if (table[i].ID == ID) {
        DoMyDuty(table[i].a);
        break;
        }
        }
        }

        The handler is called with the resource ID of the button that was clicked. It then uses a simple table to look up the value of 'a' to use for the call to the DoMyDuty() function.

        Software Zen: delete this;
        Fold With Us![^]

        K Offline
        K Offline
        krmed
        wrote on last edited by
        #3

        Another similar approach:

        // in your message map:
        ON_CONTROL_RANGE(BN_CLICKED,IDC_B1,IDC_B112,OnB)

        // handler:
        void MyClass::OnB(UINT ID)
        {
        DoMyDuty(ID - IDC_B1 + 1);
        }

        Hope that helps.

        Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

        G 1 Reply Last reply
        0
        • K krmed

          Another similar approach:

          // in your message map:
          ON_CONTROL_RANGE(BN_CLICKED,IDC_B1,IDC_B112,OnB)

          // handler:
          void MyClass::OnB(UINT ID)
          {
          DoMyDuty(ID - IDC_B1 + 1);
          }

          Hope that helps.

          Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

          G Offline
          G Offline
          Gary R Wheeler
          wrote on last edited by
          #4

          I don't think that works. The value of 'a' in his original post didn't vary predictably according to the button pressed, so you needed a lookup table approach.

          Software Zen: delete this;
          Fold With Us![^]

          P 1 Reply Last reply
          0
          • G Gary R Wheeler

            I don't think that works. The value of 'a' in his original post didn't vary predictably according to the button pressed, so you needed a lookup table approach.

            Software Zen: delete this;
            Fold With Us![^]

            P Offline
            P Offline
            phanindra varma
            wrote on last edited by
            #5

            Thanks gary!!!!! It works fine.....

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups