基本信息
源码名称:C#抽奖系统源码(适合新手学习)
源码大小:0.80M
文件格式:.rar
开发语言:C#
更新时间:2019-09-26
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
使用于各个场合的抽奖系统,代码中给出了详细注释,供大家学习,抽奖标题、抽奖内容、抽奖人员名单可自定
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;
}
//如果不允许二次中奖