0

这是表单代码,每次我测试一些输入时,应用程序都会打开然后立即关闭,我不知道是什么原因造成的。

  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();
    }

这确实使用了项目中的一些其他类及其形式,但我其他功能都可以工作,这是我迄今为止发现的唯一问题。我错过了一些明显的东西吗?

谢谢你,特拉维斯

4

3 回答 3

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 是首选,因为它保证用作应用程序主机的表单在“主”或“GUI”线程中编组。ShowDialog() 不保证直接从 Main() 运行时(Application.MessageLoop为 false),并且可能会发生一些令人惊讶的烦人的线程错误 - 所以大多数情况下 Application.Run 是实现您的目标的最佳方式是做。

于 2012-07-10T11:47:38.377 回答
0

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

放置断点IsmClassForm_LoadIsmClassForm_FormClosed捕捉表单打开和关闭的时刻。

于 2012-07-10T11:43:51.660 回答
0

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

即,一旦“代码”到达结束“}”,所有这些表单(和学生)变量都将被处理掉。

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

MSDN:显示对话框

MSDN:变量和方法范围

于 2012-07-10T11:52:18.633 回答