在解析之前停止更改表单

时间:2014-02-20 15:56:12

标签: c#

我想要移动到下一个表单,但是在解析项目时我尝试了catch,当按下下一个按钮时它会捕获,如果有问题但是仍然转到下一个表单。无论如何通过将它保持在同一个表单上来阻止它,但是能够在下一个按钮中移动到下一个表单吗?

由于

private void nextButton_Click(object sender, EventArgs e)
    {
        ParseItems();
        Formchange();
    }
public void ParseItems()
    {
        try
        {
            Amount = decimal.Parse(DepositTextBox1.Text);

            try
            {
                WeekInterestRate = decimal.Parse(WeekIntTextBox.Text);
                try
                {
                    TwoWeekInterestRate = decimal.Parse(TWeekIntTextBox.Text);
                    try
                    {
                        MonthInterestRate = decimal.Parse(MonthIntTextBox.Text);
                        try
                        {
                            ThreeMonthInterestRate = decimal.Parse(ThreeMonthIntTextBox.Text);
                        }
                        catch
                        {
                            MessageBox.Show("Please enter values in numerical form", "Input Error");
                            ThreeMonthIntTextBox.Focus();
                        }
                    }
                    catch
                    {
                        MessageBox.Show("Please enter values in numerical form", "Input Error");
                        MonthIntTextBox.Focus();

                    }
                }
                catch
                {
                    MessageBox.Show("Please enter values in numerical form","Input Error");
                    TWeekIntTextBox.Focus();
                }
            }
            catch
            {
                MessageBox.Show("Please enter values in numerical form", "Input Error");
                WeekIntTextBox.Focus();
            }
        }
        catch
        {
            MessageBox.Show("Please enter values in numerical form","Input Error");
            DepositTextBox1.Focus();
        }

    }


    //changes to the next form
        public void Formchange()
        {
            Form2 f3 = new Form2();
            f3.Show(this);
            Hide();
        }

3 个答案:

答案 0 :(得分:0)

只需将您的代码更改为以下

private void nextButton_Click(object sender, EventArgs e)
        {
            ParseItems();                
        }
    public void ParseItems()
        {
            try
            {
                Amount = decimal.Parse(DepositTextBox1.Text);

                try
                {
                    WeekInterestRate = decimal.Parse(WeekIntTextBox.Text);
                    try
                    {
                        TwoWeekInterestRate = decimal.Parse(TWeekIntTextBox.Text);
                        try
                        {
                            MonthInterestRate = decimal.Parse(MonthIntTextBox.Text);
                            try
                            {
                                ThreeMonthInterestRate = decimal.Parse(ThreeMonthIntTextBox.Text);
                                //here how you can achieve this 
                                 Formchange(); 
                            }
                            catch
                            {
                                MessageBox.Show("Please enter values in numerical form", "Input Error");
                                ThreeMonthIntTextBox.Focus();
                            }
                        }
                        catch
                        {
                            MessageBox.Show("Please enter values in numerical form", "Input Error");
                            MonthIntTextBox.Focus();

                        }
                    }
                    catch
                    {
                        MessageBox.Show("Please enter values in numerical form","Input Error");
                        TWeekIntTextBox.Focus();
                    }
                }
                catch
                {
                    MessageBox.Show("Please enter values in numerical form", "Input Error");
                    WeekIntTextBox.Focus();
                }
            }
            catch
            {
                MessageBox.Show("Please enter values in numerical form","Input Error");
                DepositTextBox1.Focus();
            }

        }

答案 1 :(得分:0)

尝试这种方法:

public bool ParseItems()
{
   bool Success=false;
   try
    {
        Amount = decimal.Parse(DepositTextBox1.Text);
        Success=true;
    }
   catch(Exception e){
   Success=false;
    }
}

if(Success)
return true;

return false;
}

private void nextButton_Click(object sender, EventArgs e)
{
    if(ParseItems())
    {
     Formchange();
    }
}

答案 2 :(得分:0)

使用返回值

更新您的函数
    public bool ParseItems()
在catch块内部添加一个return语句作为最后一个动作(在你调用Focus()之后);)

    return false;

将return语句添加到函数末尾

    return true;

更新你的呼叫代码

     if(ParseItems())
         Formchange();