gpt4 book ai didi

c# - 如何在checkedlistbox c# winforms中检查范围?

转载 作者:行者123 更新时间:2023-12-04 09:05:44 27 4
gpt4 key购买 nike

我有一个包含 100 个项目的复选框列表。显然,用户可以根据需要一一检查项目,但我想给用户选项检查项目范围(假设使用 Shift 按住按钮)。因此,用户检查其中一个项目(假设项目索引 5),然后按住 shift 按钮并检查下一个项目(索引 10),因此我应该检查项目的范围从 5...10
我没有发现任何关于这种实现的东西,看起来它不存在,也没有人做过这样的事情。
怎么做?

最佳答案

跟踪您的最后一个索引:

int lastIndex = -1;
在表单的构造函数中,将事情连接起来:
public Form1() {
InitializeComponent();

checkedListBox1.CheckOnClick = true;
checkedListBox1.SelectedIndexChanged += CheckedListBox1_SelectedIndexChanged;
checkedListBox1.MouseDown += CheckedListBox1_MouseDown;
}
然后使用这些方法更改范围内的项目:
private void CheckedListBox1_SelectedIndexChanged(object sender, EventArgs e) {
lastIndex = checkedListBox1.SelectedIndex;
}

private void CheckedListBox1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left && Control.ModifierKeys == Keys.Shift) {
var useIndex = Math.Max(lastIndex, 0);
var x = checkedListBox1.IndexFromPoint(e.Location);
if (x > -1 && x != useIndex) {
if (useIndex > x) {
for (int i = useIndex - 1; i > x; i--) {
checkedListBox1.SetItemChecked(i, !checkedListBox1.GetItemChecked(i));
}
} else {
for (int i = useIndex + 1; i < x; i++) {
checkedListBox1.SetItemChecked(i, !checkedListBox1.GetItemChecked(i));
}
}
}
}
}

关于c# - 如何在checkedlistbox c# winforms中检查范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63452545/

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