如何用C#中的Windows窗体写一个抽奖小程序。

一、使用工具。

         1.Visual Studio2017(我用的是2017,不去讨论其他版本了)。

          2.Windows窗体中的工具:Button、Label、Timer。

二、简单介绍三个工具的作用。

        1.Button:用来作按钮,抽奖的开始按钮。

         2.Label:用来显示参与抽奖的人员名单。

         3.Timer:控制参与抽奖人员的出现时间间隔。

三、具体的代码部分。

       1、

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;
using System.IO;

namespace choujiang
{
    public partial class 抽奖 : Form
    {
        static string path = "抽奖.txt";
        string[] content = File.ReadAllLines(path);
        public 抽奖()
        {
            InitializeComponent();
            timer1.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(button1.Text=="开始")
            {
                button1.Text = "结束";
                timer1.Start();
            }
            else
            {
                button1.Text = "开始";
                timer1.Stop();
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Random r = new Random();
            int i = r.Next(0,6);
            label1.Text = content[i];
        }
    }
}

四、解释部分。

       1、先在创建的项目文件目录先找到bin目录下的debug目录,在里面创建一个 "抽奖.txt"的文件,里面写入参与抽奖的名单。

       2、static string path = "抽奖.txt";
             string[] content = File.ReadAllLines(path);

              这段代码是导入这个抽奖.txt文件,并且把里面的每一行文字显示出来。

       3、if(button1.Text=="开始")
            {
                button1.Text = "结束";
                timer1.Start();
            }
            else
            {
                button1.Text = "开始";
                timer1.Stop();
            }

            用来实现点击按钮开始和点击按钮结束的作用。

       4、Random r = new Random();
            int i = r.Next(0,6);
            label1.Text = content[i];

            在Label中循环显示抽奖.txt中的文字。

       5、timer1.Start();
              程序的启动。

       6、最终效果。

     如何用C#中的Windows窗体写一个抽奖小程序。