如何使用复选框 winform c# 创建序列
How to create sequence using checkbox winform c#
Three Checkbox
in Winform 如果我选中 third 复选框,则返回为 3
如果我选中 second checkbox 返回为 2
如果 first checkbox 未选中,则返回 0 最后获取序列 [3,2,0]
例如,如果第二个 checked box 未选中,第一个 checkbox 被选中它作为序列 [3,0,1]
返回
让我们将复选框组织到一个集合中,比如说,一个数组,Tuple<CheckBox, int>[]
:
// Check box and its corresponding value
private Tuple<CheckBox, int>[] map => new Tuple<CheckBox, int>[] {
Tuple.Create(checkBox1, 3),
Tuple.Create(checkBox2, 2),
Tuple.Create(checkBox3, 1),
};
// Now we can query the collection with a help of Linq
private int[] mapBoxes() => map
.Select(pair => pair.Item1.Checked ? pair.Item2 : 0)
.ToArray();
...
int[] values = mapBoxes();
因此,您希望第三个复选框位于数组中的第一个并在 3 和 0 之间切换,第二个复选框在数组中位于第二个,在 2 和 0 之间切换,第一个复选框在数组中位于第三个并在 1 和 0 之间切换:
int[] cb = new int[]{
thirdCheckbox.Checked?3:0,
secondCheckbox.Checked?2:0,
firstCheckbox.Checked?1:0
};
Three Checkbox
in Winform 如果我选中 third 复选框,则返回为 3
如果我选中 second checkbox 返回为 2
如果 first checkbox 未选中,则返回 0 最后获取序列 [3,2,0]
例如,如果第二个 checked box 未选中,第一个 checkbox 被选中它作为序列 [3,0,1]
让我们将复选框组织到一个集合中,比如说,一个数组,Tuple<CheckBox, int>[]
:
// Check box and its corresponding value
private Tuple<CheckBox, int>[] map => new Tuple<CheckBox, int>[] {
Tuple.Create(checkBox1, 3),
Tuple.Create(checkBox2, 2),
Tuple.Create(checkBox3, 1),
};
// Now we can query the collection with a help of Linq
private int[] mapBoxes() => map
.Select(pair => pair.Item1.Checked ? pair.Item2 : 0)
.ToArray();
...
int[] values = mapBoxes();
因此,您希望第三个复选框位于数组中的第一个并在 3 和 0 之间切换,第二个复选框在数组中位于第二个,在 2 和 0 之间切换,第一个复选框在数组中位于第三个并在 1 和 0 之间切换:
int[] cb = new int[]{
thirdCheckbox.Checked?3:0,
secondCheckbox.Checked?2:0,
firstCheckbox.Checked?1:0
};