如何取消选择“全选”按钮并不是所有项目?

时间:2015-09-30 05:52:50

标签: c# winforms visual-studio-2012

我有checkbox来选择全部/取消选择checkedListBox中的所有项目。为此,我有下一个工作代码:

 private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
 if (checkedListBox2.CheckedItems.Count == checkedListBox2.Items.Count)

                checkBox1.Checked = true;
            else if (checkedListBox2.CheckedItems.Count != checkedListBox2.Items.Count)
                checkBox1.Checked = false;
}

但问题是,如果我检查了所有项目(选中了按钮select all),如果我点击了一个项目,则取消选择所有项目(并且取消选中按钮select all)。我想在点击一个项目时取消选择按钮Select all而不是所有项目?

编辑: 这是我的代码:

private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
{

            string installerfilename = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "installer.ini");
            IEnumerable<string> inilines = File.ReadAllLines(installerfilename).AsEnumerable();

           /* string selectedItem = checkedListBox2.SelectedItem.ToString();
            bool IsChecked = checkedListBox2.CheckedItems.Contains(selectedItem);*/
            bool IsChecked = 

    checkedListBox2.CheckedItems.Contains(checkedListBox2.SelectedItem);

                if (IsChecked)
                    inilines = inilines.Select(line => line == string.Format("#product={0}", checkedListBox2.SelectedItem)
                                                       ? Regex.Replace(line, string.Format("#product={0}", checkedListBox2.SelectedItem), string.Format(@"product={0}", checkedListBox2.SelectedItem))
                                                       : line);

                else
                    inilines = inilines.Select(line => (line == string.Format("product={0}", checkedListBox2.SelectedItem))
                                                       ? Regex.Replace(line, string.Format(@".*product={0}", checkedListBox2.SelectedItem), string.Format(@"#product={0}", checkedListBox2.SelectedItem))
                                                       : line);

                if (checkedListBox2.CheckedItems.Count == 0)
                    inilines = inilines.Select(line => Regex.Replace(line, @".*product=all", @"product=all"));
                else
                    inilines = inilines.Select(line => Regex.Replace(line, @".*product=all", @"#product=all"));


                if (checkedListBox2.CheckedItems.Count == checkedListBox2.Items.Count)           
                    checkBox1.Checked = true; 
                else if (checkedListBox2.CheckedItems.Count != checkedListBox2.Items.Count)
                    checkBox1.Checked = false;


                string strWrite = string.Join(Environment.NewLine, inilines.ToArray());
                File.WriteAllText(installerfilename, strWrite);

            }

     private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
            {

                string installerfilename = path + "installer.ini";
                string installertext = File.ReadAllText(installerfilename);
                var lin = File.ReadLines(path + "installer.ini").ToArray();

                CheckBox cb = sender as CheckBox;
                if ((cb.Checked) && (checkedListBox2.CheckedItems.Count != checkedListBox2.Items.Count))
                {
                                 //  checkBox1.Checked = false;
                    for (int i = 0; i < this.checkedListBox2.Items.Count; i++)
                        {
                            this.checkedListBox2.SetItemChecked(i, true)                    
                        }
                    foreach (var txt in lin)
                    {
                        if (txt.Contains("#product="))
                        {
                            // var name = txt.Split('=')[1];
                            installertext = installertext.Replace("#product=", "product=");
                        }
                        File.WriteAllText(installerfilename, installertext);
                    }
                }
                    else if ((!cb.Checked) && ((checkedListBox2.CheckedItems.Count != checkedListBox2.Items.Count) || (checkedListBox2.CheckedItems.Count == checkedListBox2.Items.Count)))
                    {

                        //checkBox1.Checked = false;
                        for (int i = 0; i < this.checkedListBox2.Items.Count; i++)
                        {
                            this.checkedListBox2.SetItemChecked(i, false);                            
                        }
                        foreach (var txt in lin)
                        {

                            if (txt.Contains("product=") && (!txt.StartsWith("#")))
                            {
                                // var name1 = txt.Split('=')[1];
                                installertext = installertext.Replace(txt, "#" +txt);
                            }
                            File.WriteAllText(installerfilename, installertext);
                        }
                    }

            }

1 个答案:

答案 0 :(得分:1)

您应该定义boolean字段,以确定用户是手动选中了CheckBox还是checkedListBox2_SelectedIndexChanged方法检查了它。然后,只有当此字段为true时,才会选中/取消选中所有项目。像下面的代码应该工作:

    bool _checkedManually = true;

    private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
    {
        if (!_checkedManually)
        {
            _checkedManually = true;
            return;
        }
        for (int i = 0; i < checkedListBox2.Items.Count; i++)
        {
            if (checkBox1.Checked)
                checkedListBox2.SetItemChecked(i, true);
            else
                checkedListBox2.SetItemChecked(i, false);
        }
    }

    private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        _checkedManually = false;

        if (checkedListBox2.CheckedItems.Count == checkedListBox2.Items.Count)
            checkBox1.Checked = true;
        else if (checkedListBox2.CheckedItems.Count != checkedListBox2.Items.Count)
            checkBox1.Checked = false;
    }

<强>更新

正如Rakesh在评论中提到的,只需将此行if(this.ActiveControl != sender ) return;添加到您的checkBox1_CheckedChanged_1方法中:

private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
            {

                string installerfilename = path + "installer.ini";
                string installertext = File.ReadAllText(installerfilename);
                var lin = File.ReadLines(path + "installer.ini").ToArray();

                if(this.ActiveControl != sender )
                    return;

                CheckBox cb = sender as CheckBox;
                if ((cb.Checked)
                {
                                 //  checkBox1.Checked = false;
                    for (int i = 0; i < this.checkedListBox2.Items.Count; i++)
                        {
                            this.checkedListBox2.SetItemChecked(i, true)                    
                        }
                    foreach (var txt in lin)
                    {
                        if (txt.Contains("#product="))
                        {
                            // var name = txt.Split('=')[1];
                            installertext = installertext.Replace("#product=", "product=");
                        }
                        File.WriteAllText(installerfilename, installertext);
                    }
                }
                else 
                {

                    //checkBox1.Checked = false;
                    for (int i = 0; i < this.checkedListBox2.Items.Count; i++)
                    {
                        this.checkedListBox2.SetItemChecked(i, false);                            
                    }
                    foreach (var txt in lin)
                    {

                        if (txt.Contains("product=") && (!txt.StartsWith("#")))
                        {
                            // var name1 = txt.Split('=')[1];
                            installertext = installertext.Replace(txt, "#" +txt);
                        }
                        File.WriteAllText(installerfilename, installertext);
                    }
                }

            }