首页

C# 随机抽奖程序源码

c#

2020-9-5

可以手动维护人员列表,中奖人数,随机进行抽奖等功能

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

namespace LuckyDraw
{
    public partial class Form1 : Form
    {
        System.Threading.Timer timer;
        public Form1()
        {
            InitializeComponent();
        }

        private void button_start_Click(object sender, EventArgs e)
        {
            var emplList = textBox_empl_list.Text;
            int firstIntLines = this.richTextBox_firstWinning_list.GetLineFromCharIndex(this.richTextBox_firstWinning_list.TextLength)   1;
            int secondIntLines = this.richTextBox_secondWinning_list.GetLineFromCharIndex(this.richTextBox_secondWinning_list.TextLength)   1;
            if (textBox_firstWinning_num.Text != "" && Convert.ToInt64(textBox_firstWinning_num.Text) >= firstIntLines)
            {
                button_stop.Visible = true;
                if (emplList != "")
                {
                    button_start.Visible = false;
                    timer = new System.Threading.Timer(new System.Threading.TimerCallback(timer_tick), null, 100, 100);
                    timer.Change(0, 50);
                }
            }
            else if (textBox_secondWinning_num.Text != "" && Convert.ToInt64(textBox_secondWinning_num.Text) >= secondIntLines)
            {
                button_secondStop.Visible = true;
                if (emplList != "")
                {
                    button_start.Visible = false;
                    timer = new System.Threading.Timer(new System.Threading.TimerCallback(timer_tick), null, 100, 100);
                    timer.Change(0, 50);
                }
            }
            else if(textBox_firstWinning_num.Text != "" && textBox_secondWinning_num.Text != "")
            {
                button_start.Visible = false;
                button_reset.Visible = true;
            }
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            label_empl_list.Visible = false;
            textBox_empl_list.Visible = false;
            label_firstWinning_num.Visible = false;
            textBox_firstWinning_num.Visible = false;
            label_secondWinning_num.Visible = false;
            textBox_secondWinning_num.Visible = false;
            //textBox_num.Visible = false;
            button_save.Visible = false;
        }

        private void linkLabel_admin_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            label_empl_list.Visible = true;
            textBox_empl_list.Visible = true;
            label_firstWinning_num.Visible = true;
            textBox_firstWinning_num.Visible = true;
            //label_secondWinning_num.Visible = true;
            //textBox_secondWinning_num.Visible = true;
            //textBox_num.Visible = true;
            button_save.Visible = true;
        }

