C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C#彩票选号

C#项目彩票选号实现思路

作者:郭恩硕

今天做了一个彩票选号的小软件,将学到的知识点总结一下,下面通过本文给大家分享C#项目彩票选号实现思路,感兴趣的朋友跟随小编一起看看吧

C#彩票选号软件

今天做了一个彩票选号的小软件,将学到的知识点总结如下;

1.写程序的思路

2.学到的知识点

3.经验技巧

4.UI界面

5.代码实现

【实体类】

//属性-临时号码(用于存储随机号及数组的)
 public List<string[]> selectNum { get; set; }
 private Random randomNum = new Random();
 //构造方法-创建临时号码对象
 public Selector()
 {
    this.selectNum = new List<string[]>();
 }
 //实例方法-随机选7个号
 public string[] selectNumArray()
 {
     string[] ranNum7 = new string[7];
     for (int i = 0; i < 7; i++)
     {
         ranNum7[i] = this.randomNum.Next(10).ToString();
     }
     return ranNum7;
 }
//实例方法-随机选n组号
public void selectNumGroup(int groupNum)
{
    for (int i = 0; i < groupNum; i++)
    {
        selectNum.Add(selectNumArray());
    }
}
  //实例方法-号码列表
  public List<string> listNum()
  {
      List<string> listData = new List<string>();
      string listName;
      for (int i = 0; i < this.selectNum.Count; i++)
      {
          if (i < 9) { listName = "第0" + (i + 1) + "组:  "; }
          else {listName = "第" + (i + 1) + "组:  ";}
          for (int j = 0; j < this.selectNum[i].Length; j++)
          {
              if (j == 5) { listName += selectNum[i][j] + "    "; } else
              {listName += selectNum[i][j] + "  ";}
          }
          listData.Add(listName); //重点
      }
      listData.Reverse();  //重点
      return listData;
  }

【主函数】

//初始化
public FrmMain()
{
    InitializeComponent();
    this.timer_sleep.Stop();
    this.btn_randomGro.Enabled = true;
    this.btn_confirm.Enabled = true;
    this.btn_random.Enabled = true;
    this.btn_clear.Enabled = false;
    this.btn_dele.Enabled = false;
    this.timer1.Start();
}
   //创建类对象
   private Selector objSelector = new Selector();
   //实例方法-列表显示
   public void showList()
   {
       this.list_num.Items.Clear();  //先清空列表,再添加
       this.list_num.Items.AddRange(this.objSelector.listNum().ToArray()); //重点
   }
//关闭
private void btnClose_Click(object sender, EventArgs e)
{
    this.Close();
}
//最小化
private void btnMin_Click(object sender, EventArgs e)
{
    this.WindowState = FormWindowState.Minimized;
}

窗口拖拽

//随机选号
private void btn_random_Click(object sender, EventArgs e)
{
    this.timer_sleep.Start();
    this.btn_random.Enabled = false;
    this.btn_confirm.Enabled = true;
}
//确认选号
private void btn_confirm_Click(object sender, EventArgs e)
{
    this.timer_sleep.Stop();
    string[] txtNums ={
        lblNum1.Text,
        lblNum2.Text,
        lblNum3.Text,
        lblNum4.Text,
        lblNum5.Text,
        lblNum6.Text,
        lblNum7.Text };  //难点
    this.objSelector.selectNum.Add(txtNums);
    showList();
    this.btn_confirm.Enabled = false;
    this.btn_random.Enabled = true;
  //随机组选
  private void btn_randomGro_Click(object sender, EventArgs e)
  {
      this.objSelector.selectNumGroup(Convert.ToInt16(this.lblNumGro.Text));
      showList();
      this.btn_clear.Enabled = true;
      this.btn_dele.Enabled = true;
  }
//删除
private void brn_dele_Click(object sender, EventArgs e)
{
    this.objSelector.selectNum.RemoveAt(this.list_num.SelectedIndex); //重点
    showList();
}
//清空
private void btn_clear_Click(object sender, EventArgs e)
{ 
    this.list_num.Items.Clear();
    this.objSelector.selectNum.Clear();  //重点
    btn_clear.Enabled = false;
    btn_dele.Enabled = false;
}
#region 【扩展】文件的打开保存、定时器实时刷新
//保存到文件
private void btnSave_Click(object sender, EventArgs e)
{
    FileStream fs = new FileStream("NumList.Txt", FileMode.Create);  //重点
    StreamWriter sw = new StreamWriter(fs);
    for (int i = 0; i < this.list_num.Items.Count; i++)
    {
        sw.WriteLine(this.list_num.Items[i]);
    }
    sw.Close();
    fs.Close();
    MessageBox.Show("保存成功", "提示您", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
//打开文件夹
private void btnsaveOpen_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("Explorer.exe",
        "J:\\自学视频\\1C#\\喜科堂 郭哥推荐\\练习第二遍\\彩票选号软件\\bin\\Debug\\NumList.Txt");
}
//定时器实时监测数值-初始化开始定时器timer1
private void timer1_Tick(object sender, EventArgs e)
{
    if (this.list_num.Items.Count == 0 || this.list_num.SelectedItem == null)
    {
        this.btn_dele.Enabled = false;
    }
    else
    {
        this.btn_dele.Enabled = true;
    }
}
#endregion

程序开发逻辑图如下所示:

到此这篇关于C#项目彩票选号实现思路的文章就介绍到这了,更多相关C#彩票选号内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文