表单显示应用程序启动时关闭

时间:2012-07-10 11:34:05

标签: c# winforms visual-studio-2010

这是表单代码,每次我测试一些输入时,应用程序将打开然后立即关闭,我无法弄清楚导致它的原因。

  namespace Assignment2
  {
public partial class IsmClassForm : Form
{
    public IsmClassForm()
    {

        InitializeComponent();
    }
    private void IsmClassForm_Load(object sender, EventArgs e)
    {
    }

    protected Student m_student;
    protected Course m_course;
    protected IsmClassForm m_next;
    protected EnrollmentForm m_home;

     public bool TestPrerequisites()

    {
        if (!m_student.Record.MeetsPrerequisites(m_course.Number))
        {
            MessageBox.Show("The registrar reports that you don't meet the prerequisites for " + m_course.Prefix + m_course.Number.ToString());
            m_student.PridePoints = m_student.PridePoints - 5;
            m_student.Record.Remove(m_course);
            return false;
        }
        return true;
    }
    public string Description
    {
        get
        {
            return textDescription.Text;
        }
        set
        {
            textDescription.Text = value;
        }
    }

    public string Title
    {
        get
        {
            return this.Text;
        }
        set
        {
            this.Text = value;
        }
    }
    public string Welcome
    {
        get
        {
            return labelWelcome.Text;
        }
        set
        {
            labelWelcome.Text = value;
        }
    }



    public bool Initialize(Student student, int course, IsmClassForm next, EnrollmentForm home)
    {
        if (student == null) return false;
        m_student = student;
        m_next = next;
        m_home = home;
        m_course = m_student.Record.FindEnrolled(course);
        if (m_course == null)
        {
            return false;
        }

        labelCourse.Text = m_course.Prefix + "-" + m_course.Number.ToString();
        return TestPrerequisites();

    }


    public enum DropMode
    {
        FreeDrop, PayDrop, Withdraw, NoDrop
    };
    DropMode mState = DropMode.FreeDrop;
    public DropMode Drop
    {
        get
        {
            return mState;
        }
        set
        {
            mState = value;
            UpdateDrop();
        }
    }

    public void UpdateDrop()
    {
        switch (Drop)
        {
            case DropMode.FreeDrop:
                buttonDrop.Text = "Drop";
                break;
            case DropMode.PayDrop:
                buttonDrop.Text = "Drop";
                break;
            case DropMode.Withdraw:
                buttonDrop.Text = "Withdraw";
                break;
            case DropMode.NoDrop:
                buttonDrop.Text = "Done";
                break;
        }

    }

    protected void buttonDrop_Click(object sender, EventArgs e)
    {
        switch (Drop)
        {
            case DropMode.FreeDrop:
                m_student.PridePoints = m_student.PridePoints - 5;
                m_student.Record.Remove(m_course);
                m_course = null;
                break;
            case DropMode.PayDrop:
                m_student.PridePoints = m_student.PridePoints - 10;
                m_student.WealthPoints = m_student.WealthPoints - 500;
                m_student.Record.Remove(m_course);
                m_course = null;
                break;
            case DropMode.Withdraw:
                m_student.PridePoints = m_student.PridePoints - 50;
                m_student.WealthPoints = m_student.WealthPoints - 500;
                m_course.Grade = "W";
                break;
            case DropMode.NoDrop:
                m_student.WealthPoints = m_student.WealthPoints - 500;
                break;
        }
        Close();


    }

    protected void IsmClassForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing)
        {
            //The student not having a grade suggest the buttons were ignored
            if (m_course != null && m_course.Grade == null)
            {
                m_course.Grade = "F";
                m_student.PridePoints = m_student.PridePoints - 100;
                m_student.WealthPoints = m_student.WealthPoints - 500;
            }
            if (m_next != null) m_next.Show();
            else if (m_home != null) m_home.Show();
        }






    }

以下是一些测试输入:

 static void TestIsmClassForm()
    {
        Student tjt1 = new Student("Travis Todd");
        tjt1.Record = new Transcript();
        tjt1.Record.Add(new Course(1, 3113, "B", false));
        tjt1.Record.Add(new Course(1, 3232, "C", false));
        tjt1.Record.Add(new Course(2, 3113, "A", true));
        tjt1.Record.Add(new Course(2, 3232, null, true));
        tjt1.Record.Add(new Course(2, 4220, null, true));
        IsmClassForm f4220 = new IsmClassForm();
        IsmClassForm f3232 = new IsmClassForm();
        IsmClassForm f4212 = new IsmClassForm();
        f4212.Initialize(tjt1, 4212, f3232, null);
        f3232.Initialize(tjt1, 3232, f4220, null);
        f4220.Initialize(tjt1, 4220, null, null);
        f4212.Show();
    }

这确实使用了项目中的其他类及其表单,但是其他函数都可以工作,这是我到目前为止找到的唯一问题。我错过了一些明显的东西吗?

谢谢你, 特拉维斯

3 个答案:

答案 0 :(得分:2)

您有两种方法可以实现这一目标;

鉴于您的输入方法:

public static void Main()     
{         
    TestIsmClassForm();    
} 

您可以使用Application.RunForm.ShowDialog

static void TestIsmClassForm()
{
    ...All of your original code...

    Application.Run(f4212.Show());

    //OR

    f4212.ShowDialog()

}

现在发生的事情是Form.Show是非阻塞的 - 应用程序调用它,继续执行该方法,然后关闭。

Application.Run将显示表单并等到它关闭后再退出应用程序。 Form.ShowDialog()将阻止调用方法,直到窗体关闭。

Application.Run是首选,因为它保证用作应用程序主机的表单在"Main" or "GUI" thread中编组。 ShowDialog()不保证直接从Main()运行(Application.MessageLoop为false)并且可能会发生一些令人惊讶的令人讨厌的线程错误 - 因此大多数时候Application.Run是实现的最佳方式你在做什么。

答案 1 :(得分:0)

确保项目中有Program.cs(这是默认名称)文件。它应该有void Main(string[] args)方法,它将构造表单的实例(即form1)并执行Application.Run(form1)

IsmClassForm_LoadIsmClassForm_FormClosed中放置断点以捕捉表格开启和关闭的时刻。

答案 2 :(得分:0)

表单消失的原因是包含表单的变量/对象超出范围并在测试块的末尾处理掉。

即。一旦'代码'到达结束'}',所有那些形式(和学生)变量将被处理掉。

您可以使用.ShowDialog()替换.Show(),这将导致代码停止,直到手动关闭表单。

MSDN: ShowDialog

MSDN: Variable and Method Scope