0

This is the form code and every time I test some inputs, the application will open and then immediately close and I can't figure out what is causing it.

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






    }

And here are some test inputs:

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

This does use some other classes in the project and their forms, but I the other functions all work and these is the only problem I have found so far. Am I missing something glaringly obvious?

Thank you, Travis

dash
  • 89,546
  • 4
  • 51
  • 71
Randy B.
  • 453
  • 4
  • 20
  • Do you get any error, are you using a try catch somewhere ? – JohnnBlade Jul 10 '12 at 11:37
  • 4
    use `Application.Run(f4214)` to show your form, it shall not exit. – Furqan Hameedi Jul 10 '12 at 11:38
  • Where are you calling TestIsmClassForm() from? Do you have a static void Main() method? If you don't call Application.Run then you will have to call f4212.ShowDialog() as otherwise, the form is shown, then the app exits - ShowDialog() will wait (block) in Main until you cose the form. Application.Run is the best way to achieve this though. – dash Jul 10 '12 at 11:39
  • Yeah I am using a static void Main() method to call it. I don't get any errors the window simply opens for a split second and then closes. – Randy B. Jul 10 '12 at 11:42
  • Use a `try` `catch` in your main to know what exception is being thrown ( or you can also look into app domain Unhandled exceptions) What do you have in your `Main`, you are launching which form via `Application.Run` ? – V4Vendetta Jul 10 '12 at 11:46
  • I have TestIsmClassForm(); in my main function to call the form. – Randy B. Jul 10 '12 at 12:10

3 Answers3

2

You have two ways to achieve this;

Given your entry method:

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

You can use Application.Run or Form.ShowDialog:

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

    Application.Run(f4212.Show());

    //OR

    f4212.ShowDialog()

}

What is happening right now is that Form.Show is non-blocking - the application calls it, continues on out of the method, and closes.

Application.Run will show the form and wait until it closes before exiting the application. Form.ShowDialog() will block the calling method until the form is closed.

Application.Run is preferred because it guarantees the form used as the application host is marshalled in the "Main" or "GUI" thread. ShowDialog() makes no guarantee when run direct from Main()(Application.MessageLoop is false) and it is possible for some surprisingly annoying threading bugs to happen - so the majority of the time Application.Run is the best way to achieve what you are doing.

Community
  • 1
  • 1
dash
  • 89,546
  • 4
  • 51
  • 71
  • Okay, this got the window to open, but the button on the window won't work or anything. – Randy B. Jul 10 '12 at 11:51
  • @T.Todd - Have you attached event handlers in the form? You have to attach event handlers, either manually, or via the designer (which usually does this in the InitializeComponent call embedded in the constructor of designed form and user control classes) – dash Jul 10 '12 at 11:53
  • Yes, I have all of the event handlers in the IsmClassForm class. – Randy B. Jul 10 '12 at 11:57
  • In your `public bool Initialize` method add the event handler explicitly - `buttonDrop.Click += buttonDrop_Click;` Then try it - use the debugger to make sure the method is actually being called and to check what is happening. You may want to raise this as a new question to separate out the startup issue from your new issue - otherwise you'll end up with lots of bits on answers. – dash Jul 10 '12 at 12:00
  • Okay, I added it and now the button works, but it just closes the window when I click it. – Randy B. Jul 10 '12 at 12:14
  • That's because you call `Close();` at the end of your buttonDrop_Click method ;-) – dash Jul 10 '12 at 12:15
  • Okay, I get it now! Sorry, very new to this. With my test inputs though, I called to Transcript, which is another form within my project. Why doesn't the call bring up a transcript with the classes I added to it? – Randy B. Jul 10 '12 at 12:22
  • When you want to show a form, you first need to create it ('Form myForm = new Form();') then you need to 'Show' it - 'myForm.Show();' or 'myForm.ShowDialog();'. Are you doing this? Take a look at the tutorial videos here to really get to grips with Windows Forms: http://windowsclient.net/learn/ – dash Jul 10 '12 at 12:31
0

Make sure there is a Program.cs (that's the default name) file in your project. It should have void Main(string[] args) method, which will construct an instance of the form (namely, form1) and do Application.Run(form1).

Place breakpoints in IsmClassForm_Load and IsmClassForm_FormClosed to catch the moments of the form opening and closing.

bohdan_trotsenko
  • 5,167
  • 3
  • 43
  • 70
0

The reason the form disappears is that the variable/object that contains the form goes out of scope and is disposed off at the end of your test block.

i.e. As soon as the 'code' reaches the closing '}' all of those form (and the Student) variables will be disposed of.

You could replace the .Show(), with .ShowDialog() which will cause the code to stop until the form is manually closed.

MSDN: ShowDialog

MSDN: Variable and Method Scope

cjb110
  • 1,310
  • 14
  • 30