        private void button_stop_Click(object sender, EventArgs e)
        {
            var LuckyDrawEmpl = stop("1");
            int intLines = this.richTextBox_firstWinning_list.GetLineFromCharIndex(this.richTextBox_firstWinning_list.TextLength)   1;
            if (LuckyDrawEmpl != "" && textBox_firstWinning_num.Text != "")
            {
                var firstWinningNum = Convert.ToInt64(textBox_firstWinning_num.Text);

                if (firstWinningNum >= intLines)
                {
                    this.richTextBox_firstWinning_list.AppendText(string.Format("{0}\r", LuckyDrawEmpl));
                    if (firstWinningNum == intLines)
                    {
                        timer_stop();
                        label_show.Invoke(new Action<string>((str) => { label_show.Text = str; }), LuckyDrawEmpl);
                        button_start.Visible = true;
                        button_stop.Visible = false;
                    }
                }
            }
        }
        private void button_secondStop_Click(object sender, EventArgs e)
        {
            var LuckyDrawEmpl = stop("2");
            int intLines = this.richTextBox_secondWinning_list.GetLineFromCharIndex(this.richTextBox_secondWinning_list.TextLength)   1;
            if (LuckyDrawEmpl != "" && textBox_secondWinning_num.Text != "")
            {
                var secondWinningNum = Convert.ToInt64(textBox_secondWinning_num.Text);
                if (secondWinningNum >= intLines)
                {
                    this.richTextBox_secondWinning_list.AppendText(string.Format("{0}\r", LuckyDrawEmpl));
                    if (secondWinningNum == intLines)
                    {
                        timer_stop();
                        button_start.Visible = true;
                        button_secondStop.Visible = false;
                    }
                }
            }
        }
        private void timer_stop()
        {
            timer.Change(-1, 0);
        }
        // 定时任务
        public void timer_tick(object test)
        {
            int num = -1;
            Thread thread = new Thread(new ThreadStart(delegate
            {
                var LuckyDrawEmpl = LuckyDrawEmplStart(ref num);
                // System.InvalidOperationException:“线程间操作无效: 从不是创建控件“label_show
                // label_show.Text = LuckyDrawEmpl.ToString(); === label_show.Invoke(new Action<string>((str) => { label_show.Text = str; }), LuckyDrawEmpl.ToString());
                label_show.Invoke(new Action<string>((str) => { label_show.Text = str; }), LuckyDrawEmpl.ToString());
            }));
            thread.Start();
        }
        // 开始滚动
        private string LuckyDrawEmplStart(ref int num)
        {
            var LuckyDrawEmpl = "";
            var emplList = textBox_empl_list.Text;
            if (emplList != "")
            {
                ArrayList emplArrayList = new ArrayList(emplList.Split(','));
                var emplNum = emplArrayList.Count;
                if (emplNum == 0)
                {
                    timer_stop();
                }
                else
                {
                    var rd = new Random();
                    var rdChange = rd.Next(0, emplNum);
                    num = rdChange;
                    LuckyDrawEmpl = emplArrayList[rdChange].ToString();
                    //textBox_num.Invoke(new Action<string>((str) => { textBox_num.Text = str; }), emplNum.ToString());
                }
            }
            return LuckyDrawEmpl;
        }
        private string stop(string type)
        {
            int num = -1;
            bool isDelete = false;
            var LuckyDrawEmpl = LuckyDrawEmplStart(ref num);
            if ((type == "1" && textBox_firstWinning_num.Text != "") || (type == "2" && textBox_secondWinning_num.Text != ""))
            {
                isDelete = true;
            }
            if (LuckyDrawEmpl != "" && isDelete == true)
            {
                var emplList = textBox_empl_list.Text;
                ArrayList emplArrayList = new ArrayList(emplList.Split(','));
                emplArrayList.RemoveAt(num);
                textBox_empl_list.Text = string.Join(",", (string[])emplArrayList.ToArray(typeof(string)));
            }
            return LuckyDrawEmpl;
        }

        private void button_reset_Click(object sender, EventArgs e)
        {
            button_start.Visible = true;
            this.richTextBox_firstWinning_list.Text = "";
            this.richTextBox_secondWinning_list.Text = "";
            textBox_empl_list.Text = "施文-13968869760,郑武-17858767257,刘苏云-13262372512,胡双江-19883712079,陈毅-15958737113,陈志翔-18757723286,袁琦琪-18569513679,刘建树-13777702652,胡允乐-15957700640,李海峰-13566280322,宋伟辉-13968838283,聂兴中-15558977257,陈晓骏-18357720719,王子浩-13676413362,郑黄彬-15356210332,黄诚-13957775791,徐冠业-18815047455,汤显隆-13968955249,毛前程-18958891939,包林洁-15858845199,李伟平-15990472659,周嘉伟-17769525337,陈培芳-13777757035,李红水-13017812837,何广献-13858877932,林文文-18367823102,张建武-15968736180,应玲玲-15167862506,陈瓯阳-13777702297,周艳丽-13587479826,杨岳乾-15957644119,陈方园-13757728569,金通县-15258722188,洪丽莹-15258671918,唐玮玮-13738715006,刘荣恒-18968935893,王健-15325875066,钟备路-17857053645,汪茜茜-13819752095,林传国-13456918581,陈少海-13456050829,黄赛武-15158715988,王雪磊-13758454594,吴宁-15959303050,陈昕光-13957756151,姜楠-13886613102,陈祥塔-18458477786,周九江-15858791210,林文如-13296706188,王杰-18757094550,陈宜枫-15658667890,周晓东-15957713423,缪佳佳-13819748015,黄飞-13736713873,吕建升-13676729255,黄红亮-15858511315,朱晟-13738709813,南海舟-13758460100,南茜玄-13626531506,南海蓉-13858822480";
            textBox_firstWinning_num.Text = "1";
            textBox_secondWinning_num.Text = "";
        }

