从动态创建的复选框获取文本值 [英] Getting Text Value from a dynamically created checkbox

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

问题描述

大家好,


这是我的代码:

Hello guys,


Here is my code:

CheckBox[] cb = new CheckBox[100];
for (int i = 1; r2.Read(); i++)
            {
                int height = 1;
                    cb[i] = new CheckBox();
                    cb[i].Name = i.ToString();
                    cb[i].Text = r2[1].ToString();
                    cb[i].Location = new Point(30, i * 30);
                    this.Controls.Add(cb[i]);
                    height += 22;
            }



我也有一个按钮事件,当我单击该按钮时,我想获取属于所选复选框的文本数据.我真的需要你的帮助.谢谢

〜Cansin



I also have a button event, when i clicked that button, i want to get the text data which is belong to that selected checkbox. I really need your help. thank you

~Cansin

推荐答案

有一些问题:

1.数组索引从0开始,而不是1

2.您不检查数组是否溢出,如果for循环超过100次迭代该怎么办?

3.高度用作什么?它在for块中是本地的,不用于任何用途

4.定义选择"?您可以使复选框处于焦点状态,但是当您单击按钮时,该复选框将失去焦点.因此,可以这么说,那时没有选定"复选框.
There are a few issues there:

1. The array index starts at 0, not 1

2. You don''t check for array overflow, what if the for loop crosses 100 iterations?

3. What''s height used for? It''s local to the for block and not used for anything

4. Define "selected"? You can have a checkbox in focus, but the moment you click on the button the checkbox loses focus. So at that time there is no "selected" checkbox, so to speak.


为什么不使用Listbox代替呢?您可以打开复选框,选择一个项目时,只需获取该项目的文本即可.除此之外,它看起来比在窗体上具有100个单独的列表框要好得多.
Why don''t you use a Listbox instead? You can turn on checkboxes, and when you select an item, just get the item''s text. Beyond that, it will look a lot better than having 100 individual listboxes on a form.


另一种方法是使用CheckedListBox:

http://msdn.microsoft.com/en-us/library/system. windows.forms.checkedlistbox.aspx [ ^ ]
An alternative approach would be to use a CheckedListBox:

http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.aspx[^]


这篇关于从动态创建的复选框获取文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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