C# 如何编辑CheckedListBox中项目的名称?

C# 如何编辑CheckedListBox中项目的名称?,c#,winforms,checkedlistbox,C#,Winforms,Checkedlistbox,我有一个CheckedListBox,它有X个项目。这些项目在运行时放置在那里。这些项目应该表示可以在DataGridView中显示的报告。我现在需要做的是在报告名称旁边的括号中显示每个报告的记录计数。我试图编辑项目的实际名称,但时间不长,但找不到如何编辑。于是,我野蛮地强迫它。将项目保存到数组中,清除项目,将记录计数附加到数组中的每个项目,创建新项目。这导致了问题,因为现在它没有保留我的检查,原因是每当我生成报告时,我都会清除项目并重新创建它们。那么,除了执行另一个foreach循环来保存选中

我有一个
CheckedListBox
,它有X个项目。这些项目在运行时放置在那里。这些项目应该表示可以在
DataGridView
中显示的报告。我现在需要做的是在报告名称旁边的括号中显示每个报告的记录计数。我试图编辑项目的实际名称,但时间不长,但找不到如何编辑。于是,我野蛮地强迫它。将项目保存到数组中,清除项目,将记录计数附加到数组中的每个项目,创建新项目。这导致了问题,因为现在它没有保留我的检查,原因是每当我生成报告时,我都会清除项目并重新创建它们。那么,除了执行另一个
foreach
循环来保存选中状态外,有人知道如何更改
选中列表框中现有项的文本吗

以下是我目前拥有的代码:

在MainForm.Designer.cs中:

this.clbReports.Items.AddRange(new object[] {
"Report 1",
"Report 2",
"Report 3",
"Report 4",
"Report 5",
"Report 6",
"Report 7",
"Report 8",
"Report 9",
"Report 10",
"Report 11"});
它看起来像:

我希望它看起来像(但不会全部是0):

以下是SelectedIndexChanged函数:

