Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Windows Form with a panel on it that I would like to add check boxes too. However, when the form starts, it does not know how many check boxes it will need or what their text will be.

I have a function in another class that returns an array of Strings. The number of Strings in the array will be the number of check boxes I need. When a new check box is added it goes directly below the one before it (the first one just goes at the top left of the panel).

So it should look something like:

for(int i = 0; i < array.length; i++)
{
   // new checkbox with id checkBox + i.ToString()
   // checkBoxi.text = array[i]
   // add checkBoxi to panel below the one before it
}

I'm not really sure how to actually implement this, so any advice would be great.

My other idea is just to hide about 15 checkboxes (there will never be more than 15) when the form loads, and show as many as necessary when the array is returned, but I'd rather do it the way I've described above.

This is in Visual C++ 2008 Express Edition.

Thanks in advance!
Posted
Updated 1-Jul-11 10:37am
v2
Comments
Philippe Mori 1-Jul-11 16:30pm    
For a small number of checkboxes, hiding them would be the easier way to do it (assuming you are using MFC or the Windows API). Typically, you could uses consecutive IDs so a simple loop would allows to show or hide checkboxes.

In the text above you are talking of Windows Forms and a panel. Are you using WinForms or MFC or the Windows API. In Winforms, you could uses CheckedListBox in that case. By the way, it is much simpler to dynamicaly add controls in WinForms as a flow layout panel could be used.

A checkbox is just a CButton with certain button styles, to create dynamically, just do this:
//pseudo-code
//Initialize container, could be linked list, or CArray, or array, or vector to contain all of the CButton pointers
for(...){
 CheckboxContainer[i] = new CButton;
 CheckboxContainer[i]->Create("Text", BS_CHECKBOX | WS_VISIBLE | BS_TEXT, 
   rect, parentWnd, nID); //Just remember to keep track of the location and update after each iteration to space out the containing rects, for the IDs, you can allocate a block of resource IDs
}


Button styles:
http://msdn.microsoft.com/en-us/library/tf9hd91s%28VS.80%29.aspx[^]

CButton::Create() description:
http://msdn.microsoft.com/en-us/library/bw4e0cww%28v=vs.80%29.aspx[^]
 
Share this answer
 
Comments
obp42 1-Jul-11 16:36pm    
Sorry I should have mentioned that I'm using the express edition, which I don't think will let me use any of the CWnd classes. Any other ideas?
Albert Holguin 1-Jul-11 18:25pm    
If you don't want to use MFC you can still make your own windows with the WinAPI... just a bit more work intensive...
obp42 1-Jul-11 20:44pm    
Thanks Albert, I won't have a chance to work on this again until Tuesday but I'm sure I'll be able to figure it out with the information you've provided.
I developed thing like this before
it can be done
C#
static unsigned int idc_CehckBoxButtonStart,idc_CehckBoxButtonEnd; // to remember the start and end of the ids of your button arrray.
switch(msg)
{
case WM_CREATE:
  ...
 Array=GetTheArray(); //your function that will give you the text array list
 idc_CehckBoxButtonStart=IDC_CHECKBOX_START_NUMBER;
 idc_CehckBoxButtonEnd=idc_CehckBoxButtonStart;
 for(i=0;i<arraycount;i++)>
 {
      CreateWindowEx(0,L"BUTTON",Array.cText,WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_RADIOBUTTON|WS_GROUP,STATIC_WIDTH+10,top,115,ALL_HEIGHT,hWnd,(HMENU)idc_CehckBoxButtonEnd,ghInstance,NULL); //it is for radiobutton
  idc_CehckBoxButtonEnd++;
 }
break;
case WM_COMMAND:
{
wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
   if(wmId>=idc_CehckBoxButtonStart && wmId<idc_cehckboxbuttonend)>
   {
     //do your magic.
     break;
   }
   //rest of command process 

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900