动态将复选框添加到面板 [英] Dynamically add check boxes to panel

查看:110
本文介绍了动态将复选框添加到面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有面板的Windows窗体,我也想添加复选框.但是,当表单开始时,它不知道它将需要多少个复选框或它们的文本是什么.

我在另一个类中有一个函数,该函数返回字符串数组.数组中的字符串数将是我需要的复选框数.添加新复选框后,它直接位于其前一个复选框的下方(第一个复选框位于面板的左上方).

所以它看起来应该像这样:

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
}


我不太确定如何实际执行此操作,因此任何建议都是不错的选择.

我的另一个想法是在窗体加载时隐藏大约15个复选框(永远不会超过15个复选框),并在返回数组时显示尽可能多的复选框,但我宁愿按照自己的方式进行操作如上所述.

这是在Visual C ++ 2008 Express Edition中.


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!

推荐答案

复选框只是具有某些按钮样式的CButton,要动态创建,只需执行以下操作:
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
}



按钮样式:
http://msdn.microsoft.com/en-us/library/tf9hd91s% 28VS.80%29.aspx [ ^ ]

CButton :: Create()说明:
http://msdn.microsoft.com/en-us/library/bw4e0cww%28v = vs.80%29.aspx [ ^ ]



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[^]


我之前开发过类似的东西
可以做到的
I developed thing like this before
it can be done
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 

}


这篇关于动态将复选框添加到面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