2013-03-31 95 views
1

從一些Java開始,我只是在學習C#之後的幾天,所以就像一個簡單的程序,我試圖讓一個程序來保存一些密碼。當this.Close()執行所有窗體關閉?

我將註釋掉問題的代碼。

我主要形式的代碼是:

{ 
public partial class fLogin : Form 
{ 
    public fLogin() 
    { 
     InitializeComponent(); 
    } 

    private void btnLogin_Click(object sender, EventArgs e) 
    { 
     bool checkUser = md5(tbUsername.Text) == Properties.Settings.Default.Username; 
     bool checkPass = md5(tbPassword.Text) == Properties.Settings.Default.Password; 
     if (checkUser && checkPass) 
     { 
      Passwords pFrame = new Passwords(); 
      pFrame.Activate(); 
      this.Hide(); 
     } 
    } 

    private String md5(String tBox) 
    { 
     MD5 md5 = new MD5CryptoServiceProvider(); 
     md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(tBox)); 
     byte[] result = md5.Hash; 
     StringBuilder strBuilder = new StringBuilder(); 
     for (int i = 0; i < result.Length; i++) 
     { 
      strBuilder.Append(result[i].ToString("x2")); 
     } 
     String finalValue = strBuilder.ToString(); 
     return finalValue; 
    } 

    private void fLogin_Load(object sender, EventArgs e) 
    { 
     if (Properties.Settings.Default.Username == "null" || Properties.Settings.Default.Password == "null") 
     { 
      //This is where I create my second Form!. 
      //CreatePassword newPass = new CreatePassword(); 
      //this.Hide(); 
      //newPass.Show(); 
     } 
     else if (md5(tbUsername.Text) == Properties.Settings.Default.Username && md5(tbPassword.Text) == Properties.Settings.Default.Password) 
     { 
      Passwords pFrame = new Passwords(); 
      this.Hide(); 
      pFrame.Show(); 
     } 
    } 

    private void btnClose_Click(object sender, EventArgs e) 
    { 
     Application.Exit(); 
    } 

    private void btnCPass_Click(object sender, EventArgs e) 
    { 
     ChangePass CPFrame = new ChangePass(); 
     CPFrame.ShowDialog(this); 
    } 
    } 
} 

我的第二形態的代碼是

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

namespace PassMan 
{ 
public partial class CreatePassword : Form 
{ 
    public CreatePassword() 
    { 
     InitializeComponent(); 
    } 

    private void btnSave_Click(object sender, EventArgs e) 
    { 
     String username = md5(tbUsername.Text); 
     String password = md5(tbPassword.Text); 
     String[] pwds = { username, password }; 
     Properties.Settings.Default.Username = username; 
     Properties.Settings.Default.Password = password; 
     Properties.Settings.Default.Save(); 
     //This is where I Exit. It Closes this form & the other form! 
     //this.Close(); 

    } 

    private String md5(String tBox) 
    { 
     MD5 md5 = new MD5CryptoServiceProvider(); 
     md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(tBox)); 
     byte[] result = md5.Hash; 
     StringBuilder strBuilder = new StringBuilder(); 
     for (int i = 0; i < result.Length; i++) 
     { 
      strBuilder.Append(result[i].ToString("x2")); 
     } 
     String finalValue = strBuilder.ToString(); 
     return finalValue; 
    } 

    private void btnCancel_Click(object sender, EventArgs e) 
    { 
     Application.Exit(); 
    } 

    protected override void OnFormClosing(FormClosingEventArgs e) 
    { 
     Application.Exit(); 
    } 


} 
} 
+0

添加的FormClosing事件處理程序在Form1中,在其上設置一個斷點,並期待在調用堆棧時觸發。 –

+0

我已更新我的答案,以更好地解釋您的問題。 – scartag

回答

4

這種行爲是因爲你已經設置thisnewForm父,當你叫newForm.ShowDialog(this);

因此當您撥打this.Close()時,它將關閉this表單及其所有子女f奧姆斯。

更新

從更新的問題(代碼追加)我注意到,你在你的CreatePassword形式的窗體關閉事件關閉整個應用程序。

protected override void OnFormClosing(FormClosingEventArgs e) 
    { 
     Application.Exit(); //comment this out. 
    } 

註釋掉Application.Exit();

相關問題