访问c#中的动态创建的复选框 [英] Accessing dynamically created checkbox in c#

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

问题描述

我使用以下代码打开一个表单时,我将创建几个复选框:

  private void OpenFolder_Load EventArgs e)
{
int i = 0;
foreach(filesToOpen中的字符串文件)
{
标签lbl = new Label();
lbl.Text = Path.GetFileNameWithoutExtension(file);
lbl.Width = 200;
lbl.Height = 25;
lbl.AutoEllipsis = true;
lbl.Location = new System.Drawing.Point(10,40 + 25 * i);

this.Controls.Add(lbl);

string checkName =check+ i;
CheckBox check = new CheckBox();
check.Checked = true;
check.AccessibleName = checkName;
check.Location = new System.Drawing.Point(340,40 + 25 * i);
check.CheckedChanged + = new EventHandler(check_CheckedChanged);

this.Controls.Add(check);

CheckBoxes.Add(check);

i ++;
}

我试图检查复选框的状态我的OK按钮(只有当选中了一定数量的复选框时,用户才可以验证)



这里是我使用的代码,但它失败,以定位复选框:

  private void check_CheckedChanged(Object sender,EventArgs e)
{
for int i = 0; i< filesToOpen.Count(); i ++)
{
string tbarName =tbar+ i;
string checkName =check+ i;

CheckBox ckb = this.Controls.OfType< CheckBox>()
.Where(c => c.AccessibleName.Equals(checkName))作为CheckBox;
TrackBar tkb = this.Controls.OfType< TrackBar>()
.Where(t => t.AccessibleName.Equals(tbarName))作为TrackBar;
// TrackBar tkb = this.Controls.Find(tbarName,false).First()as TrackBar;
// CheckBox ckb = this.Controls.Find(checkName,false).First()as CheckBox;

if(ckb.Checked == true)
{
// do stuff
}
}
}

我做错了什么/真的错了吗?

方案

鉴于您将复选框添加到自己的列表中:

  CheckBoxes.Add 

这将是更简单的循环,而不是试图找到与文件相关的控制: / p>

  foreach(CheckBoxes中的var checkBox)
{
if(checkbox.Checked)
{
// Do stuff ...
}
}

但是,您不需要使用单独的列表。此行错误:

  CheckBox ckb = this.Controls.OfType< CheckBox>()
。 => c.AccessibleName.Equals(checkName))作为CheckBox;
返回 IEnumerable< CheckBox> 但是你试图将它直接转换为 CheckBox ,它将返回 null 。你应该有的是:

  CheckBox ckb = this.Controls.OfType< CheckBox>()
。 c => c.AccessibleName.Equals(checkName))。

您仍然需要检查 ckb 为null(只是在列表中没有任何内容),但这应该返回你正在寻找的控制。


I am creating a few checkboxes when I open a form with the following code:

    private void OpenFolder_Load(object sender, EventArgs e)
    {
        int i = 0;
        foreach (string file in filesToOpen)
        {
            Label lbl = new Label();
            lbl.Text = Path.GetFileNameWithoutExtension(file);
            lbl.Width = 200;
            lbl.Height = 25;
            lbl.AutoEllipsis = true;
            lbl.Location = new System.Drawing.Point(10, 40 + 25 * i);

            this.Controls.Add(lbl);

            string checkName = "check" + i;
            CheckBox check = new CheckBox();
            check.Checked = true;
            check.AccessibleName = checkName;
            check.Location = new System.Drawing.Point(340, 40 + 25 * i);
            check.CheckedChanged +=new EventHandler(check_CheckedChanged);

            this.Controls.Add(check);

            CheckBoxes.Add(check);

            i++;
        }

and I am trying to check the state of the checkboxes everytime one changes to toggle my OK button (the user can validate only if there are a certain number of the checkboxes checked)

here is the code I use, but it fails as I am not able to target the checkboxes:

    private void check_CheckedChanged(Object sender, EventArgs e)
    {
        for (int i = 0; i < filesToOpen.Count(); i++)
        {
            string tbarName = "tbar" + i;
            string checkName = "check" + i;

            CheckBox ckb = this.Controls.OfType<CheckBox>()
                     .Where(c => c.AccessibleName.Equals(checkName)) as CheckBox;
            TrackBar tkb = this.Controls.OfType<TrackBar>()
                     .Where(t => t.AccessibleName.Equals(tbarName)) as TrackBar;
            //TrackBar tkb = this.Controls.Find(tbarName, false).First() as TrackBar;
            //CheckBox ckb = this.Controls.Find(checkName, false).First() as CheckBox;

            if (ckb.Checked == true)
            {
                //do stuff
            }
        }
    }

what am I doing wrong/really wrong?

解决方案

Given that you add the checkboxes to your own list:

CheckBoxes.Add(check);

it would be simpler to loop over that rather than trying to find the control associated with the file:

foreach (var checkBox in CheckBoxes)
{
    if (checkbox.Checked)
    {
        // Do stuff...
    }
}

However, you shouldn't need to use a separate list. This line is wrong:

CheckBox ckb = this.Controls.OfType<CheckBox>()
                   .Where(c => c.AccessibleName.Equals(checkName)) as CheckBox;

Where returns a IEnumerable<CheckBox> but you are trying to cast it directly to a CheckBox which will return null. What you should have is:

CheckBox ckb = this.Controls.OfType<CheckBox>()
                   .Where(c => c.AccessibleName.Equals(checkName)).First();

You will still need to check to see if ckb is null (just in case there is nothing on the list) but this should return you the control you are looking for.

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

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