我有一個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