2010-08-09 80 views
6

我目前正在開發一個小型項目並希望獲得一些幫助。沒有退出應用程序的關閉窗體

我有2種形式,第一種是登錄窗口,第二種是主程序。我遇到的問題是,當我用this.Close()關閉form1時,它正在退出整個程序。

我有一種感覺,我需要使用線程或類似的東西,但我找不到合適的資源來解決我的問題。

謝謝。

+2

是這winforms或wpf?這不是一個線程問題,而是一個什麼「this」在你最有可能使用的範圍內的問題。代碼將有所幫助。 – 2010-08-09 18:25:30

+0

可能重複的[關閉窗體,然後調用另一個。](http://stackoverflow.com/questions/2751076/closing-a-form-and-then-call-another-one) – 2010-08-09 18:30:15

+0

我還沒有做很多與winforms,但只是爲了驗證,你正在創建你的登錄屏幕與主窗口,因爲它的父母,不是? – 2010-08-09 18:53:46

回答

6

可以隱藏第一種形式,而不是將其關閉:

this.Hide(); 
Form2 form2 = new Form2(); 
form2.Show(); 
+1

然後我有另一個問題,當我關閉Form2我想關閉隱藏的Form1,我該怎麼做? – 2010-08-09 18:31:12

+0

@Victor:同樣的道理,但您需要提供一種真正關閉這兩種表單並關閉應用程序的方法。 – 2010-08-09 18:33:30

+0

@Victor:Application.Exit()應該訣竅 – 2010-08-09 18:37:33

2

如果你使用WPF,您可以設置Application.MainWindow你的第二個「主要」窗口,關閉您的登錄表單之前。

+0

另一種方式WPF更靈活... – 2010-08-09 18:33:51

+0

實際上在vb.net中,您可以指定應用程序關閉模式。 C#不允許你輕鬆地說。 http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/61b817f8-d7d3-44de-9095-91a6e3f2150c – ioWint 2011-08-21 05:02:56

3

難道你不能改變你的program.cs,以便它運行主窗體,並在主窗體的啓動時創建並顯示登錄窗體,然後隱藏自身(等待登錄發生以顯示自身)?

+1

準確地說,我會說: 請確保你沒有使用第一種形式像這樣加載應用程序... Application.Run(new Form1()); – MilkyWayJoe 2010-08-09 18:29:35

1

Program.cs是您的主要功能所在。如果使用Visual Studio將項目創建爲Windows應用程序,那麼main中會有一個運行在啓動程序時打開的窗體的函數。只需從主登錄表單中獲取登錄信息,然後調用第二個窗口即可。這裏有一個例子:

[STAThread] 
private static void Main(string[] args) 
{ 
    //there's some other code here that initializes the program 

    //Starts the first form (login form) 
    Application.Run(new form1()); 

    //Get the login info here somehow. Maybe save in public members of form1 or 
    // in a global utilities or global user class of some kind 

    //Run the main program 
    Application.Run(new mainProgramForm()); 
} 

編輯:忘了什麼事

我忘了提,如果你得到登錄表單會員,你必須先實例化。這不是一個很好的技術,我建議在此做全球用戶類的想法,但我有需要此方法的程序,因爲我提到這個問題,這裏是例子:

private static void Main(string[] args) 
{ 
    //there's some other code here that initializes the program 

    //Instead of running a new form here, first create it and then run it 
    Form1 form1 = new Form1(); //Creates the form 
    Application.Run(form1);  //Runs the form. Program.cs will continue after the form closes 

    //Get the login info 
    string username = form1.Username; 
    string password = form1.Password; 

    //Get rid of form1 if you choose 
    form1.Dispose(); 

    //Validate the user info 

    //Run the main program 
    Application.Run(new mainProgramForm()); 
} 
0

打開程序。 cs - 在應用程序運行調用之前,您可以做很多事情,包括顯示您的登錄信息。以下是我的一個項目的示例。它試圖找到數據庫連接。如果不能,它會打開一個嚮導並連接到訪問或mssql。如果打開狀態良好,則顯示spalsh屏幕,最後運行該應用程序,否則關閉。

Application.EnableVisualStyles(); 
Application.SetCompatibleTextRenderingDefault(false); 
DialogResult LclResult; 

EESDatabasePersistentData LclPersist; 
LclPersist = new EESDatabasePersistentData(); 
string LclDataType; 
string LclDatabase; 
string LclServer; 
string Password; 
string UserID; 
if (!LclPersist.GetConnection(out LclServer, out LclDatabase, out LclDataType, out UserID, out Password)) 
{ 
     // Run the connection wizard 
     GetConnection(out LclServer, out LclDatabase, out LclDataType, out UserID, out Password); 
} 


if (LclDataType == "ACCESS") 
     InitDataAccess(LclDatabase); 
else if (LclDataType == "SQLSERVER") 
     InitDataSQL(LclServer, LclDatabase, UserID, Password); 
if (OpenGood) 
{ 
     if (!System.Diagnostics.Debugger.IsAttached) 
     { 
       FormSplash.Instance.SetupVersion(); 
       /////////////////////////////////// 
       // If we don't call do events 
       // splash delays loading. 
       Application.DoEvents(); 
       FormSplash.Instance.Show(); 
     } 
     Application.DoEvents(); 

     Application.Run(new FormMentorMain()); 
} 
else 
     Application.Exit(); 
0

什麼你的意見,以創建類似的東西

tray Icon

其所謂的托盤圖標

使用戶可以close all formsapp still running ,,和user can go back to app any time

讓我們開始編碼(注意這是WPF應用程序)

private NotifyIcon m_notifyIcon; 
private ContextMenuStrip m_contextMenu; 
private bool _ForceClose; 

public MainWindow() 
{ 
    InitializeComponent(); 
    CreateNotifyIcon(); 

} 

private void CreateNotifyIcon() 
{ 
    m_contextMenu = new ContextMenuStrip(); 

    ToolStripMenuItem mI1 = new ToolStripMenuItem { Text = "Open" }; 
    mI1.Click += (sender, args) => Maximize(); 
    ToolStripMenuItem mI2 = new ToolStripMenuItem { Text = "Exit" }; 
    mI2.Click += (sender, args) => EndApplication(); 
    m_contextMenu.Items.Add(mI1); 
    m_contextMenu.Items.Add(mI2); 
    //Initalize Notify Icon 
    m_notifyIcon = new NotifyIcon 
    { 
     Text = "Application Title", 
     Icon = new Icon("Icon.ico"), 
     //Associate the contextmenustrip with notify icon 
     ContextMenuStrip = m_contextMenu, 
     Visible = true 
    }; 
} 

,我們有這些功能

protected void Minimize() 
{ 
    Hide(); 
} 

protected void Maximize() 
{ 
    Show(); 
    this.WindowState =WindowState.Normal; 
} 

protected void EndApplication() 
{ 
    Minimize(); 
    Thread.Sleep(1000); 
    _ForceClose = true; 
    WindowState = WindowState.Normal; 
    this.Close(); 
    Environment.Exit(0); 
} 

Window Closing事件偵聽器(不要忘記將其添加)

private void Window_Closing(object sender, CancelEventArgs e) 
{ 
    if (_ForceClose == false) 
    { 
     e.Cancel = true; 
     Minimize(); 
    } 
} 

就是這樣,希望它能幫助你

相關問題