Windows窗体应用程序意外崩溃C#

时间:2015-02-28 22:08:08

标签: c#

这是我的代码。我不认为代码有任何问题。有时它运行正常,但有时会意外崩溃。

namespace searchingConsoleWFA
{
public partial class Form1 : Form
{
    searchingConsole.Class1 objj = new searchingConsole.Class1();
    List<searchingConsole.Class1> obj = new List<searchingConsole.Class1>();

    private void button3_Click(object sender, EventArgs e)
    {         
        String compName, tit, conName, phone, fax, addr, pCode, city, reg, cntry;

        if (String.IsNullOrEmpty(textBox1.Text) || String.IsNullOrEmpty(textBox2.Text)
             || String.IsNullOrEmpty(textBox3.Text) || String.IsNullOrEmpty(textBox4.Text)
             || String.IsNullOrEmpty(textBox5.Text) || String.IsNullOrEmpty(textBox6.Text)
             || String.IsNullOrEmpty(textBox7.Text) || String.IsNullOrEmpty(textBox8.Text)
             || String.IsNullOrEmpty(textBox9.Text) || String.IsNullOrEmpty(textBox10.Text)
            )
        {
            MessageBox.Show("Please Enter Complete Information", "Invalid");
        }
        else
        {
            compName = textBox1.Text;
            tit = textBox2.Text;
            conName = textBox3.Text;
            phone = textBox4.Text;
            fax = textBox5.Text;
            addr = textBox6.Text;
            pCode = textBox7.Text;
            city = textBox8.Text;
            reg = textBox9.Text;
            cntry = textBox10.Text;

            obj.Add(new searchingConsole.Class1(compName, tit, conName, phone, fax, addr,
                pCode, city, reg, cntry));
            MessageBox.Show("Company Added!", "Registered");
        }
        textBox1.Clear();
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();
        textBox5.Clear();
        textBox6.Clear();
        textBox7.Clear();
        textBox8.Clear();
        textBox9.Clear();
        textBox10.Clear();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        String compName, conName;
        compName = textBox11.Text;
        conName = textBox12.Text;

        if (String.IsNullOrEmpty(textBox11.Text) && String.IsNullOrEmpty(textBox12.Text))
        {
            MessageBox.Show("Enter Company Name and Contact Name", "Invalid");
        }
        else if (String.IsNullOrEmpty(textBox11.Text))
        {
            MessageBox.Show("Enter Company Name", "Invalid");
        }
        else if (String.IsNullOrEmpty(textBox12.Text))
        {
            MessageBox.Show("Enter Contact Name", "Invalid");
        }
        else
        {
            for (int i = 0; i < obj.Count; i++)
            {
                if (obj[i].getCompanyName() == compName && obj[i].getContactName() == conName)
                {
                    MessageBox.Show("Match Found", "Search Result");
                }
                else
                {
                    MessageBox.Show("Match Not Found", "Search Result");
                }

                textBox1.Clear();
                textBox2.Clear();
            }
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        DialogResult result = MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButtons.YesNo);
        if (result == DialogResult.Yes)
        {
            Environment.Exit(0);
        }
        else if (result == DialogResult.No)
        {

        }
    }
}

这是我创建的表单。点击&#39;取消&#39;程序应该退出。 &#39; OK&#39;按钮用于搜索和“应用”#39;按钮用于记录某些数据结构中的数据。

问题是,如果我首先创建一个记录然后退出然后程序退出正常,但如果我先退出它会崩溃。同样,如果搜索某些记录,程序运行正常,但如果文本字段为空,则程序崩溃。

任何帮助?

2 个答案:

答案 0 :(得分:1)

我认为在WPF应用程序中您不使用Environment.Exit(0),而是使用Application.Exit()

让我们看看:

  • Environment.Exit(0) - &GT; &#34;退出此过程并为基础操作系统提供指定的退出代码&#34;。我总是在控制台应用程序上使用它。
  • Application.Exit() - &GT; &#34;通知所有消息泵必须终止,然后在处理消息后关闭所有应用程序窗口&#34;。 你必须在WPF上使用这个。

注意:而不是那些ifelse我认为您可以尝试执行try catch阻止。像这样使用它:

    try
    {
            //Code goes here
    }
    catch(Exception)
    {
            throw; //Say here what you want the message to show like: Insert a text there
    }

答案 1 :(得分:0)

使用Application.Exit()代替Environment.Exit(0)。 如果它仍然崩溃,请将退出代码放在try catch表达式中并读取内部异常。我会告诉你你的问题。