实例介绍
【实例简介】使用于各个场合的抽奖系统,代码中给出了详细注释,供大家学习,抽奖标题、抽奖内容、抽奖人员名单可自定
【实例截图】
【核心代码】 public partial class frmPerson : Form
{
public static string filePath = string.Empty ;//定义文件路径的全局变量
public List<Person> objListQuery = new List<Person>();//满足条件的人员信息
private PersonService objPersonServices = new PersonService();//针对人员的功能
private int actionFlag = 0;//定义一个操作标识符,1表示添加,2表示修改,3表示删除
private LuckyPersonService objLuckyPersonService=new LuckyPersonService();
public frmPerson()
{
InitializeComponent();
//默认禁用明细区域
gboxDetail.Enabled = false;
//默认加载数据到datagridList
dgvPerson.DataSource = null;
dgvPerson.AutoGenerateColumns = false;
dgvPerson.DataSource = Program.objListPerson;
lblTotalPerson.Text = dgvPerson.RowCount.ToString();
}
//模块01:导入和呈现数据:选择文件-->读取到List-->展示在dategridview中-->展示明细
//模块02:人员信息查询:所有的人员:Program.objListPerson
//满足条件的人员:objListQuery
//模块03:增、删、改 -->List<Person>-->更新datagridview
//控件事件
private void button6_Click(object sender, EventArgs e)//关闭frmPerson窗体
{
this.Close();
}
private void frmPerson_FormClosed(object sender, FormClosedEventArgs e)//关闭窗体后
{
frmMain.objfrmPerson = null; ;
}
/// <summary>
/// 导入文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnImport_Click(object sender, EventArgs e)//导入文件
{
//选择导入文件的路径
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "csv文件|*.csv|TXT文件|*.txt|所有文件|*.*";
if(openfile.ShowDialog()==DialogResult.OK)
{
filePath = openfile.FileName;
}
//读取到List中
try
{
Program.objListPerson = fileOperator.ReadFile(filePath);
}
catch (Exception ex)
{
MessageBox.Show("读取文件出错,具体原因:" ex.Message,"系统消息",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
//把文件导入到datagridview上
dgvPerson.DataSource = null;//刷新数据
dgvPerson.AutoGenerateColumns = false;//取消显示多余的行数
dgvPerson.DataSource = Program.objListPerson;
//更新抽奖人数
if (Program.objListPerson != null)
{
lblTotalPerson.Text = Program.objListPerson.Count.ToString();
}
else
{
lblTotalPerson.Text = Convert.ToString(0);
}
//显示明细信息
if (dgvPerson.Rows.Count == 0) return ;//如果行数为0,就不显示
else
{
txtPersonId.Text = dgvPerson.Rows[0].Cells[0].Value.ToString();
txtMobile.Text = dgvPerson.Rows[0].Cells[1].Value.ToString();
txtPersonName.Text = dgvPerson.Rows[0].Cells[2].Value.ToString();
}
}
private void dgvPerson_SelectionChanged(object sender, EventArgs e) //鼠标选择行发生变化时触发的事件
{
if (dgvPerson.Rows.Count == 0) return;//如果表格没有数据则不展示
if (dgvPerson.CurrentRow.Selected == false)//如果当前鼠标没有选择行,则默认展示第一行
{
try
{
txtPersonId.Text = dgvPerson.Rows[0].Cells[0].Value.ToString();
txtMobile.Text = dgvPerson.Rows[0].Cells[1].Value.ToString();
txtPersonName.Text = dgvPerson.Rows[0].Cells[2].Value.ToString();
}
catch
{
return;
}
}
else//如果选中就展示当前行
{
txtPersonId.Text = dgvPerson.CurrentRow.Cells[0].Value.ToString();
txtMobile.Text = dgvPerson.CurrentRow.Cells[1].Value.ToString();
txtPersonName.Text = dgvPerson.CurrentRow.Cells[2].Value.ToString();
}
}
private void txtQueryByName_TextChanged(object sender, EventArgs e) //按照姓名查找
{
//清空objListQuery
objListQuery.Clear();
//获取满足条件的person
objListQuery = objPersonServices.GetAllPersonByName(txtQueryByName.Text.Trim(),Program.objListPerson);
//绑定到明细区
dgvPerson.DataSource = null;
dgvPerson.DataSource = objListQuery;
}
private void txtQueryByMobile_TextChanged(object sender, EventArgs e)//按照称号查询
{
//清空objListQuery
objListQuery.Clear();
//获取
objListQuery = objPersonServices.GetAllPersonByMobile(txtQueryByMobile.Text,Program.objListPerson);
//绑定到明细区
dgvPerson.DataSource = null;
dgvPerson.DataSource = objListQuery;
}
private void btnAdd_Click(object sender, EventArgs e)//为添加人员做准备
{
//禁用按钮
DisableButton();
//编号禁用,采用自动排序生成,姓名和称号清空
txtPersonId.Enabled = false;
txtPersonId.Text = string.Empty;
txtMobile.Text = string.Empty;
txtPersonName.Text = string.Empty;
txtPersonId.Text=objPersonServices.GetNewPersonId(Program.objListPerson);
//让称号获得焦点
txtMobile.Focus();
//修改操作标识符
actionFlag = 1;
}
private void btnUpdate_Click(object sender, EventArgs e)//为修改人员做准备
{
//禁用按钮
DisableButton();
//编号禁用
txtPersonId.Enabled = false;
//让称号获得焦点
txtMobile.Focus();
//修改操作标识符
actionFlag = 2;
}
private void btnDelete_Click(object sender, EventArgs e)//为删除人员做准备
{
if (dgvPerson.DataSource == null) return;
else if (dgvPerson.CurrentRow.Selected == false) return;
//禁用按钮和明细区
DisableButton();
//修改操作标识符
actionFlag = 3;
}
private void btnCommit_Click(object sender, EventArgs e)//提交添加、删除或修改
{
//明细区展示最新添加的一行
//判断输入是否合理
if(txtPersonName.Text==string.Empty)
{
MessageBox.Show("姓名不能为空","系统消息",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
txtPersonName.Focus();
return;
}
if(txtMobile.Text==string.Empty)
{
MessageBox.Show("江湖称号不能为空","系统消息",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
txtMobile.Focus();
return;
}
if(!Judgement.JudgementMobile(txtMobile.Text.Trim()))
{
MessageBox.Show("江湖称号必须是汉字","系统消息",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
txtMobile.Focus();
return;
}
if (!Judgement.JudgementMobile(txtPersonName.Text.Trim()))
{
MessageBox.Show("姓名必须是汉字", "系统消息", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
txtPersonName.Focus();
return;
}
//封装类 类的初始化器
Person person = new Person
{
PersonId = Convert.ToInt32(txtPersonId.Text.Trim()),
PersonName=txtPersonName.Text.Trim(),
Mobile=txtMobile.Text.Trim(),
};
//运行提交
switch (actionFlag)
{
case 1://添加
objPersonServices.AddPerson(person,Program.objListPerson);
//添加到dgvPerson
dgvPerson.DataSource = null;
dgvPerson.DataSource = Program.objListPerson;
//明细区展示最新添加的一行
if (dgvPerson.DataSource == null) return;
txtMobile.Text = dgvPerson.Rows[dgvPerson.RowCount-1 ].Cells[1].Value.ToString();
txtPersonName.Text = dgvPerson.Rows[dgvPerson.RowCount-1 ].Cells[2].Value.ToString();
//更新总数
lblTotalPerson.Text = Program.objListPerson.Count.ToString();
//提示成功
MessageBox.Show("添加成功","系统消息",MessageBoxButtons.OK,MessageBoxIcon.Information);
//启用按钮
EnablButton();
break;
case 2://修改
objPersonServices.UpdatePerson(person, Program.objListPerson);
//更新dgvPerson
dgvPerson.DataSource = null;
dgvPerson.DataSource = Program.objListPerson;
//明细区展示当前选择行
txtMobile.Text = dgvPerson.CurrentRow.Cells[1].Value.ToString();
txtPersonName.Text = dgvPerson.CurrentRow.Cells[2].Value.ToString();
//更新总数
lblTotalPerson.Text = Program.objListPerson.Count.ToString();
//提示修改成功
MessageBox.Show("修改成功", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
//启用按钮
EnablButton();
break;
case 3:
//删除操作
objPersonServices.DeletePerson(txtPersonId.Text,Program.objListPerson);
MessageBox.Show("是否确定删除此人员","系统",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
MessageBox.Show("删除成功","系统消息",MessageBoxButtons.OK,MessageBoxIcon.Information);
//更新dgvPerson
dgvPerson.DataSource = null;
dgvPerson.DataSource = Program.objListPerson;
//详细区指向第一行
txtPersonId.Text = dgvPerson.Rows[0].Cells[0].Value.ToString();
txtPersonName.Text = dgvPerson.Rows[0].Cells[2].Value.ToString();
txtMobile.Text = dgvPerson.Rows[0].Cells[1].Value.ToString();
//更新总数
lblTotalPerson.Text = Program.objListPerson.Count.ToString();
//启用按钮
EnablButton();
break;
default:
break;
}
}
private void btnCancel_Click(object sender, EventArgs e)//取消添加、删除或修改
{
//启用Button
EnablButton();
//如果是添加,展示明细
if (dgvPerson.DataSource == null) return;
if(actionFlag==1)
{
//展示第一行
txtPersonId.Text = dgvPerson.Rows[0].Cells[0].Value.ToString();
txtMobile.Text = dgvPerson.Rows[0].Cells[1].Value.ToString();
txtPersonName.Text = dgvPerson.Rows[0].Cells[2].Value.ToString();
}
}
private void btnSave_Click(object sender, EventArgs e)//保存文件
{
try
{
if (fileOperator.WriteFile(filePath, Program.objListPerson))
{
MessageBox.Show("保存文件成功", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show("保存文件失败,具体原因:" ex.Message, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//自定义方法
private void DisableButton()//禁用按钮
{
//禁用
btnImport.Enabled = false;
btnAdd.Enabled = false;
btnDelete.Enabled = false;
btnUpdate.Enabled = false;
btnSave.Enabled = false;
//启用
gboxDetail.Enabled = true; ;
}
public void EnablButton()//启用按钮
{
//启用
btnImport.Enabled = true;
btnAdd.Enabled = true;
btnDelete.Enabled = true;
btnUpdate.Enabled = true;
btnSave.Enabled = true;
//禁用
gboxDetail.Enabled = false;
}
//如果不允许二次中奖
【实例截图】

【核心代码】 public partial class frmPerson : Form
{
public static string filePath = string.Empty ;//定义文件路径的全局变量
public List<Person> objListQuery = new List<Person>();//满足条件的人员信息
private PersonService objPersonServices = new PersonService();//针对人员的功能
private int actionFlag = 0;//定义一个操作标识符,1表示添加,2表示修改,3表示删除
private LuckyPersonService objLuckyPersonService=new LuckyPersonService();
public frmPerson()
{
InitializeComponent();
//默认禁用明细区域
gboxDetail.Enabled = false;
//默认加载数据到datagridList
dgvPerson.DataSource = null;
dgvPerson.AutoGenerateColumns = false;
dgvPerson.DataSource = Program.objListPerson;
lblTotalPerson.Text = dgvPerson.RowCount.ToString();
}
//模块01:导入和呈现数据:选择文件-->读取到List-->展示在dategridview中-->展示明细
//模块02:人员信息查询:所有的人员:Program.objListPerson
//满足条件的人员:objListQuery
//模块03:增、删、改 -->List<Person>-->更新datagridview
//控件事件
private void button6_Click(object sender, EventArgs e)//关闭frmPerson窗体
{
this.Close();
}
private void frmPerson_FormClosed(object sender, FormClosedEventArgs e)//关闭窗体后
{
frmMain.objfrmPerson = null; ;
}
/// <summary>
/// 导入文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnImport_Click(object sender, EventArgs e)//导入文件
{
//选择导入文件的路径
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "csv文件|*.csv|TXT文件|*.txt|所有文件|*.*";
if(openfile.ShowDialog()==DialogResult.OK)
{
filePath = openfile.FileName;
}
//读取到List中
try
{
Program.objListPerson = fileOperator.ReadFile(filePath);
}
catch (Exception ex)
{
MessageBox.Show("读取文件出错,具体原因:" ex.Message,"系统消息",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
//把文件导入到datagridview上
dgvPerson.DataSource = null;//刷新数据
dgvPerson.AutoGenerateColumns = false;//取消显示多余的行数
dgvPerson.DataSource = Program.objListPerson;
//更新抽奖人数
if (Program.objListPerson != null)
{
lblTotalPerson.Text = Program.objListPerson.Count.ToString();
}
else
{
lblTotalPerson.Text = Convert.ToString(0);
}
//显示明细信息
if (dgvPerson.Rows.Count == 0) return ;//如果行数为0,就不显示
else
{
txtPersonId.Text = dgvPerson.Rows[0].Cells[0].Value.ToString();
txtMobile.Text = dgvPerson.Rows[0].Cells[1].Value.ToString();
txtPersonName.Text = dgvPerson.Rows[0].Cells[2].Value.ToString();
}
}
private void dgvPerson_SelectionChanged(object sender, EventArgs e) //鼠标选择行发生变化时触发的事件
{
if (dgvPerson.Rows.Count == 0) return;//如果表格没有数据则不展示
if (dgvPerson.CurrentRow.Selected == false)//如果当前鼠标没有选择行,则默认展示第一行
{
try
{
txtPersonId.Text = dgvPerson.Rows[0].Cells[0].Value.ToString();
txtMobile.Text = dgvPerson.Rows[0].Cells[1].Value.ToString();
txtPersonName.Text = dgvPerson.Rows[0].Cells[2].Value.ToString();
}
catch
{
return;
}
}
else//如果选中就展示当前行
{
txtPersonId.Text = dgvPerson.CurrentRow.Cells[0].Value.ToString();
txtMobile.Text = dgvPerson.CurrentRow.Cells[1].Value.ToString();
txtPersonName.Text = dgvPerson.CurrentRow.Cells[2].Value.ToString();
}
}
private void txtQueryByName_TextChanged(object sender, EventArgs e) //按照姓名查找
{
//清空objListQuery
objListQuery.Clear();
//获取满足条件的person
objListQuery = objPersonServices.GetAllPersonByName(txtQueryByName.Text.Trim(),Program.objListPerson);
//绑定到明细区
dgvPerson.DataSource = null;
dgvPerson.DataSource = objListQuery;
}
private void txtQueryByMobile_TextChanged(object sender, EventArgs e)//按照称号查询
{
//清空objListQuery
objListQuery.Clear();
//获取
objListQuery = objPersonServices.GetAllPersonByMobile(txtQueryByMobile.Text,Program.objListPerson);
//绑定到明细区
dgvPerson.DataSource = null;
dgvPerson.DataSource = objListQuery;
}
private void btnAdd_Click(object sender, EventArgs e)//为添加人员做准备
{
//禁用按钮
DisableButton();
//编号禁用,采用自动排序生成,姓名和称号清空
txtPersonId.Enabled = false;
txtPersonId.Text = string.Empty;
txtMobile.Text = string.Empty;
txtPersonName.Text = string.Empty;
txtPersonId.Text=objPersonServices.GetNewPersonId(Program.objListPerson);
//让称号获得焦点
txtMobile.Focus();
//修改操作标识符
actionFlag = 1;
}
private void btnUpdate_Click(object sender, EventArgs e)//为修改人员做准备
{
//禁用按钮
DisableButton();
//编号禁用
txtPersonId.Enabled = false;
//让称号获得焦点
txtMobile.Focus();
//修改操作标识符
actionFlag = 2;
}
private void btnDelete_Click(object sender, EventArgs e)//为删除人员做准备
{
if (dgvPerson.DataSource == null) return;
else if (dgvPerson.CurrentRow.Selected == false) return;
//禁用按钮和明细区
DisableButton();
//修改操作标识符
actionFlag = 3;
}
private void btnCommit_Click(object sender, EventArgs e)//提交添加、删除或修改
{
//明细区展示最新添加的一行
//判断输入是否合理
if(txtPersonName.Text==string.Empty)
{
MessageBox.Show("姓名不能为空","系统消息",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
txtPersonName.Focus();
return;
}
if(txtMobile.Text==string.Empty)
{
MessageBox.Show("江湖称号不能为空","系统消息",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
txtMobile.Focus();
return;
}
if(!Judgement.JudgementMobile(txtMobile.Text.Trim()))
{
MessageBox.Show("江湖称号必须是汉字","系统消息",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
txtMobile.Focus();
return;
}
if (!Judgement.JudgementMobile(txtPersonName.Text.Trim()))
{
MessageBox.Show("姓名必须是汉字", "系统消息", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
txtPersonName.Focus();
return;
}
//封装类 类的初始化器
Person person = new Person
{
PersonId = Convert.ToInt32(txtPersonId.Text.Trim()),
PersonName=txtPersonName.Text.Trim(),
Mobile=txtMobile.Text.Trim(),
};
//运行提交
switch (actionFlag)
{
case 1://添加
objPersonServices.AddPerson(person,Program.objListPerson);
//添加到dgvPerson
dgvPerson.DataSource = null;
dgvPerson.DataSource = Program.objListPerson;
//明细区展示最新添加的一行
if (dgvPerson.DataSource == null) return;
txtMobile.Text = dgvPerson.Rows[dgvPerson.RowCount-1 ].Cells[1].Value.ToString();
txtPersonName.Text = dgvPerson.Rows[dgvPerson.RowCount-1 ].Cells[2].Value.ToString();
//更新总数
lblTotalPerson.Text = Program.objListPerson.Count.ToString();
//提示成功
MessageBox.Show("添加成功","系统消息",MessageBoxButtons.OK,MessageBoxIcon.Information);
//启用按钮
EnablButton();
break;
case 2://修改
objPersonServices.UpdatePerson(person, Program.objListPerson);
//更新dgvPerson
dgvPerson.DataSource = null;
dgvPerson.DataSource = Program.objListPerson;
//明细区展示当前选择行
txtMobile.Text = dgvPerson.CurrentRow.Cells[1].Value.ToString();
txtPersonName.Text = dgvPerson.CurrentRow.Cells[2].Value.ToString();
//更新总数
lblTotalPerson.Text = Program.objListPerson.Count.ToString();
//提示修改成功
MessageBox.Show("修改成功", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
//启用按钮
EnablButton();
break;
case 3:
//删除操作
objPersonServices.DeletePerson(txtPersonId.Text,Program.objListPerson);
MessageBox.Show("是否确定删除此人员","系统",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
MessageBox.Show("删除成功","系统消息",MessageBoxButtons.OK,MessageBoxIcon.Information);
//更新dgvPerson
dgvPerson.DataSource = null;
dgvPerson.DataSource = Program.objListPerson;
//详细区指向第一行
txtPersonId.Text = dgvPerson.Rows[0].Cells[0].Value.ToString();
txtPersonName.Text = dgvPerson.Rows[0].Cells[2].Value.ToString();
txtMobile.Text = dgvPerson.Rows[0].Cells[1].Value.ToString();
//更新总数
lblTotalPerson.Text = Program.objListPerson.Count.ToString();
//启用按钮
EnablButton();
break;
default:
break;
}
}
private void btnCancel_Click(object sender, EventArgs e)//取消添加、删除或修改
{
//启用Button
EnablButton();
//如果是添加,展示明细
if (dgvPerson.DataSource == null) return;
if(actionFlag==1)
{
//展示第一行
txtPersonId.Text = dgvPerson.Rows[0].Cells[0].Value.ToString();
txtMobile.Text = dgvPerson.Rows[0].Cells[1].Value.ToString();
txtPersonName.Text = dgvPerson.Rows[0].Cells[2].Value.ToString();
}
}
private void btnSave_Click(object sender, EventArgs e)//保存文件
{
try
{
if (fileOperator.WriteFile(filePath, Program.objListPerson))
{
MessageBox.Show("保存文件成功", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show("保存文件失败,具体原因:" ex.Message, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//自定义方法
private void DisableButton()//禁用按钮
{
//禁用
btnImport.Enabled = false;
btnAdd.Enabled = false;
btnDelete.Enabled = false;
btnUpdate.Enabled = false;
btnSave.Enabled = false;
//启用
gboxDetail.Enabled = true; ;
}
public void EnablButton()//启用按钮
{
//启用
btnImport.Enabled = true;
btnAdd.Enabled = true;
btnDelete.Enabled = true;
btnUpdate.Enabled = true;
btnSave.Enabled = true;
//禁用
gboxDetail.Enabled = false;
}
//如果不允许二次中奖
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论