以编程方式检查DataBound CheckListBox [英] Programmatically Checking DataBound CheckListBox

查看:92
本文介绍了以编程方式检查DataBound CheckListBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataBound CheckedListBox,我需要检查其中的一些项目。我尝试使用以下代码...

I have a DataBound "CheckedListBox", I need to check some items on it. I tried with following code...

if (!string.IsNullOrEmpty(search.Languages))
        {
            string[] langs = search.Languages.Split(',');
            for (int i = 0; i < (langs.Length - 1); i++)
            {
                for (int j = 0; j < clbLang.Items.Count; j++)
                {
                    string lng = clbLang.Items[j] as string;
                    if (lng.Trim() == langs[i])
                    {
                        clbLang.SetItemChecked(j, true);
                        break;
                    }
                }
            }
        }

否错误,调试执行正在经历 检查过程,但最终我看不到对其进行检查。

No errors, debuged execution is going through "checking" process, but finally I cannot see anything checked on it.

然后我添加了一个按钮并添加以下代码。 (点击检查所有项目

Then I have added a button and added following code to it. (upon click check all the items)

private void button9_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < clbLang.Items.Count; i++)
        {
            clbLang.SetItemChecked(i, true);
        }
    }

这是 检查 所有项目,请告诉我是否有人在这里看到问题...?

It is "checking" all the items, Please tell me if anyone can see a issue here...?

推荐答案

最后发现,它是MS引入的错误

Finally found out, it is a Bug introduced by MS.

在这里已得到很好的解释。

It is well explained here.


问题很容易重现。只需隐藏并显示一个数据绑定的
CheckedListBox,您就会注意到先前检查过的
是如何取消选中
的。

The issue is easy to reproduce. Just hide and show a databound CheckedListBox and you will notice how the previously checked items get unchecked.

CheckedListBox SetItemChecked方法不起作用

所以我们必须找到一种解决方法...我尝试了以下方法,它的工作原理很好...

So we have to find a workaround... I tried follwing way, it is working nice...

在我调用检查的地方添加了以下内容...我要添加需要在控件的标记中检查的内容

At the place where I was calling checking of items I have added following... I am adding what I need to check in Tag of the control.

if (!string.IsNullOrEmpty(search.Languages))
{
    clbLang.Tag = search.Languages;
}

然后,遵循该控件的 VisibleChanged()事件中的代码。

Then, following code in that control's "VisibleChanged()" event.

private void clbLang_VisibleChanged(object sender, EventArgs e)
    {
        string lngs = clbLang.Tag as string;
        if (!string.IsNullOrEmpty(lngs))
        {
            string[] langs = lngs.Split(',');
            foreach (string lang in langs)
            {
                int j = 0;
                foreach (DataRowView row in clbLang.Items)
                {
                    if (row != null)
                    {
                        string lng = row[1] as string;
                        if (lng.Trim() == lang)
                        {
                            clbLang.SetItemChecked(j, true);
                            break;
                        }
                    }
                    j++;
                }
            }
        }
    }

此与我合作良好,希望它将对您有利...

This works well with me, hope it will benefit you...

这篇关于以编程方式检查DataBound CheckListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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