|
C#彩票选号软件
今天做了一个彩票选号的小软件,将学到的知识点总结如下;
1.写程序的思路
2.学到的知识点
- 判断、循环
- 文件的操作(保存、打开)
- 集合、数组(增加-AddRange、删除-RemoveAt、排序-Reverse)
- 关键字:Random(随机数-Next)
- 定时器的应用(启动、停止、实时刷新)
3.经验技巧
- 开发控件的属性可以查看界面后的designer中的控件缺省代码并修改应用
4.UI界面
5.代码实现
【实体类】
- //属性-临时号码(用于存储随机号及数组的) public List selectNum { get; set; } private Random randomNum = new Random(); //构造方法-创建临时号码对象 public Selector() { this.selectNum = new List(); }
复制代码- //实例方法-随机选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 listNum() { List listData = new List(); 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();
复制代码
- Step03:【添加列表-AddRange】调用实体类
- //实例方法-列表显示 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;}
复制代码 窗口拖拽- #region 拖动窗体的实现private Point mouseOff;//鼠标移动位置变量private bool leftFlag;//标签是否为左键private void FrmMain_MouseDown(object sender, MouseEventArgs e){ if (e.Button == MouseButtons.Left) { mouseOff = new Point(-e.X, -e.Y); //得到变量的值 leftFlag = true; //点击左键按下时标注为true; }}private void FrmMain_MouseMove(object sender, MouseEventArgs e){ if (leftFlag) { Point mouseSet = Control.MousePosition; mouseSet.Offset(mouseOff.X, mouseOff.Y); //设置移动后的位置 Location = mouseSet; }}private void FrmMain_MouseUp(object sender, MouseEventArgs e){ if (leftFlag) { leftFlag = false;//释放鼠标后标注为false; }}//在所需移动的控件事件中选定3方法即可实现#endregion
复制代码
- Step05:【随机选号】让标签数字滚动,开始定时器即可(使能按钮)
- //随机选号private void btn_random_Click(object sender, EventArgs e){ this.timer_sleep.Start(); this.btn_random.Enabled = false; this.btn_confirm.Enabled = true;}
复制代码
- Step06:【确认选号】停止滚动、并保存当前数字到成员变量、并显示列表
- //确认选号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;
复制代码
- Step07:【随机组选】将组数传递到参数方法、显示列表
- //随机组选 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; }
复制代码
- Step08:【清除、清空】获取选中的索引,调用集合项的【RemoveAt】;【Clear】
- //删除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");}//定时器实时监测数值-初始化开始定时器timer1private 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
复制代码 程序开发逻辑图如下所示:
来源:https://www.cnblogs.com/guoenshuo/p/18348165
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
|