case break结构与return的有关要点

//确认事件
private void cmd_ok_Click(object sender, EventArgs e)
{
//客户名称是否为空
if (txt_banhao.Text.TrimEnd() == "")
{
MessageBox.Show("电脑版号不能为空", "关闭", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
txt_banhao.Focus();
return;//这个return必须写才能跳出整个点击事件,因为不会执行break,不写return就会往下执行switch语句
}

switch (lab_state.Text)
{
case "添加": //if()else()里面的return最多只能有一个,否则break会有警告
if (SpareIsExist(txt_banhao.Text.TrimEnd()) == true)
{
MessageBox.Show("该电脑版号已存在", "关闭", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
//return; //不写return也行,因为会跳过else而执行break强制结束该点击按钮事件
}
else
{
//不存在旧版号
Ulock_Contral();
lab_logindate.Text = DateTime.Now.ToString("yyyy-MM-dd");
lab_update.Text = DateTime.Now.ToString("yyyy-MM-dd");
txt_banhao.Enabled = false;
cmd_ok.Enabled = false;
//return;//不写return也行,程序也会执行break结束 "添加"这个条件
}

break;

case "修改":

if (SpareIsExist(txt_banhao.Text.TrimEnd()) == true)
{
if (_state_touchang == "已投产")
{
MessageBox.Show("注意该版号已投产", "关闭", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
//解锁各控件
Ulock_Contral();
txt_banhao.Enabled = false;
cmd_ok.Enabled = false;

}

if (lab_formstate.Text == "未制版")
{
//解锁各控件
Ulock_Contral();
txt_banhao.Enabled = false;
cmd_ok.Enabled = false;

}
if (lab_formstate.Text == "已制版")
{
MessageBox.Show("注意该版号已制版", "关闭", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
//解锁各控件
Ulock_Contral();
txt_banhao.Enabled = false;
cmd_ok.Enabled = false;

}
if (lab_formstate.Text == "已投产")
{
MessageBox.Show("该版号已投产无法修改", "关闭", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
//解锁各控件
//Ulock_Contral();
//txt_banhao.Enabled = false;
//cmd_ok.Enabled = false;

}

}
else
{

MessageBox.Show("不存在该电脑版号", "关闭", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

}

break;

case "删除":

if (SpareIsExist(txt_banhao.Text.TrimEnd()) == true)
{
if (_state_touchang == "已投产")
{
MessageBox.Show("已投产不能删除", "关闭", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

}

if (lab_formstate.Text == "未制版")
{
//解锁各控件
//Ulock_Contral();
txt_banhao.Enabled = false;
cmd_ok.Enabled = false;
cmd_save.Enabled = true;

}
else
{
MessageBox.Show("已制版不能删除", "关闭", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

}

}
else
{

MessageBox.Show("不存在该电脑版号", "关闭", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

}

break;

}

}

转载于:https://www.cnblogs.com/tangfei/p/4436420.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值