【问题标题】:C# - Auto-check items in checkedlistbox when specific item is checkedC# - 检查特定项目时自动检查选中列表框中的项目
【发布时间】:2015-09-09 09:28:35
【问题描述】:

我的程序中有一个选中列表框,允许用户控制在图表上启用的 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;
}

这很好用。然而,我和我的最终用户发现,一个特定系列(checkedlistbox 和图表系列索引 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;
            }

        }
    }

这给我带来了各种各样的错误,尽管它们都没有提供特别丰富的信息,而且我在这个论坛上找到的答案似乎与我的问题无关! 我真的不知道我离实现我想要实现的目标还有多远,所以任何建议或正确方向的观点都会有很大帮助!

谢谢, 标记

【问题讨论】:

    标签: c# charts checkedlistbox


    【解决方案1】:

    问题是你在这里创建了一个无限循环。当您选择第 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;
                }
            }
        }
    }
    

    【讨论】:

    • 您好,感谢您的回复。我已经对此进行了测试,它最初似乎可以工作,我相信我将能够从现在开始清理它给我带来的任何问题!谢谢@MichaelMcMullin
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    相关资源
    最近更新 更多