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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Creating an unknown number of checkboxes

Creating an unknown number of checkboxes

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 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.
  • B Offline
    B Offline
    b_girl
    wrote on last edited by
    #1

    I need to be able to create some number of checkboxes, this number depends on the number of items that the user previously selected. What I have done is created a loop for the number of selected items, and in the loop I: for(x = 0; x < numberSelected; x++) { CButton button; CRect rect; CString buttonText; GetClientRect(rect); ClientToScreen(rect); buttonText.Format("%d", x); button.Create(buttonText, WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX, rect , this, x); } UpdateData(false); Now, I know that *if* this were to work, they would all show up in the same spot because the rect structure never changes, but I just wanted to make sure it would work in a loop like this before I went too far into it. But, this doesn't work anyway, can anyone help me out? Thanks.

    B T D 3 Replies Last reply
    0
    • B b_girl

      I need to be able to create some number of checkboxes, this number depends on the number of items that the user previously selected. What I have done is created a loop for the number of selected items, and in the loop I: for(x = 0; x < numberSelected; x++) { CButton button; CRect rect; CString buttonText; GetClientRect(rect); ClientToScreen(rect); buttonText.Format("%d", x); button.Create(buttonText, WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX, rect , this, x); } UpdateData(false); Now, I know that *if* this were to work, they would all show up in the same spot because the rect structure never changes, but I just wanted to make sure it would work in a loop like this before I went too far into it. But, this doesn't work anyway, can anyone help me out? Thanks.

      B Offline
      B Offline
      b_girl
      wrote on last edited by
      #2

      one more thing, that for loop is inside my OnInitDialog function.

      1 Reply Last reply
      0
      • B b_girl

        I need to be able to create some number of checkboxes, this number depends on the number of items that the user previously selected. What I have done is created a loop for the number of selected items, and in the loop I: for(x = 0; x < numberSelected; x++) { CButton button; CRect rect; CString buttonText; GetClientRect(rect); ClientToScreen(rect); buttonText.Format("%d", x); button.Create(buttonText, WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX, rect , this, x); } UpdateData(false); Now, I know that *if* this were to work, they would all show up in the same spot because the rect structure never changes, but I just wanted to make sure it would work in a loop like this before I went too far into it. But, this doesn't work anyway, can anyone help me out? Thanks.

        T Offline
        T Offline
        Terry ONolley
        wrote on last edited by
        #3

        One thing that might cause problems is that you are defining a single CButton object - and then trying to call it's Create() member over and over again. You might want to create an array of buttons as a member variable for that dialog box (if you know the maximum number you might possibly have - say 117 if you were doing chemical elements) and then make visible the ones you need visible and use MoveWindow() to position them.


        Have you answered an MTQ? Check out the stats!


        1 Reply Last reply
        0
        • B b_girl

          I need to be able to create some number of checkboxes, this number depends on the number of items that the user previously selected. What I have done is created a loop for the number of selected items, and in the loop I: for(x = 0; x < numberSelected; x++) { CButton button; CRect rect; CString buttonText; GetClientRect(rect); ClientToScreen(rect); buttonText.Format("%d", x); button.Create(buttonText, WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX, rect , this, x); } UpdateData(false); Now, I know that *if* this were to work, they would all show up in the same spot because the rect structure never changes, but I just wanted to make sure it would work in a loop like this before I went too far into it. But, this doesn't work anyway, can anyone help me out? Thanks.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Once the for loop goes out of scope, the CButton object ceases to exist. Try this:

          CButton *pButton;
          for (...)
          {
          pButton = new CButton;
          pButton->Create(...);
          }


          Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

          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