gpt4 book ai didi

c# - 如何取消选择按钮 `Select all` 而不是所有项目?

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:20 24 4
gpt4 key购买 nike

我有一个复选框,用于从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;
}

但问题是,如果我检查了所有项目(按钮 全选 被选中)并且如果我点击一个项目,所有项目都被取消选择(并且按钮 全选 未选中)。我希望当我单击一个项目时只取消选择按钮全选而不是所有项目?

编辑:这是我的代码:

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

}

最佳答案

您应该定义一个 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);
}
}

}

关于c# - 如何取消选择按钮 `Select all` 而不是所有项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32858746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com