如何知道动态创建的chekboxs是否被选中? [英] How to know dynamically created chekboxes is selected or not?

查看:90
本文介绍了如何知道动态创建的chekboxs是否被选中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我正在尝试生成动态标签,具体取决于动态创建的选中复选框。



我的.Net框架是3.5

这里是动态复选框的代码

 System.Windows.Forms.CheckBox [] checkBox =  new  System.Windows.Forms.CheckBox [bran_count]; 

for int i = 0 ; i < bran_count; ++ i)
{
checkBox [i] = new CheckBox();
checkBox [i] .Name = radio + Convert.ToString(i);
checkBox [i] .Text = ds2.Tables [ 0 ]。行[i] [ 2 ]的ToString();

checkBox [i] .Location = new System.Drawing.Point( 125 * i, 15 );
groupBox1.Controls.Add(checkBox [i]);
checkBox [i] .CheckStateChanged + = new System.EventHandler(CheckBoxCheckedChanged);

}
}
私有 void CheckBoxCheckedChanged( object sender,EventArgs e)
{
// MessageBox.Show();
}





欢迎任何指导,如果可能的话给出一些代码片段。

问候

解决方案

无论点击哪个复选框,都会触发,但会告诉您如何找出它是哪一个是

 私人  void  CheckBoxCheckedChanged(< span class =code-keyword> object  sender,EventArgs e)
{
CheckBox c =(CheckBox)sender;
MessageBox.Show(c.Name);
}



...并且只会触发刚检查的复选框


< blockquote>

  for  int  i =  0 ; i <  checkBox.length; ++ i)
{
if (checkBox [i] .ischecked == true
{
// MessageBox.Show();
}
}


Hi All

I am trying to generated the dynamic labels depending on the checked checkboxes which are dynamically created.

My .Net frame is 3.5
Here is code for dynamic checkboxes

System.Windows.Forms.CheckBox[] checkBox = new System.Windows.Forms.CheckBox[bran_count];

            for (int i = 0; i < bran_count; ++i)
            {
                checkBox[i] = new CheckBox();
                checkBox[i].Name = "radio" + Convert.ToString(i);
                checkBox[i].Text = ds2.Tables[0].Rows[i][2].ToString();
                
                checkBox[i].Location = new System.Drawing.Point(125 * i, 15);
                groupBox1.Controls.Add(checkBox[i]);
                checkBox[i].CheckStateChanged += new System.EventHandler(CheckBoxCheckedChanged);
                
            }
        }
        private void CheckBoxCheckedChanged(object sender, EventArgs e)
        {
            //MessageBox.Show();
        }



Any guidance is welcome, if possible give some code snippet.
Regards

解决方案

This will fire regardless of which checkbox is clicked but shows you how to work out which one it was

private void CheckBoxCheckedChanged(object sender, EventArgs e)
{
    CheckBox c = (CheckBox)sender;
    MessageBox.Show(c.Name);
}


... and will only fire for the checkbox that was just checked


for (int i = 0; i < checkBox.length; ++i)
{
if( checkBox[i].ischecked==true)
{
//MessageBox.Show();
}
}


这篇关于如何知道动态创建的chekboxs是否被选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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