Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式检查复选框列表中文本等于所需内容的项目_C#_Items_Checklistbox - Fatal编程技术网

C# 以编程方式检查复选框列表中文本等于所需内容的项目

C# 以编程方式检查复选框列表中文本等于所需内容的项目,c#,items,checklistbox,C#,Items,Checklistbox,在C语言中,我试图检查复选框列表中的一个项目,其中的文本等于我需要的内容 我将修改代码以检查数据库中存在的项 如果您想要一个示例,我需要选择等于abc的checklistbox项。假设CheckedListBox中的项是字符串: for (int i = 0; i < checkedListBox1.Items.Count; i++) { if ((string)checkedListBox1.Items[i] == value) { checkedLi

在C语言中,我试图检查复选框列表中的一个项目,其中的文本等于我需要的内容

我将修改代码以检查数据库中存在的项


如果您想要一个示例,我需要选择等于abc的checklistbox项。

假设CheckedListBox中的项是字符串:

  for (int i = 0; i < checkedListBox1.Items.Count; i++)
  {
    if ((string)checkedListBox1.Items[i] == value)
    {
      checkedListBox1.SetItemChecked(i, true);
    }
  }

基于ASP.NET复选框列表的示例

<asp:CheckBoxList ID="checkBoxList1" runat="server">
    <asp:ListItem>abc</asp:ListItem>
    <asp:ListItem>def</asp:ListItem>
</asp:CheckBoxList>


private void SelectCheckBoxList(string valueToSelect)
{
    ListItem listItem = this.checkBoxList1.Items.FindByText(valueToSelect);

    if(listItem != null) listItem.Selected = true;
}

protected void Page_Load(object sender, EventArgs e)
{
    SelectCheckBoxList("abc");
}

全部归功于@Jim Scott-只增加了一点触感。ASP.NET 4.5和C

再折射一点。。。如果将复选框列表作为对象传递给该方法,则可以将其用于任何复选框列表。也可以使用文本或值

private void SelectCheckBoxList(string valueToSelect, CheckBoxList lst)
{
    ListItem listItem = lst.Items.FindByValue(valueToSelect);
    //ListItem listItem = lst.Items.FindByText(valueToSelect);
    if (listItem != null) listItem.Selected = true;
}

//How to call it -- in this case from a SQLDataReader and "chkRP" is my CheckBoxList`

SelectCheckBoxList(dr["kRPId"].ToString(), chkRP);`
//多重选择:

          private void clbsec(CheckedListBox clb, string text)
          {
              for (int i = 0; i < clb.Items.Count; i++)
              {
                  if(text == clb.Items[i].ToString())
                  {
                      clb.SetItemChecked(i, true);
                  }
              }
          }

我尝试添加动态创建的ListItem并分配所选值

foreach(var item in yourListFromDB)
{
 ListItem listItem = new ListItem();
 listItem.Text = item.name;
 listItem.Value = Convert.ToString(item.value);
 listItem.Selected=item.isSelected;                 
  checkedListBox1.Items.Add(listItem);
}
checkedListBox1.DataBind();

避免使用绑定数据源,因为它不会从数据库绑定选中/未选中的数据源

你自己试过了吗?复选框列表的界面是你不懂的吗?请不要在标题前加C之类的前缀。这就是标签的作用。请参阅帖子中关于签名的常见问题解答。我也删除了你的感谢信,因为很可能没有人会帮助你,因为你没有表现出你试图帮助自己。我花了两个小时试图找到解决方法,但我似乎不知道如何让checkedListBox.SetItemChecked与checklistbox中的一个项目相关联。FindByText函数对我来说不存在。这是吗asp.net应用程序?让我知道什么版本的.NET和什么环境,我很乐意为您提供具体说明。嗨,Jim,我将在Visual Studio 2010中使用C.NET,使用Framework 4.0客户端。对,但是什么样的项目?Winform、Silverlight、ASP.NET…?这是您想要匹配的值,例如MyValueifindex>0的字符串文字应该是ifindex>=0,因为如果IndexOf在列表中找到第一项,它可以返回零。如果找不到该值,则返回-1。仅检查第一项如何使用此方法使用foreach。我看不到。SetItemChecked作为扩展添加一些解释,说明此答案如何帮助解决当前问题
clbsec(checkedListBox1,"michael");

or 

clbsec(checkedListBox1,textBox1.Text);

or

clbsec(checkedListBox1,dataGridView1.CurrentCell.Value.toString());
foreach(var item in yourListFromDB)
{
 ListItem listItem = new ListItem();
 listItem.Text = item.name;
 listItem.Value = Convert.ToString(item.value);
 listItem.Selected=item.isSelected;                 
  checkedListBox1.Items.Add(listItem);
}
checkedListBox1.DataBind();