1

CheckedListBoxX個のアイテムを持つ があります。これらのアイテムは、実行時にそこに配置されます。これらのアイテムは、 に表示できるレポートを表すと想定されてい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 はチェックする必要があるレポートです。

4

4 に答える 4

1

もし私があなたなら、INotifyPropertyChanged インターフェイスを試してみます。必要でない限り、イベントを台無しにしないでください。これは、デザイナーを使用してアイテムを作成できないことを意味しますが、私が理解している限り、とにかく実行時に変更されたリストです...

詳細に:

• INotifyPropertyChanged を実装するクラス (「Foo」など) を作成します (基本的に、これにより、テキスト プロパティが変更されたことがリスナーに通知されます)。このクラスは、すべてのエントリの名前を保持します。

• ObservableCollection を作成し、CheckedListBox をそのコレクションにバインドします。WinForms では、DataBindingSource を作成し、コレクションを一方の端に接続し、ComboBox をもう一方の端に接続する必要があります。

• コレクションに加えられた変更は、コントロールに表示されます。

HTHセビ

于 2010-09-08T13:40:10.227 に答える
0

ListBox(またはCheckedListBox)の項目を変更するには、これらの項目のToString()結果を変更する必要があります。

最も簡単な解決策は、それが表すレポートへの参照を持つ「Holder」クラスを作成することです。その場合、HolderクラスのToString()メソッドは次のようになります。

public override string ToString()
{
   return String.Format("{0} ({1})", BaseStr, MyReport.RecordCount);
}

なんらかの方法で変更した場合MyReport.RecordCount(レポートのレコード数が変更されたため)、を呼び出すだけclbReports.Refresh()で、新しい値が自動的に表示されます。

この方法では、2番目のコードブロックに一時配列ソリューションも必要ないと思います。ただし、アイテムのチェック状態を取得する別の方法を提案したいと思います。を繰り返し処理し、その配列内のインデックスの値のみを配列に入力clbReports.CheckedIndicesできます。bCheckedtrue

于 2010-09-08T15:03:28.190 に答える
0

さて、時間の制約のために、私は別のものを試しました。私は、CheckBoxes = true および View = List の ListView を使用しました。また、[すべてのエラー レポートを表示] と [タグ付きレコードを表示] をリストの外側のチェックボックスから削除しました。これにより、必要な機能を実行するのがはるかに簡単になりました。これが新しいコードです。

MainForm.Designer.cs

    // 
    // 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);

MainForm.cs

    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() + ")";

皆さんの助けとアイデアに感謝します。

于 2010-09-08T16:54:54.217 に答える