如何动态删除未选中的复选框 [英] How to remove unchecked checkbox dynamically

查看:87
本文介绍了如何动态删除未选中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我正在使用C#在框架工作3.5上制作一个win form应用程序。

我正在创建动态复选框,具体取决于访问表中的记录数。在此之后,我还能够获得动态创建的复选框的名称,文本和其他属性,并且我在窗体上显示动态创建的标签中所选checkboex的名称。

我的问题是如果我取消选中复选框,然后该复选框的名称也在标签上,我想从未选中复选框的表单中删除该动态标签。

表单设计示例

dCeck1 dCheck2 dCheck3 dCheck4 dCheck5(如果选择dCeck1 dCheck2 dCheck4 dCheck5)

dLabel1 dLabe2 dLabe4 dLable5(将显示这些标签)

现在,如果我取消选择dCheck4,则应删除dLabel4 。



如何实现这一目标,这是我正在尝试的代码 -



  private   void  RO_SelectedIndexChanged( object  sender,EventArgs e)
{
groupBox1.Controls.Clear();
String m = RO.SelectedItem.ToString();

Console.WriteLine(m);

aCommand2 = new OleDbCommand( select * from branch_tbl,region_tbl其中branch_tbl.region_id = region_tbl.region_id和region_tbl.region_name LIKE' + m + ',main_connection);
aAdapter2 = new OleDbDataAdapter(aCommand2);
ds2 = new DataSet();
aAdapter2.Fill(ds2, app_info);
ds2.Tables [ 0 ]。Constraints.Add( pk_bno,ds2.Tables [ 0 ]。列[ 0 ] , true );

int bran_count = ds2.Tables [ 0 ]。Rows.Count;
Console.WriteLine(bran_count);

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);
}

}
int count = 1 ;
int position = 1 ;
private void CheckBoxCheckedChanged( object sender,EventArgs e)
{
CheckBox c =(CheckBox)sender;
标签myLabel;
if (c.Checked == true
{
计数++;
myLabel = new Label();
myLabel.Name = label + count.ToString();
myLabel.Text = c.Text;
myLabel.Location = new Point( 20 ,65 * position);
this .Controls.Add(myLabel);
position ++;
}
其他
{
return ;
}
if (c.CheckState == CheckState.Unchecked)
{
.Controls.Remove(myLabel);
this .Update();
}
}



提前谢谢每个人

解决方案

< BLOCKQUOTE> 1。存储一系列标签,就像你使用CheckBoxes数组一样,并在创建复选框的同时创建标签



 label =  new 标签[bran_count]; 

for int i = 0 ; i < bran_count; ++ i)
{
label [i] = new Label();
....
groupBox1.Controls.Add(checkBox [i]);
groupBox1.Controls.Add(label [i]);
.......
}





2.在您的事件处理程序中隐藏与之关联的标签未选中的复选框

  int  label_index =  0  ; 
for int i = 0 ; i < checkbox.Length; i ++)
{
if (checkbox [i] .Name ==((CheckBox)sender).Name)
{
label_index = i;
break ;
}
}

// 添加逻辑以检查是否复选框是否检查
// 而不是删除标签只是隐藏它
label [label_index] .Visible = false ;


Hello

I am making a win form application on frame work 3.5 using C#.
I am creating dynamic checkboxes depending on the numbers of records in access table. After this I am also able to get the name, text and other properties of dynamically created checkboxes and I am displaying the name of selected checkboexs in dynamically created labels on Form.
My problem is if I uncheck the checkbox, then also name of that checkbox is coming on label, I want to remove that dynamic label from the form for which checkbox is unchecked.
Example of Form Design
dCeck1 dCheck2 dCheck3 dCheck4 dCheck5 (if dCeck1 dCheck2 dCheck4 dCheck5 is selected)
dLabel1 dLabe2 dLabe4 dLable5 (these label will be displayed)
Now if I unselect dCheck4 then dLabel4 should be removed.

How can I achieve this, here is code which I am trying-

private void RO_SelectedIndexChanged(object sender, EventArgs e)
{
    groupBox1.Controls.Clear();
    String m = RO.SelectedItem.ToString();

    Console.WriteLine(m);

    aCommand2 = new OleDbCommand("select * from branch_tbl,region_tbl where branch_tbl.region_id=region_tbl.region_id and region_tbl.region_name LIKE '"+m +"'", main_connection);
    aAdapter2 = new OleDbDataAdapter(aCommand2);
    ds2 = new DataSet();
    aAdapter2.Fill(ds2, "app_info");
    ds2.Tables[0].Constraints.Add("pk_bno", ds2.Tables[0].Columns[0], true);

    int bran_count = ds2.Tables[0].Rows.Count;
    Console.WriteLine(bran_count);

    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);
    }

}
int count = 1;
int position = 1;
private void CheckBoxCheckedChanged(object sender, EventArgs e)
{
    CheckBox c = (CheckBox)sender;
    Label myLabel;
    if (c.Checked == true)
    {
        count++;
        myLabel = new Label();
        myLabel.Name="label" + count.ToString();
        myLabel.Text = c.Text;
        myLabel.Location = new Point(20,65*position);
        this.Controls.Add(myLabel);
        position++;
    }
    else
    {
        return;
    }
    if(c.CheckState == CheckState.Unchecked)
    {
        this.Controls.Remove(myLabel);
        this.Update();
    }
}


Thank you every body in advance

解决方案

1. store an array of labels, just like you do with an array of CheckBoxes and create the labels at the same time you create checkboxes

label= new Label[bran_count];

for (int i = 0; i < bran_count; ++i)
{
    label[i] = new Label();
    ....     
    groupBox1.Controls.Add(checkBox[i]);
    groupBox1.Controls.Add(label[i]);
    .......          
}



2. In your event handler hide the label associated with unchecked checkbox

int label_index = 0;
for(int i = 0; i < checkbox.Length; i++)
{
    if(checkbox[i].Name == ((CheckBox)sender).Name)
    {
         label_index = i;
         break;
    }
}

//add logic to check if checkbox is checked or not
//instead of removing the label just hide it
label[label_index].Visible = false;


这篇关于如何动态删除未选中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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