我有一个CheckedListBox
有X个项目。这些项目在运行时放置在那里。这些项目应该代表可以在DataGridView
中显示的报告。我现在需要做的是在报告名称旁边的括号内显示每个报告的记录计数。我试了一下,并没有太长时间来编辑项目的实际名称,但无法知道如何去做。那么,我蛮横的强迫它。将项目保存到数组中,清除项目,将记录计数附加到数组中的每个项目,创建新项目。那么,这已经造成了问题,因为现在它不保留我的支票,原因是因为每当我生成报告时,我都清除这些项目并重新创建它们。那么,有没有人知道更改CheckedListBox
中现有项目的文本的方法,而不是做另一个foreach
循环来保存检查的状态?如何编辑CheckedListBox中的项目名称?
这里是我目前拥有的代码:
在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();
}
而这里就是我清理的项目,得到了报告数量和创造新项目的代码。
_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++;
}
请告诉我,还有一个更简单的方法来做到这一点。我尝试了通过clbReports.Items做foreach循环,但它想让我把它转换为一个字符串(当试图转换为CheckBox时出错),所以我无法更改该值。即使我可以将它转换为CheckBox,我也有一种感觉,它会给我Enumeration失败的错误,因为列表已被更改(或者他们用它来表示)。任何和所有的帮助是受欢迎的。谢谢。
编辑:请知道,报告X只是为了使实际的报告名称不显示为通用。但是,在代码中,我只是复制并粘贴,以便显示所有错误报告和显示所有标记记录是我需要检查的报告。
你不需要在clbReports_SelectedIndexChanged的else语句中如果strCheckBox.Contains(“显示所有错误报告”),因为它永远不会是真的,因为它们都将进入'如果'部分,从来没有其他人。只是一点点,但它会帮助保持代码更清晰 – w69rdy 2010-09-08 13:31:05
为什么不只是'int nCount = _lstReportRecords.Where(rr => rr.Description == str)'而不是复杂的方式? – Timwi 2010-09-08 13:33:04
@Timwi - 老实说,因为我不知道这是一个更简单的方法。 :)我认为你必须做类似于你定义一个函数的JavaScript。现在在我的代码中改变了....在我所做的所有地方都是这样。 :) – XstreamINsanity 2010-09-08 13:35:55