CheckedListBox
X個のアイテムを持つ があります。これらのアイテムは、実行時にそこに配置されます。これらのアイテムは、 に表示できるレポートを表すと想定されていDataGridView
ます。ここで行う必要があるのは、レポート名のすぐ横にある括弧内に各レポートのレコード数を表示することです。あまり長くはありませんが、アイテムの実際の名前を編集しようとしましたが、その方法がわかりませんでした。それで、私はそれを力ずくで強制しました。アイテムを配列に保存し、アイテムをクリアし、配列内の各アイテムにレコード数を追加し、新しいアイテムを作成しました。さて、これは問題を引き起こしました。なぜなら、レポートを生成するたびにアイテムをクリアして再作成するためです。まあ、別のことをするよりも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にキャストできたとしても、リストが変更されたために列挙が失敗したというエラーが表示される気がします(または、彼らはそれを言います)。どんな助けでも大歓迎です。ありがとう。
編集:レポート X は、実際のレポート名を一般的なものに保つために表示されないようにするためのものであることをご承知おきください。ただし、コードではコピーして貼り付けただけなので、Show All Error Reports と Show All Tagged Records はチェックする必要があるレポートです。