        private void button_reset2_Click(object sender, EventArgs e)
        {
            button_start.Visible = true;
            this.richTextBox_firstWinning_list.Text = "";
            this.richTextBox_secondWinning_list.Text = "";
            textBox_empl_list.Text = "施文-13968869760,郑武-17858767257,刘苏云-13262372512,胡双江-19883712079,陈毅-15958737113,陈志翔-18757723286,袁琦琪-18569513679,刘建树-13777702652,胡允乐-15957700640,李海峰-13566280322,宋伟辉-13968838283,聂兴中-15558977257,陈晓骏-18357720719,王子浩-13676413362,郑黄彬-15356210332,黄诚-13957775791,徐冠业-18815047455,汤显隆-13968955249,毛前程-18958891939,包林洁-15858845199,李伟平-15990472659,周嘉伟-17769525337,陈培芳-13777757035,李红水-13017812837,何广献-13858877932,林文文-18367823102,张建武-15968736180,应玲玲-15167862506,陈瓯阳-13777702297,周艳丽-13587479826,杨岳乾-15957644119,陈方园-13757728569,金通县-15258722188,洪丽莹-15258671918,唐玮玮-13738715006,刘荣恒-18968935893,王健-15325875066,钟备路-17857053645,汪茜茜-13819752095,林传国-13456918581,陈少海-13456050829,黄赛武-15158715988,王雪磊-13758454594,吴宁-15959303050,陈昕光-13957756151,姜楠-13886613102,陈祥塔-18458477786,周九江-15858791210,林文如-13296706188,王杰-18757094550,陈宜枫-15658667890,周晓东-15957713423,缪佳佳-13819748015,黄飞-13736713873,吕建升-13676729255,黄红亮-15858511315,朱晟-13738709813,南海舟-13758460100,南茜玄-13626531506,南海蓉-13858822480";
            textBox_firstWinning_num.Text = "1";
            textBox_secondWinning_num.Text = "";
        }

