C#中的Windows窗体应用程序立即关闭

C#中的Windows窗体应用程序立即关闭,c#,C#,我刚开始用C#编程,我正试图让我的windows窗体应用程序正常运行。但是,每当我运行它时,它都会立即打开和关闭。每当我在Java中输入类似的代码时,GUI就没有问题。我错过了一些小东西吗 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

我刚开始用C#编程,我正试图让我的windows窗体应用程序正常运行。但是,每当我运行它时,它都会立即打开和关闭。每当我在Java中输入类似的代码时,GUI就没有问题。我错过了一些小东西吗

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

}
}

不要调用
Form1\u FormClosing()
表单1\u加载中
。不确定是否需要,但
都将关闭表单。我怀疑您在表单的
Load`事件中附加了Form1_Load

[编辑]


您注释显示消息框,这将导致消息框在表单加载中显示。窗体未进行任何更改以呈现自身

您在这里所做的是向用户显示一条消息,告诉他/她应用程序的新版本可用

现在,他/她有什么选择

如果回答为否:您关闭应用程序
如果答案是肯定的:您也将其关闭

这正是你在这里做的


请把你的问题弄清楚,以便我们可以帮助你。

尝试将呼叫
MessageBox.Show(消息,标题,
更改为
MessageBox.Show(这,消息,标题,
-这将使消息框成为表单的模态。另一件要检查的事情是如何显示表单-您是否使用VS生成的默认
Main
方法,或者您是否对其进行了任何更改?

如果您想在
表单加载()中结束应用程序,请使用
表单关闭()
窗体的事件,并调用
this.Close();

示例:

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
    }

}

你可以试试这种东西

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

到底是什么问题???您的意思是说您无法查看消息框本身还是仅查看其中的内容

如果消息框本身未显示,则表示未正确调用Form1\u Load事件。请尝试删除该事件,然后右键单击form->Properties重新创建该事件。在事件选项卡中,单击Load,然后在Load事件内再次调用Form1\u FormClosing方法


已尝试在我的系统中执行,表单按预期运行。

你的意思是看不到messagebox吗?是的,这就是我的意思。messagebox在我的桌面上是打开的,但我什么也看不到。如果他们单击“是”,则会指向应用程序的新版本。如果不,则会告诉他们,如果他们想更新,则必须更新使用它,表单应该在那之后关闭。我相信这就是我所拥有的,但到目前为止,它只是在我构建解决方案后立即关闭,我从未看到任何消息。方法
Form1\u OnLoad(object sender,EventArgs e)如何
created?问题是
Form1\u FormClosing
方法似乎不是事件处理程序。看一看:它没有参数。我没有做任何更改。让我试试。我尝试添加了其他建议,但消息框还没有出现。在您的设计器文件中,您是否也看到了这个。Load+=new System、 EventHandler(this.Form1_Load);或者您正在调用另一个表单