抽奖小程序的设计

csdn俱乐部的活动中准备了抽奖的小环节为了娱乐一下,写了一个小程序;

从txt文件中导入信息列表,并添加到listbox 控件中

OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "文本文件(*.txt)|*.txt";
            int i = 0;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                string recorde;
                while (sr.Peek() != -1)//循环写入
                {
                    recorde = sr.ReadLine();
                    if (recorde.Length > 0)
                    {
                        if (i < 10)
                        {
                            this.listBoxCollection.Items.Add("0" + i + "|" + recorde);
                        }
                        else
                        {
                            this.listBoxCollection.Items.Add(i + "|" + recorde);
                        }
                    }
                    i++;
                }
            }

private int[] GetRandom(int number)  //得到不重复的数组
        {
            int[] sArray = new int[number];
            Random ra = new Random();
            for (int i = 0; i < number; i++)
            {
                int  m=ra.Next(0,this.listBoxCollection.Items.Count);
                this.listBoxCollection.SelectedIndex = m;
                 Thread.Sleep(1200);

                int  mNum=0;
                foreach (Control lb in this.panelResult.Controls)
                {
                    if (lb.GetType().ToString().Trim() == "System.Windows.Forms.Button")
                    {
                        if (Convert.ToInt32(lb.Tag) == m)
                        {
                            mNum++;
                        }
                    }
                }
                for (int x = 0; x < i; x++)
                {
                    if (sArray[x] == m)
                    {
                        mNum++;
                    }
                }
                if (mNum > 0)
                {
                    i--;
                }
                else
                {
                    sArray[i] = m;
                }
            }
            return sArray;
        }

 

 

动态加入相就的按钮控件

int[] sArray = GetRandom(Convert.ToInt32(textBoxSum.Text));
                    Button[] lb = new Button[sArray.Length];
                    for (int n = 0; n < lb.Length; n++)  //根据选取随机数得到相应的值
                    {
                        lb[n] = new Button();
                        panelResult.Controls.Add(lb[n]);
                        if (colornum % 2 == 0)
                        {
                            lb[n].BackColor = Color.Brown;
                        }
                        else
                        {
                            lb[n].BackColor = Color.Black;
                        }
                        lb[n].ForeColor = Color.White;
                        lb[n].Text = GetLine(sArray[n]);
                        lb[n].Tag = sArray[n].ToString();
                        //lb[n].Font = new Font("宋体", 10F, System.Drawing.FontStyle.Regular);
                        lb[n].BringToFront();
                        lb[n].AutoSize = true;
                        lb[n].TextAlign = ContentAlignment.MiddleLeft;
                        lb[n].Left = 20;
                        lb[n].Top = (n + cursor) * 30;

                    }

 

 

由于有及时响应所以查询时间长了一些

下载网址   http://download.csdn.net/source/2190746

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lyflcear

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值