        private void textBox_secondWinning_num_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
抽奖/.vs/抽奖/v15/.suo , 52736
抽奖/.vs/抽奖/v15/Server/sqlite3/db.lock , 0
抽奖/.vs/抽奖/v15/Server/sqlite3/storage.ide , 618496
抽奖/.vs/抽奖/v15/Server/sqlite3/storage.ide-shm , 32768
抽奖/.vs/抽奖/v15/Server/sqlite3/storage.ide-wal , 4132392
抽奖/LuckyDraw/App.config , 187
抽奖/LuckyDraw/bin/Debug/app.publish/LuckyDraw.exe , 824320
抽奖/LuckyDraw/bin/Debug/LuckyDraw.application , 1827
抽奖/LuckyDraw/bin/Debug/LuckyDraw.exe , 823296
抽奖/LuckyDraw/bin/Debug/LuckyDraw.exe.config , 187
抽奖/LuckyDraw/bin/Debug/LuckyDraw.exe.manifest , 4103
抽奖/LuckyDraw/bin/Debug/LuckyDraw.pdb , 38400
抽奖/LuckyDraw/bin/Debug/Newtonsoft.Json.dll , 700336
抽奖/LuckyDraw/bin/Debug/Newtonsoft.Json.xml , 707721
抽奖/LuckyDraw/Core/EmplListEntity.cs , 207
抽奖/LuckyDraw/Form1.cs , 11104
抽奖/LuckyDraw/Form1.Designer.cs , 19062
抽奖/LuckyDraw/Form1.resx , 1212690
抽奖/LuckyDraw/LuckyDraw.csproj , 5668
抽奖/LuckyDraw/LuckyDraw.csproj.user , 554
抽奖/LuckyDraw/LuckyDraw_TemporaryKey.pfx , 1716
抽奖/LuckyDraw/obj/Debug/DesignTimeResolveAssemblyReferences.cache , 827
抽奖/LuckyDraw/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 7465
抽奖/LuckyDraw/obj/Debug/LuckyDraw.application , 1827
抽奖/LuckyDraw/obj/Debug/LuckyDraw.csproj.CopyComplete , 0
抽奖/LuckyDraw/obj/Debug/LuckyDraw.csproj.CoreCompileInputs.cache , 42
抽奖/LuckyDraw/obj/Debug/LuckyDraw.csproj.FileListAbsolute.txt , 1581
抽奖/LuckyDraw/obj/Debug/LuckyDraw.csproj.GenerateResource.cache , 1012
抽奖/LuckyDraw/obj/Debug/LuckyDraw.csprojAssemblyReference.cache , 50641
抽奖/LuckyDraw/obj/Debug/LuckyDraw.exe , 823296
抽奖/LuckyDraw/obj/Debug/LuckyDraw.exe.manifest , 4103
抽奖/LuckyDraw/obj/Debug/LuckyDraw.Form1.resources , 805303
抽奖/LuckyDraw/obj/Debug/LuckyDraw.pdb , 38400
抽奖/LuckyDraw/obj/Debug/LuckyDraw.Properties.Resources.resources , 180
抽奖/LuckyDraw/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs , 0
抽奖/LuckyDraw/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs , 0
抽奖/LuckyDraw/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs , 0
抽奖/LuckyDraw/packages.config , 143
抽奖/LuckyDraw/Program.cs , 521
抽奖/LuckyDraw/Properties/AssemblyInfo.cs , 1330
抽奖/LuckyDraw/Properties/Resources.Designer.cs , 2831
抽奖/LuckyDraw/Properties/Resources.resx , 5612
抽奖/LuckyDraw/Properties/Settings.Designer.cs , 1096
抽奖/LuckyDraw/Properties/Settings.settings , 249
抽奖/packages/Newtonsoft.Json.12.0.3/.signature.p7s , 18492
抽奖/packages/Newtonsoft.Json.12.0.3/lib/net20/Newtonsoft.Json.dll , 570792
抽奖/packages/Newtonsoft.Json.12.0.3/lib/net20/Newtonsoft.Json.xml , 604413
抽奖/packages/Newtonsoft.Json.12.0.3/lib/net35/Newtonsoft.Json.dll , 505776
抽奖/packages/Newtonsoft.Json.12.0.3/lib/net35/Newtonsoft.Json.xml , 549505
抽奖/packages/Newtonsoft.Json.12.0.3/lib/net40/Newtonsoft.Json.dll , 574376
抽奖/packages/Newtonsoft.Json.12.0.3/lib/net40/Newtonsoft.Json.xml , 561424
抽奖/packages/Newtonsoft.Json.12.0.3/lib/net45/Newtonsoft.Json.dll , 700336
抽奖/packages/Newtonsoft.Json.12.0.3/lib/net45/Newtonsoft.Json.xml , 707721
抽奖/packages/Newtonsoft.Json.12.0.3/lib/netstandard1.0/Newtonsoft.Json.dll , 669104
抽奖/packages/Newtonsoft.Json.12.0.3/lib/netstandard1.0/Newtonsoft.Json.xml , 686366
抽奖/packages/Newtonsoft.Json.12.0.3/lib/netstandard1.3/Newtonsoft.Json.dll , 688040
抽奖/packages/Newtonsoft.Json.12.0.3/lib/netstandard1.3/Newtonsoft.Json.xml , 694572
抽奖/packages/Newtonsoft.Json.12.0.3/lib/netstandard2.0/Newtonsoft.Json.dll , 693680
抽奖/packages/Newtonsoft.Json.12.0.3/lib/netstandard2.0/Newtonsoft.Json.xml , 706433
抽奖/packages/Newtonsoft.Json.12.0.3/lib/portable-net40+sl5+win8+wp8+wpa81/Newtonsoft.Json.dll , 468912
抽奖/packages/Newtonsoft.Json.12.0.3/lib/portable-net40+sl5+win8+wp8+wpa81/Newtonsoft.Json.xml , 519414
抽奖/packages/Newtonsoft.Json.12.0.3/lib/portable-net45+win8+wp8+wpa81/Newtonsoft.Json.dll , 668584
抽奖/packages/Newtonsoft.Json.12.0.3/lib/portable-net45+win8+wp8+wpa81/Newtonsoft.Json.xml , 686366
抽奖/packages/Newtonsoft.Json.12.0.3/LICENSE.md , 1104
抽奖/packages/Newtonsoft.Json.12.0.3/Newtonsoft.Json.12.0.3.nupkg , 2596051
抽奖/packages/Newtonsoft.Json.12.0.3/packageIcon.png , 8956
抽奖/抽奖.sln , 1126
没有账号? 忘记密码?

社交账号快速登录