项目需求:

当winform界面上存在多个按钮时(大于2个),用户需求为当点击其中一个按钮后,其它按钮全部为禁用,当被点击的按钮后台逻辑执行完成后,再释放所有按钮。用户可再次点击其它按钮。

此案例多用于多线程,防止线程未执行完,再次执行线程

后台示例代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            btns = new Button[] { button1,button2,button3,button4,button5,button6,button7,button8 }; //将所需锁住的按钮名称放置在此集合中
        }
        Button[] btns = null;
        private void button1_Click(object sender, EventArgs e) //单击按钮
        {
            Button_Close();
            Thread.Sleep(3000); //模拟延时
            Button_Open();    
        }
        private void Button_Close() //锁住集合内全部数组
        {
            foreach(Button button in btns)
            {
                button.Enabled = false;
            }
        }
        private void Button_Open() //打开集合内全部数组
        {
            foreach (Button button in btns)
            {
                button.Enabled = true;
            }
        }
    }
}

前台界面如下图所示:

C# 模拟button按钮批量锁住与打开-LMLPHP

运行后界面:

C# 模拟button按钮批量锁住与打开-LMLPHP

所有按钮全部被锁住,3s后释放

09-17 18:25