当this.Close()执行所有表单关闭?

时间:2013-03-31 15:09:47

标签: c# forms

我从一些Java开始过去几天才学习C#,所以就像一个简单的程序我试图制作一个程序来保存一些密码。

我会用问题注释掉代码。

我的主要表格代码是:

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


}
}

1 个答案:

答案 0 :(得分:4)

这种行为是因为您在调用this

时将newForm设为newForm.ShowDialog(this);的父级

因此,当您致电this.Close()时,它会关闭this表单及其所有子表单。

<强>更新

根据您更新的问题(代码添加),我注意到您正在关闭CreatePassword表单的表单结束事件中的整个应用程序。

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

评论Application.Exit();