计算机抽奖系统

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 
 11 namespace WindowsFormsApplication1
 12 {
 13     public partial class Form1 : Form
 14     {
 15         public Form1()
 16         {
 17             InitializeComponent();
 18         }
 19 
 20         private void Form1_Load(object sender, EventArgs e)
 21         {
 22             //构建网格布局
 23             BuildLsyout();
 24             //向网格布局中加按钮
 25             AddButtonToTableLayout();
 26         }
 27         private void BuildLsyout()
 28         {
 29             //清除原先的列
 30             tableLayoutPanel1.ColumnStyles.Clear();
 31             //添加新列
 32             tableLayoutPanel1.ColumnCount = 20;
 33             for (int i = 0; i < 20; i++)
 34             {
 35                 ColumnStyle style = new ColumnStyle(SizeType.Percent,5);
 36                 tableLayoutPanel1.ColumnStyles.Add(style);
 37             }
 38             //清除原先的行
 39             tableLayoutPanel1.RowStyles.Clear();
 40             //添加新行
 41             tableLayoutPanel1.RowCount = 20;
 42             for (int i = 0; i < 20; i++)
 43             {
 44                 ColumnStyle style = new ColumnStyle(SizeType.Percent,5);
 45                 tableLayoutPanel1.RowStyles.Add(style);
 46             }
 47         }//构建网格布局
 48         private void AddButtonToTableLayout() //向网格布局中加按钮
 49         {
 50             Random rand = new Random();
 51             for (int i = 0; i < 400; i++)
 52             {
 53                 Button bot = new Button();
 54                 bot.Text = i.ToString();
 55                 bot.Dock = DockStyle.Fill;
 56                 tableLayoutPanel1.Controls.Add(bot);
 57                 if (rand.Next(100) % 30 == 0)
 58                 {
 59                     bot.Tag = 1;
 60                 }
 61                 else if (rand.Next(100) % 50 == 0)
 62                 {
 63                     bot.Tag = 10;
 64                 }
 65                 else if (rand.Next(100) % 100 == 0)
 66                 {
 67                     bot.Tag = 100;
 68                 }
 69                 else if (rand.Next(100) % 200 == 0)
 70                 {
 71                     bot.Tag = 500;
 72                 }
 73                 else if (rand.Next(100) % 500 == 0)
 74                 {
 75                     bot.Tag = 1000;
 76                 }
 77                 else
 78                 {
 79                     bot.Tag = 0;
 80                 }
 81                 //初始化按钮的事件
 82                 bot.Click += bot_Click;
 83 
 84             }
 85 
 86         }
 87         void bot_Click(object sender, EventArgs e)
 88         {
 89             Button bot = sender as Button;
 90             int n =(int)bot.Tag;
 91             if (n!=0)
 92             {
 93                 bot.BackColor = Color.Red;
 94                 bot.ForeColor = Color.Blue;
 95                 bot.Text = n.ToString();
 96                 bot.Font = new Font("微软雅黑", 12, FontStyle.Bold);
 97             }
 98             bot.Enabled = false;
 99         }   //按键
100     }
101 }
View Code
原文地址:https://www.cnblogs.com/m123/p/4511223.html