winform 实现彩票功能

时间:2014-06-22 16:12:02   收藏:0   阅读:383

bubuko.com,布布扣

最近项目中要实现买彩票功能,功能已实现
不足之处请大神指教,学习进步
球的自定义控件:

<pre name="code" class="csharp">public partial class Ball : UserControl
    {
        
        /// <summary>
        /// 显示的数字
        /// </summary>
        private string _num;
        [RefreshProperties(RefreshProperties.Repaint)]
        [DefaultValue("")]
        public string Num
        {
            get { return _num; }
            set
            {
                this._num = value;
                this.btnBall.Text = value;
            }
        }

        /// <summary>
        /// 球类型
        /// </summary>
        private int ballType = 1;
        [Description("球类型 1 红球 2 蓝球")]
        [RefreshProperties(RefreshProperties.Repaint)]
        [DefaultValue("")]
        public int BallType
        {
            get { return ballType; }
            set { ballType = value; }
        }
        /// <summary>
        /// 背景图片
        /// </summary>
        private Image background = Resources.lottery_no;

        public Image Background
        {
            get { return background; }
            set
            {
                this.btnBall.BackgroundImage = value;
                background = value;
            }
        }

        /// <summary>
        /// 选中后的背景图片
        /// </summary>
        private Image selectedBackgroud;

        public Image SelectedBackgroud
        {
            get { return selectedBackgroud; }
            set
            {
                this.btnBall.BackgroundImage = value;
                selectedBackgroud = value;
            }
        }

        /// <summary>
        /// 球的选中状态
        /// </summary>
        private bool _isSelected = false;
        [Description("球是否选中")]
        [RefreshProperties(RefreshProperties.Repaint)]
        [DefaultValue("")]
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                _isSelected = value;
                setBallView(value);
            }
        }

        /**
         * 字体颜色,默认为黑色
         */
        private Color ballFontColor = Color.Black;

        public Color BallFontColor
        {
            get { return ballFontColor; }
            set
            {
                this.btnBall.ForeColor = value;
                ballFontColor = value;
            }
        }

        public delegate void stateUpdate(Ball obj);
        /// <summary>
        /// 状态更新事件
        /// </summary>
        public event stateUpdate stateChange;
        
        public Ball()
        {
            InitializeComponent();
            this.Load += new EventHandler(Ball_Load);
            this.btnBall.Click += new EventHandler(btnBall_Click);
        }
        private void setBallView(bool isTrue)
        {
            if (isTrue)
            {
                if (BallType == 1)
                {
                    SelectedBackgroud = Resources.lottery_red;
                    BallFontColor = Color.White;
                }
                else
                {
                    SelectedBackgroud = Resources.lottery_blue;
                    BallFontColor = Color.White;
                }
            }
            else
            {
                if (BallType == 1)
                {
                    SelectedBackgroud = Resources.lottery_no;
                    BallFontColor = Color.Red;
                }
                else
                {
                    SelectedBackgroud = Resources.lottery_no;
                    BallFontColor = Color.RoyalBlue;
                }
            }
        }
        /// <summary>
        /// 点击球事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnBall_Click(object sender, EventArgs e)
        {
            if (!IsSelected)
            {
                if (BallType == 1)
                {
                    SelectedBackgroud = Resources.lottery_red;
                    BallFontColor = Color.White;
                }
                else
                {
                    SelectedBackgroud = Resources.lottery_blue;
                    BallFontColor = Color.White;
                }
                IsSelected = true;
            }
            else
            {
                if (BallType == 1)
                {
                    SelectedBackgroud = Resources.lottery_no;
                    BallFontColor = Color.Red;
                }
                else
                {
                    SelectedBackgroud = Resources.lottery_no;
                    BallFontColor = Color.RoyalBlue;
                }
                IsSelected = false;
            }
            this.stateChange(this);
        }

        void Ball_Load(object sender, EventArgs e)
        {
            if (BallType == 1)
            {
                BallFontColor = Color.Red;
            }
            else
            {
                BallFontColor = Color.RoyalBlue;
            }
            this.Background = Resources.lottery_no;
        }
    }
2.填充球集合

/// <summary>
        /// 常规红球集合
        /// </summary>
        private List<Ball> cRedBall = new List<Ball>();
foreach (Control item in pan_cg.Controls)
            {
                if (item is Ball)
                {
                    if (item.Tag == null)
                        continue;
                    (item as Ball).stateChange += new Ball.stateUpdate(LotteryDoubleBallForm_stateChange);
                    switch (item.Tag.ToString())
                    {
                        case "1":
                            cRedBall.Add(item as Ball);
                            break;
                        case "2":
                            cBlueBall.Add(item as Ball);
                            break;
                    }
                }
            }
3、选择球触发事件
void LotteryDoubleBallForm_stateChange(Ball obj)
        {
            switch (obj.Tag.ToString())
            {
                case "1"://常规红球
                    if (obj.IsSelected)
                    {
                        if (!this.SelectedcRedBall.Contains(obj))
                        {
                            this.SelectedcRedBall.Add(obj);
                        }
                    }
                    else
                    {
                        if (this.SelectedcRedBall.Contains(obj))
                        {
                            this.SelectedcRedBall.Remove(obj);
                        }
                    }
                    break;
                case "2":
                    if (obj.IsSelected)
                    {
                        if (!this.SelectedcBlueBall.Contains(obj))
                        {
                            this.SelectedcBlueBall.Add(obj);
                        }
                    }
                    else
                    {
                        if (this.SelectedcBlueBall.Contains(obj))
                        {
                            this.SelectedcBlueBall.Remove(obj);
                        }
                    }
                    break;
}
}

4机选红球

private void RamcRed(int count)
        {
            for (int i = 0; i < count; i++)
            {
                bool bl = true;
                int ranIndex = 0;
                while (bl)
                {
                    Random ran = new Random();
                    ranIndex = ran.Next(1, 33);
                    Ball ball = this.cRedBall[ranIndex];
                    if (!this.SelectedcRedBall.Contains(ball))
                    {
                        this.SelectedcRedBall.Add(ball);
                        ball.IsSelected = true;
                        bl = false;
                    }
                    else
                    {
                        bl = true;
                    }
                }
            }
        }



winform 实现彩票功能,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!