4

WindowsフォームアプリケーションにCheckedListBoxコントロールがあり、そこから選択するアイテムのリストが表示されます。ユーザーがまだチェックしている間、チェックされた(選択されていない)アイテムのみをカウントして表示したい。私はあなたがそれらをチェックするときに数えることを意味します。

ItemCheckイベントとCheckedListBox.CheckedItems.Countを使用しようとしましたが、問題は、アイテムがチェックされていない場合でも、1回おきにクリックがカウントされることです。何かをチェックするとカウントされ、もう一度チェックを外すと、それもカウントされます。

これは、MSDNの「ItemCheckイベントが発生するまでチェック状態は更新されない」というコメントと関係があると思います。私はここでの問題を完全には理解していません。

ありがとうございました。

4

6 に答える 6

9

ItemCheckEventArgs パラメーターには、変更がチェックされているか、チェックされていないか、どちらでもないかを示すプロパティ (NewValue) があります。

イベントが発生するまで CheckedItems.Count が更新されない場合 (これは私がその発言から理解していることです) - そのカウントを追加して、ItemChecckEventArgs がチェック (+1) かチェック解除 (-1) かを確認できます。 )、正しい合計を取得できます。

(私がその発言を間違って理解していない限り、それは非常に曖昧です)。

于 2012-10-25T14:48:17.333 に答える
7

Haedrian はこれに正しく答えましたが、生のコードも大いに役立つと思います。したがって、C# コードは次のとおりです。

private void CheckedListBox_ItemCheck(Object sender, ItemCheckEventArgs e)
{
    int sCount = checkedListBox.CheckedItems.Count;
    if (e.NewValue == CheckState.Checked  ) { ++sCount; }
    if (e.NewValue == CheckState.Unchecked) { --sCount; }
    // now sCount is count of selected items in checkedListBox.
}

私は同じ問題を抱えていましたが、この質問と回答で解決策が得られました。質問と既存の回答をありがとう!

于 2013-11-04T20:23:37.763 に答える
3

のイベント ハンドラを追加しSelectedIndexChanged、 からカウントを取得しますCheckedItems.Count

ItemCheck実際の値はありませんが、最後の変更が処理される前の値であり、Haedrian が提案したように EventArgs に従ってカウントを調整する必要があります。

于 2012-10-25T14:47:01.703 に答える
1

CheckedItems.Count の更新された値をすぐにe.NewValue取得しないため、状態が Checked であるかどうかを取得するために更新された値を取得するために使用します。

カウントを取得するためのグローバル変数を宣言します

int chkListCount = 0;

ItemCheckイベントでは、次のコードを与えます

private void chkList_ItemCheck(object sender, ItemCheckEventArgs e)
{
     if (e.NewValue == CheckState.Checked)
     {
         chkListCount++;
     }
     else if(e.NewValue == CheckState.Unchecked)
     {
         chkListCount--;
     }
}

選択したアイテムの数を取得する単純なロジック

于 2014-11-13T09:35:10.790 に答える
0

初心者としては賢明で知的な仕事ではありませんが、最終的には私の問題を解決しました.

private void CheckedListBox_ItemCheck(Object sender, ItemCheckEventArgs e)
    {
        int count = 0;
        if (e.NewValue.ToString() == "Checked")
        {
            // first get all the checked items
            foreach (object checkeditem in CheckedListBox.CheckedItems)
            {
                string checkItem = CheckedListBox.GetItemCheckState(CheckedListBox.Items.IndexOf(checkeditem)).ToString();
                if (checkItem == "Checked")
                {
                    count = count + 1;
                }
            }
            // Now, below is the most important part considering the remark on MSDN
            // "The check state is not updated until after the ItemCheck event occurs."
            count = count + 1; // Plus 1 as the NewValue was Checked
            labelCount.Text = "You have selected " + count + "Items.";
        }
        else if (e.NewValue.ToString() == "Unchecked")
        {
            // first get all the checked items
            foreach (object checkeditem in CheckedListBox.CheckedItems)
            {
                string checkItem = CheckedListBox.GetItemCheckState(CheckedListBox.Items.IndexOf(checkeditem)).ToString();
                if (checkItem == "Checked")
                {
                    count = count + 1;
                }
            }
            // Now, below is the most important part considering the remark on MSDN
            // "The check state is not updated until after the ItemCheck event occurs."
            count = count - 1; // minus 1 as the NewValue was Unchecked,
            labelCount.Text = "You have Selected " + count + "Items.";
        }
    }  

このコードに関するコメントをいただければ幸いです。

于 2012-10-25T16:34:00.510 に答える
0

これはずっと前に回答されていることは知っていますが、MouseUp および KeyUp イベントを処理する方が簡単であることがわかりました。これらのイベントが発生した場合、CheckedItems.Count プロパティは正確です。どちらも同じことを行うため、作業へのメソッドを作成し、両方のイベント ハンドラーからそのメソッドを呼び出しました。

    private void clbFolders_KeyUp(object sender, KeyEventArgs e) { Update(); }
    private void clbFolders_MouseUp(object sender, MouseEventArgs e) { Update(); }

    private void Update()
    {
        btnDelete.Enabled = clbFolders.CheckedItems.Count > 0;
    }
于 2017-02-04T02:06:38.030 に答える