目录
1.全选,反选
思路:
全选: 当你一个一个把选项全部点亮时,全选选项也会亮
反选: 点击按钮后所有选项内容反过来
本文用到:
CheckBox(复选框)、Panel(容器控件)用来隔绝其他内容
winform窗口name属性
checkBox1 = 红双喜 checkBox2 = 白利 checkBox3 = 加多宝 checkBox4 = 喜之郎
checkBox5 = 压缩饼干 checkBox6 = 全选/取消全选 checkBox7 = 反选
源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
//创建一个CheckBox(多选)的集合
private CheckBox[] checkBoxes;
public Form1()
{
InitializeComponent();
//往集合里添加多选控件
checkBoxes = new CheckBox[] { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5 };
}
private void checkBox6_CheckedChanged(object sender, EventArgs e)
{
//如果全选亮起,则以上五个选项亮起
if (checkBox6.Checked == true)
{
//遍历全部多选控件
foreach (CheckBox checkBox in this.checkBoxes)
{
checkBox.Checked = checkBox6.Checked;
}
}
//当以上五个选项全部亮起并且全选等于false的时候遍历全部多选控件让他们等于false
//检查是否所有复选框都被选中。如果是,则进入条件判断中。
if (!checkBoxes.Any(checkBox => !checkBox.Checked))
{
//检查全选复选框 checkBox6.Checked 是否为 false,如果是,则将所有复选框的状态设置为全选复选框的状态。
if (!checkBox6.Checked)
{
foreach (CheckBox checkBox in checkBoxes)
{
checkBox.Checked = checkBox6.Checked;
}
}
}
}
private void checkBox7_CheckedChanged(object sender, EventArgs e)
{
//遍历工具集合
//把五个选项全部赋予他们的反值
foreach (CheckBox checkBox in this.checkBoxes)
{
checkBox.Checked = !checkBox.Checked;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
//引用UpdateSelectAllCheckBox方法
UpdateSelectAllCheckBox();
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
UpdateSelectAllCheckBox();
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
UpdateSelectAllCheckBox();
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
UpdateSelectAllCheckBox();
}
private void checkBox5_CheckedChanged(object sender, EventArgs e)
{
UpdateSelectAllCheckBox();
}
//封装
public void UpdateSelectAllCheckBox()
{
//当你一个一个把选项全部点亮时,全选选项也会亮
//当你有一个选项不亮时,全选选项就不会亮
// 使用 LINQ 的 All 方法检查复选框集合中的所有元素是否被选中
bool allChecked = checkBoxes.All(checkBox => checkBox.Checked);
checkBox6.Checked = allChecked;
}
}
}
2.两个ListBox中的内容左右相互移动
思路:
先把左边选中的内容添加到右边,再把选中的内容删除,右边同理
运行效果:
点击添加到购物车按钮后:
点击从购物车删除效果相反
本文用到:
两个botton按钮,两个ListBox列表框
winform窗口
listBox1 = listBox1 shopping = shopping 添加到购物车 = addition 从购物车删除 = delete
源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//在listbox中添加元素
listBox1.Items.Add("白酒");
listBox1.Items.Add("红酒");
listBox1.Items.Add("葡萄酒");
listBox1.Items.Add("幺幺九");
}
private void addition_Click(object sender, EventArgs e)
{
//调用方法A
A(listBox1,shopping);
}
private void delete_Click(object sender, EventArgs e)
{
A(shopping,listBox1);
}
//封装方法
public void A(ListBox listBox1, ListBox shopping)
{
//把你选中的选项添加到shopping框里
foreach (var selectedItem in listBox1.SelectedItems)
{
shopping.Items.Add(selectedItem);
}
//创建一个叫lists的用来存储索引的集合
List<int> lists = new List<int>();
//把你选中的选项遍历进去
foreach (int selectedIndex in listBox1.SelectedIndices)
{
//把你要选中选项的索引添加进去
lists.Add(selectedIndex);
}
//按索引逆序删除选中项
for (int i = lists.Count - 1; i >= 0; i--)
{
listBox1.Items.RemoveAt(lists[i]);
}
}
}
}
3. 部门添加
运行效果:
输入姓名后点击添加按钮
本文用到:
botton按钮,comboBox下拉框,label标签,panel容器。
winform窗口
源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//添加数组
comboBox1.Items.AddRange(new string[] { "销售部", "人力资源部", "技术部" });
comboBox3.Items.AddRange(