2014-10-07 109 views
0

早上好,不能关闭表格

我有几个问题,但我不确定哪个是重要问题,所以我会先说明我的整体问题。我无法关闭我的Winform应用程序。我搜索并找到了很多答案,但他们要么不工作,我不明白,或两者兼而有之。

如果我做了我所有的工作,然后调用Application.Exit,表单永远不会关闭。如果我放这个,结果相同。关闭。但是,如果我在表单上放置一个按钮并调用Application.Exit,它将关闭表单。

我显然不理解这个流程,我希望对某人清楚我正在尝试做什么。作为一名非程序员,我一直在为这个项目拼凑几个月,这是我的最后一步 - 如果从命令行运行参数运行,完成工作后关闭表单。我会尝试更长的时间来解决它,但我的Visual Studio试用期结束后这一周,所以我把专家的意见:)

谢谢 托德

的Program.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace ProgramCSToormTest 
{ 
static class Program 
{ 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main(String[] args) 
    { 

     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     //add if 
     if (args.Length == 0) 
     { 
      Application.Run(new Form1("Form")); 
     } 
     else 
     { 
      Application.Run(new Form1(args[0])); 

     } 
    } 


} 
} 

Form1上。 cs

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

namespace ProgramCSToormTest 
{ 

public partial class Form1 : Form 
{ 
    string CLArg1; 
    string ReturnText; 
    public Form1(string Arg1) 
    { 
     InitializeComponent(); 
     if (Arg1 != null) 
     { 
      CLArg1 = Arg1; 
      textBox1.Text = Display(CLArg1); 
      //button1.Enabled = false; 
     } 
     else 
     { 
      textBox1.Text = "click button to start"; 
     } 
     Application.Exit(); //This seems to be ignored 
    } 

    public void button1_Click(object sender, EventArgs e) 
    { 
     CLArg1 = null; 
     textBox1.Text = Display("Hello World"); 
     Application.Exit(); 
    } 
    public string Display(string DisplayText) 
    { 
     if (CLArg1 != null) 
     { 
      ReturnText = CLArg1; 
      return(ReturnText); 

     } 
     else 
     { 
      ReturnText = DisplayText; 
      return(ReturnText); 
     } 

    } 
} 
}  
+1

您可以下载visual studio express for c#。它的免费,并会让你继续。 :) – paqogomez 2014-10-07 15:43:52

+2

你试图退出应用程序的构造函数将是我敢打赌,为什么它不工作。如果您要在创建表单之前删除表单,那么使用表单有什么意义。 – Hammerstein 2014-10-07 15:45:19

+0

谢谢@paqogomez - 我必须检查一下。 – 2014-10-07 15:52:16

回答

1

请参阅this的问题。 Application.Close()仅适用于已创建应用程序的情况。这通过调用Application.Run()完成。现在。在您的代码中,您可以从表单的构造函数中调用Application.Exit()。在需要创建应用程序的Application.Run()之前执行。

要解决此问题,请等到Application.Run()之后。或者,如果您想在构造函数中使用Environment.Exit(int statusCode)退出应用程序。当使用Environment.Exit(int statusCode)时请记住this

+1

应该指出的是'Application.Exit,Close等'不应该放入构造函数中。 – TyCobb 2014-10-07 15:51:49

+0

确实。构造函数是为了设置一个对象,而不应该用它来做任何的繁琐的提升。如果你真的想从构造函数中退出应用程序,一定要在构造函数的注释中提到它。但应尽可能避免。 – TheDutchDevil 2014-10-07 15:56:00

+0

谢谢@TheDutchDevil和TyCobb - 我将不得不查看链接,看看我能否理解你告诉我的内容。我想我的错误信念是Program.c中的Application.Run发生在我认为你调用构造函数之前。 – 2014-10-07 16:05:21

0

当窗体从程序类加载时,无法关闭应用程序。加载表单后请尝试调用Exit方法:

private void Form1_Load(object sender, EventArgs e) 
    { 
     if (CLArg1 != String.Empty) 

     Application.Exit(); 
    } 
+0

谢谢@MikeG – 2014-10-07 18:33:43