簡體   English   中英

C#-當選中特定項目時,自動檢查checkedlistbox中的項目

[英]C# - Auto-check items in checkedlistbox when specific item is checked

我的程序中有一個清單框,該框允許用戶控制在圖形上啟用的9個可能的序列中的哪個。 我已經做到了,因此可以使用以下代碼一次僅在圖形上啟用兩個系列:

//if there are two checked items
if (e.NewValue == CheckState.Checked && chListBoxChartSeries.CheckedItems.Count >= 2)
{
    //new item cannot be checked
    e.NewValue = CheckState.Unchecked;
}

這很好。 但是,我本人和我的最終用戶發現,一個特定系列(復選框列表和圖表系列索引2)實際上沒有任何意義,除非與其他兩個系列(索引3和4)相比將其顯示出來。 我已經嘗試更改上面的代碼,以識別索引2中的項目已選中,並允許我-甚至自動-檢查相關索引中的項目。 完整功能如下圖所示:

    private void chListBoxChartSeries_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        int indexA = 2;
        //specify indexes for related parameters
        int indexB = 3;
        int indexC = 4;
        //give positive checkstate
        CheckState autochecked = CheckState.Checked;

        if (chListBoxChartSeries.CheckedItems.Contains(chListBoxChartSeries.Items[indexA]))
        {
            //do not limit number of checked items
            //apply checkstates to items at this index
            chListBoxChartSeries.SetItemCheckState(indexB, autochecked);
            chListBoxChartSeries.SetItemCheckState(indexC, autochecked);
        }
        else //does not contain item at index 2
        {
            //if there are two checked items
            if (e.NewValue == CheckState.Checked && chListBoxChartSeries.CheckedItems.Count >= 2)
            {
                //new item cannot be checked
                e.NewValue = CheckState.Unchecked;
            }

        }
    }

這給我帶來了各種各樣的錯誤,盡管這些錯誤都沒有特別有用,而且我在此論壇上對他們發現的答案似乎與我的問題無關! 我真的不知道我離要達到的目標還有多遠,因此任何建議或正確方向的建議都將為您帶來巨大的幫助!

謝謝馬克

問題是您要在此處創建無限循環。 選擇項目2時,直到此方法完成后的一段時間,它才立即添加到chListBoxChartSeries.CheckedItems 因此,它不會在第一次迭代中運行if語句的第一部分,因此不會自動檢查項目3和4。

后來,當你手動選擇,比如說,第3項, 它將運行的第一部分。 但是,一旦到達chListBoxChartSeries.SetItemCheckState(indexB, autochecked); ,它將再次觸發事件。 它將到達同一行,並無限期地重復該過程。

這是一個粗略的版本,應該更接近您的需求。 有點着急,所以我確定您需要整理一下邏輯:

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    int indexA = 2;
    //specify indexes for related parameters
    int indexB = 3;
    int indexC = 4;
    //give positive checkstate
    CheckState autochecked = CheckState.Checked;

    if (e.Index == indexA && !chListBoxChartSeries.CheckedItems.Contains(chListBoxChartSeries.Items[indexA]))
    {
        //do not limit number of checked items
        //apply checkstates to items at this index
        chListBoxChartSeries.SetItemCheckState(indexB, autochecked);
        chListBoxChartSeries.SetItemCheckState(indexC, autochecked);
    }
    else //does not contain item at index 2
    {
        //if there are two checked items
        if (chListBoxChartSeries.CheckedItems.Contains(chListBoxChartSeries.Items[indexA]) && !(e.Index == indexB || e.Index == indexC))
        {
            if (e.NewValue == CheckState.Checked && chListBoxChartSeries.CheckedItems.Count >= 2)
            {
                //new item cannot be checked
                e.NewValue = CheckState.Unchecked;
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM