开发者

Windows Form Application in C# Closes Immediately

I just started programming with C# and I'm trying to get my windows form application to function properly. However, whenever I run it, it just opens up and closes immediately. Whenever I type similar code into Java, there's no problem with the GUI. Did I miss something small here?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : F开发者_StackOverfloworm
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Form1_FormClosing();
    }



    private void Form1_FormClosing()
    {
        const string message =
            "There's an updated version of this program available. Would you like to download now?";
        const string caption = "Please update";
        var result = MessageBox.Show(message, caption,
                                     MessageBoxButtons.YesNo,
                                     MessageBoxIcon.Question);

        // If the no button was pressed ...
        if (result == DialogResult.No)
        {
            MessageBox.Show("Program will close now. If you want to use this program please update to the newest version.", "Please update");
            this.Close();
        }
        else if (result == DialogResult.Yes)
        {
            System.Diagnostics.Process.Start("http://www.google.com");
            this.Close();
        }
    }

}
}


Don't call Form1_FormClosing(); within the Form1_Load. Not sure if you wanted that but both No and Yes will close the form. I suspect you have the Form1_Loadattached to theLoad` event of the form.

[Edit]

You comment that the message box is shown which will happen cause it is being displayed within the Load of the form. The form has not had a change to render itself.


What you're doing here is showing a message to the user telling him/her that a new version of the application is available.

Now, what choise does he/she have?

If the answer is no: You close the application
If the answer is yes: You close it too

That's exactly what you're doing here.

Please, make your question more clear so we can help you.


Try changing call MessageBox.Show(message, caption, ... to MessageBox.Show(this, message, caption, ... - that would make the message box modal to the form. Another thing to check is how you are showing your form - are you using default Main method generated by VS or have u made any changes to it?


You can try something of this sort

DialogResult result = MessageBox.Show(message, caption,
                                         MessageBoxButtons.YesNo,
                                         MessageBoxIcon.Question, 
                                         MessageBoxDefaultButton.Button1, 
                                         MessageBoxOptions.DefaultDesktopOnly); 


If you want to end the application in Form_Load(). Use FormClosing() event of Form, and call this.Close();.

EXAMPLE:

private void Form1_Load(object sender, EventArgs e)
{
   this.Close();  //this will call Form_Closing()

}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{

    //do stuff
    if (result == DialogResult.No)
    {
          e.Cancel = true;        // if you don't want to close your form 
    }
    else
    {
        // do stuff on closing form
    }

}


What exactly is the problem??? Do you mean to say you are not able to view the message box itself or just the contents in it??

If the message box itself is not shown, then it means the Form1_Load event is not getting called properly. Try deleting the event and create it again by right clicking on the form ->Properties. In the event tab, click Load and then call the Form1_FormClosing method again inside the Load event.

Tried executing in my system and the form is working as expected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