我现在想请教一下如何让程序直接终止后面的代码,返回主窗体。
比如说:只要执行eRr了。弹出警告窗口后直接就返回主窗体,后面的运算不计算了。
public Form1()
{
InitializeComponent();
}
private void eRr()
{
MessageBox.Show("请输入有效时间", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void button1_Click(object sender, EventArgs e)
{
dOo(tImes, dAtes);
}
private void dOo (string tI, string dA)
{
tI = tI.Trim('0');
Regex didTime = new Regex(@"^([0-9]+(:)*)+$");
Regex didDate = new Regex(@"^([0-9]+(:)*(-)*(\\)*(/)*)+$");
if (didTime.IsMatch(tI) & didDate.IsMatch(dA))
{
if (!tI.Contains(":"))
{
if (tI.Length > 0 & tI.Length <= 4)
{
tI = tIMe(tI);
}
else
{
eRr();
}
}
int a = Convert.ToInt32(tI.Remove(2));
if (a > 0 & a < 24)
{
tI = Convert.ToDateTime(tI).ToShortTimeString();
tI = tI.Remove(tI.Length - 3);
}
else
{
eRr();
}
}
else
{
eRr();
}
}
eRr();后面接return
或者 你可以使用goto 标签的方式进行代码跳转 这样也可以省去很多重复代码 比如:
private void dOo (string tI, string dA)没有返回类型的方法用
return;
有返回类型的用
return 类型变量值;
你以上想执行eRr()你就把不想执行的代码删除或是注释掉不就得了
追问不能注释掉了哇,代码正确的时候还要执行那部分呢。不正确的时候执行这部分,跳过那部分直接回到主窗体。
追答你不执行你怎么知道代码不正确?