private void clbReports_SelectedIndexChanged(object sender, EventArgs e)
{
    string strCheckBox = clbReports.SelectedItem.ToString();
    bool bShowAllIsChecked = clbReports.GetItemChecked(clbReports.FindString("Show All Error Reports"));
    bool bSelected = clbReports.GetItemChecked(clbReports.FindString(strCheckBox));
    int nIndex = -1;

    if (strCheckBox.Contains("Show All Error Reports"))
    {
        foreach (string str in _strReports)
        {
            if (!str.Contains("Show All Error Reports") && !str.Contains("Show Tagged Records"))
            {
                nIndex = clbReports.FindString(str);
                if (nIndex > -1)
                {
                    clbReports.SetItemChecked(nIndex, bSelected);
                }
            }
        }
    }
    else
    {
        if (strCheckBox.Contains("Show All Error Reports") || bShowAllIsChecked)
        {
            foreach (string str in _strReports)
            {
                nIndex = clbReports.FindString(str);
                if (nIndex > -1)
                {
                clbReports.SetItemChecked(nIndex, false);
                }
            }
        }

        nIndex = clbReports.FindString(strCheckBox);
        if (nIndex > -1)
        {
            clbReports.SetItemChecked(nIndex, bShowAllIsChecked ? true : bSelected);
        }
    }

    string[] strCheckedItems = new string[clbReports.CheckedItems.Count];
    clbReports.CheckedItems.CopyTo(strCheckedItems, 0);
    List<string> checkBoxReportFilter = new List<string>();
    foreach (ReportRecord obj in this._lstReportRecords)
    {
        foreach (string str in strCheckedItems)
        {
            if (str.Contains(obj.Description))
            {
                checkBoxReportFilter.Add(obj.PartID.ToString());
            }
        }
    }
    try
    {
        if (checkBoxReportFilter.Count == 0 && clbReports.CheckedItems.Count > 0)
        {
            throw new NullReferenceException();
        }

        _strReportFilter = String.Join(",", checkBoxReportFilter.ToArray());
    }
    catch (NullReferenceException)
    {
        _strReportFilter = "-1";
    }

    generateReport();
}
private void clbreport\u SelectedIndexChanged(对象发送方,事件参数e)
{
字符串strCheckBox=clbReports.SelectedItem.ToString();
bool bShowAllIsChecked=clbReports.GetItemChecked(clbReports.FindString(“显示所有错误报告”);
boolbselected=clbReports.GetItemChecked(clbReports.FindString(strCheckBox));
int nIndex=-1;
if(strCheckBox.Contains(“显示所有错误报告”))
{
foreach(字符串str在_strReports中)
{
如果(!str.Contains(“显示所有错误报告”)&&!str.Contains(“显示标记记录”))
{
nIndex=clbreport.FindString(str);
如果(nIndex>-1)
{
clbReports.SetItemChecked(nIndex,bSelected);
}
}
}
}
其他的
{
if(strCheckBox.Contains(“显示所有错误报告”)| | bShowAllIsChecked)
{
foreach(字符串str在_strReports中)
{
nIndex=clbreport.FindString(str);
如果(nIndex>-1)
{
clbreport.SetItemChecked(nIndex,false);
}
}
}
nIndex=clbreport.FindString(strCheckBox);
如果(nIndex>-1)
{
clbReports.SetItemChecked(nIndex,bShowAllIsChecked?true:bSelected);
}
}
string[]strCheckedItems=新字符串[clbreport.CheckedItems.Count];
clbreport.CheckedItems.CopyTo(strCheckedItems,0);
List checkBoxReportFilter=新列表();
foreach(此中的ReportRecord obj.\lstu ReportRecords)
{
foreach(strCheckedItems中的字符串str)
{
if(str.Contains(对象描述))
{
checkBoxReportFilter.Add(obj.PartID.ToString());
}
}
}
尝试
{
如果(checkBoxReportFilter.Count==0&&clbReports.CheckedItems.Count>0)
{
抛出新的NullReferenceException();
}
_strReportFilter=String.Join(“,”,checkBoxReportFilter.ToArray());
}
捕获(NullReferenceException)
{
_strReportFilter=“-1”;
}
generateReport();
}
这是我清除项目、获取报告计数和创建新项目的代码

_lstReportRecords = _dataController.ReportList;
bool[] bChecked = new bool[clbReports.Items.Count];
int nCounter = 0;
foreach (string str in _strReports)
{
    foreach (string str2 in clbReports.SelectedItems)
    {
        bChecked[nCounter] = str2.Contains(str);
    }
    nCounter++;
}

clbReports.Items.Clear();
nCounter = 0;

foreach (string str in _strReports)
{
    int nCount = _lstReportRecords.Where<ReportRecord>(delegate(ReportRecord rr) {
        return rr.Description == str;
    }).Count();

    string newReport = str + " (" + nCount + ")";
    clbReports.Items.Add(newReport);
    clbReports.SetItemChecked(nCounter, bChecked[nCounter]);
    nCounter++;
}
\u lstreptrecords=\u dataController.ReportList;
bool[]b检查=新bool[clbreport.Items.Count];
int n计数器=0;
foreach(字符串str在_strReports中)
{
foreach(clbreport.SelectedItems中的字符串str2)
{
b检查[nCounter]=str2.Contains(str);
}
nCounter++;
}
clbreport.Items.Clear();
n计数器=0;
foreach(字符串str在_strReports中)
{
int nCount=lstReportRecords.Where(委托(ReportRecord rr){
返回rr.Description==str;
}).Count();
字符串newReport=str+“(+nCount+”);
clbreport.Items.Add(newReport);
clbreport.SetItemChecked(n计数器,b检查[n计数器]);
nCounter++;
}
请告诉我有一个更简单的方法。我尝试通过clbreport.Items执行foreach循环,但它希望我将其转换为字符串(尝试转换为复选框时出错),因此无法更改值。即使我可以将它转换为一个复选框,我感觉它会给我一个错误,即枚举失败是因为列表已更改(或他们如何命名它)。欢迎任何和所有的帮助。谢谢


编辑:请注意,报告X只是为了不显示实际的报告名称,以保持其通用性。但是,在代码中,我只是复制和粘贴了“显示所有错误报告”和“显示所有标记的记录”是我需要检查的报告。

如果我是您,我会尝试尝试使用INotifyPropertyChanged接口。 除非必要,否则你不应该把事情搞砸。这意味着您不能使用设计器来创建项,但据我所知,它是一个运行时修改的列表

详细内容:

•创建一个实现INotifyPropertyChanged的类(例如“Foo”)(基本上这会告诉任何侦听器文本属性已经更改)。此类将保存所有条目的名称

•创建一个ObservableCollection并将您的CheckedListBox绑定到该集合。在WinForms中,您必须创建一个数据绑定源,并将集合插入一端,将组合框插入另一端

•对集合所做的任何更改都将在控件中可见


Sebi

要更改列表框(或选中列表框)中的项目,应将这些项目“
更改为字符串()
结果

最简单的解决方案是创建一个“Holder”类,该类引用它所表示的报告。那么Holder类的ToString()方法应该是
public override string ToString()
{
   return String.Format("{0} ({1})", BaseStr, MyReport.RecordCount);
}
    // 
    // cbTaggedRecords
    // 
    this.cbTaggedRecords.AutoSize = true;
    this.cbTaggedRecords.Location = new System.Drawing.Point(151, 9);
    this.cbTaggedRecords.Name = "cbTaggedRecords";
    this.cbTaggedRecords.Size = new System.Drawing.Size(106, 17);
    this.cbTaggedRecords.TabIndex = 3;
    this.cbTaggedRecords.Text = "Tagged Records";
    this.cbTaggedRecords.UseVisualStyleBackColor = true;
    this.cbTaggedRecords.CheckedChanged += new System.EventHandler(this.ShowTaggedRecords_CheckChanged);
    // 
    // cbAllErrorReports
    // 
    this.cbAllErrorReports.AutoSize = true;
    this.cbAllErrorReports.Location = new System.Drawing.Point(6, 9);
    this.cbAllErrorReports.Name = "cbAllErrorReports";
    this.cbAllErrorReports.Size = new System.Drawing.Size(102, 17);
    this.cbAllErrorReports.TabIndex = 2;
    this.cbAllErrorReports.Text = "All Error Reports";
    this.cbAllErrorReports.UseVisualStyleBackColor = true;
    this.cbAllErrorReports.CheckedChanged += new System.EventHandler(this.ShowAllErrorReports_CheckChanged);
    // 
    // listView1
    // 
    this.listView1.CheckBoxes = true;
    listViewItem1.StateImageIndex = 0;
    listViewItem2.StateImageIndex = 0;
    listViewItem3.StateImageIndex = 0;
    listViewItem4.StateImageIndex = 0;
    listViewItem5.StateImageIndex = 0;
    listViewItem6.StateImageIndex = 0;
    listViewItem7.StateImageIndex = 0;
    listViewItem8.StateImageIndex = 0;
    listViewItem9.StateImageIndex = 0;
    this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
    listViewItem1,
    listViewItem2,
    listViewItem3,
    listViewItem4,
    listViewItem5,
    listViewItem6,
    listViewItem7,
    listViewItem8,
    listViewItem9});
    this.listView1.Location = new System.Drawing.Point(6, 29);
    this.listView1.Name = "listView1";
    this.listView1.Size = new System.Drawing.Size(281, 295);
    this.listView1.TabIndex = 1;
    this.listView1.UseCompatibleStateImageBehavior = false;
    this.listView1.View = System.Windows.Forms.View.List;
    this.listView1.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandler(this.listView_ItemChecked);
    private void listView_ItemChecked(object sender, ItemCheckedEventArgs e)
    {
        if (e != null)
        {
            int nLength = e.Item.Text.IndexOf("(") - 1;
            string strReport = nLength <= 0 ? e.Item.Text : e.Item.Text.Substring(0, nLength);
            if (e.Item.Checked)
            {
                _lstReportFilter.Add(strReport);
            }
            else
            {
                _lstReportFilter.Remove(strReport);
            }
        }

        List<string> checkBoxReportFilter = new List<string>();
        foreach (ReportRecord obj in this._lstReportRecords)
        {
            foreach (string str in _lstReportFilter)
            {
                if (str.ToLower().Contains(obj.Description.ToLower()))
                {
                    checkBoxReportFilter.Add(obj.PartID.ToString());
                }
            }
        }
        try
        {
            if (checkBoxReportFilter.Count == 0 && listView1.CheckedItems.Count > 0)
            {
                throw new NullReferenceException();
            }

            _strReportFilter = String.Join(",", checkBoxReportFilter.ToArray());
        }
        catch (NullReferenceException)
        {
            _strReportFilter = "-1";
        }

        if (!bShowAll)
        {
            generateReport();
        }
    }

    private void ShowAllErrorReports_CheckChanged(object sender, EventArgs e)
    {
        bShowAll = true;
        foreach (ListViewItem lvi in listView1.Items)
        {
            lvi.Checked = ((CheckBox)sender).Checked;
        }

        _lstReportFilter.Clear();
        bShowAll = false;
        generateReport();
    }

    private void ShowTaggedRecords_CheckChanged(object sender, EventArgs e)
    {
        bool bChecked = ((CheckBox)sender).Checked;
        if (bChecked)
        {
            if (!_lstReportFilter.Contains("Show Tagged Records"))
            {
                _lstReportFilter.Add("Show Tagged Records");
            }
        }
        else
        {
            _lstReportFilter.Remove("Show Tagged Records");
        }

        listView_ItemChecked(null, null);
    }
            _lstReportRecords = _dataController.ReportList;

            int nTotalCount = 0;

            foreach (ListViewItem lvi in listView1.Items)
            {
                int nCount = _lstReportRecords.Where(rr => lvi.Text.Contains(rr.Description)).Count();
                nTotalCount += nCount;
                lvi.Text = (lvi.Text.Contains("(") ? lvi.Text.Substring(0, lvi.Text.IndexOf("(") + 1) : lvi.Text + " (") + nCount.ToString() + ")";
            }

            cbAllErrorReports.Text = (cbAllErrorReports.Text.Contains("(") ? cbAllErrorReports.Text.Substring(0, cbAllErrorReports.Text.IndexOf("(") + 1) : cbAllErrorReports.Text + " (") + nTotalCount.ToString() + ")";
            int nTaggedCount = _lstReportRecords.Where(rr => rr.Description.Contains("Tagged")).Count();
            cbTaggedRecords.Text = (cbTaggedRecords.Text.Contains("(") ? cbTaggedRecords.Text.Substring(0, cbTaggedRecords.Text.IndexOf("(") + 1) : cbTaggedRecords.Text + " (") + nTaggedCount.ToString() + ")";
this.clbReports.Items[nIndex] = "new text of the item"