C#WinForm案例(持续更新)

这篇博客详细介绍了C# WinForm应用的各种实战案例,包括全选/反选功能,ListBox内容移动,部门添加,轮播图实现,menuStrip菜单栏设计,2048游戏,番茄钟功能以及视频播放器的开发。每个案例都提供了运行效果展示及源码,帮助读者深入理解WinForm编程。
摘要由CSDN通过智能技术生成

目录

1.全选,反选

源码:

2.两个ListBox中的内容左右相互移动

运行效果:

 点击添加到购物车按钮后:

 源码:

3. 部门添加

运行效果:

  源码:

4. 轮播图

运行效果:

   源码:

5. menuStrip菜单栏

 Form1源码:

 ChildForm源码:

 Puchi源码:

6.2048纯手写代码(放代码里就能用,无需添加控件)

7.番茄钟

运行效果:

 源码:

8.视频播放器

源码 


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(new string[] { "男", "女" });

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //每次选择之前先清除comboBox2里的内容
            comboBox2.Items.Clear();
            //获取选择内容的索引
            int selectedIndex 
  • 32
    点赞
  • 136
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值