C#彩票选号软件
今天做了一个彩票选号的小软件,将学到的知识点总结如下;
1.写程序的思路
- 实体类(属性、方法)
- 主程序调用类方法
2.学到的知识点
- 判断、循环
- 文件的操作(保存、打开)
- 集合、数组(增加-AddRange、删除-RemoveAt、排序-Reverse)
- 关键字:Random(随机数-Next)
- 定时器的应用(启动、停止、实时刷新)
3.经验技巧
- 开发控件的属性可以查看界面后的designer中的控件缺省代码并修改应用
4.UI界面
5.代码实现
【实体类】
- 功能一:定义一个保存选号的成员并【创建对象】
//属性-临时号码(用于存储随机号及数组的)public List<string[]> selectNum { get; set; }private Random randomNum = new Random();//构造方法-创建临时号码对象public Selector(){this.selectNum = new List<string[]>();}
- 功能二:生成7个随机数【数组】并将数组返回
//实例方法-随机选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组随机数组无需返回【带参数】
//实例方法-随机选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;}
【主函数】
- Step01:【初始化】
//初始化
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();
}
- Step02:【创建类对象】
//创建类对象private Selector objSelector = new Selector();
- Step03:【添加列表-AddRange】调用实体类<遍历成员而生成的列表>
//实例方法-列表显示public void showList(){this.list_num.Items.Clear(); //先清空列表,再添加this.list_num.Items.AddRange(this.objSelector.listNum().ToArray()); //重点}
- Step04:【缺省类】关闭、最小化、窗口拖拽
//关闭
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;
}
- Step09:【扩展】文件的打开保存、实时刷新
#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
程序开发逻辑图如下所示: