2014-10-07 111 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