如何用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、最终效果。

    

评论 32
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值