C# 终止后面的程序代码直接返回主窗体

我现在想请教一下如何让程序直接终止后面的代码,返回主窗体。
比如说:只要执行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)
{
    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
            {
                goto error;
            }

        }
        int a = Convert.ToInt32(tI.Remove(2));
        if (a > 0 & a < 24)
        {
            tI = Convert.ToDateTime(tI).ToShortTimeString();
            tI = tI.Remove(tI.Length - 3);
        }
        else
        {
            goto error;
        }
}
    else
    {
        goto error;
    }

return; //正常执行 直接结束
error: //如果出错 直接跳转这里
eRr();
}

温馨提示:内容为网友见解,仅供参考
第1个回答  2014-10-06
public Form1()
        {
            InitializeComponent();
        }
 private void eRr()
        {

            MessageBox.Show("请输入有效时间", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            
        }
 private void button1_Click(object sender, EventArgs e)
        {
            eRr();
           //dOo(tImes, dAtes);
        }

没有返回类型的方法用

return;

有返回类型的用

return 类型变量值;

你以上想执行eRr()你就把不想执行的代码删除或是注释掉不就得了

追问

不能注释掉了哇,代码正确的时候还要执行那部分呢。不正确的时候执行这部分,跳过那部分直接回到主窗体。

追答

你不执行你怎么知道代码不正确?

第2个回答  2014-10-06
eRr();后面直接跟return;
相似回答