当this.Close()执行时,所有表单都关闭吗? [英] When this.Close() executed all forms close?
问题描述
过去几天来,我只是从某种Java中学习C#,因此,作为一种简单的程序,我正在尝试制作一个包含一些密码的程序.
I've only been learning C# the past few days after coming from some Java, so just as a sort of simple program I'm trying to make a program to hold some passwords.
我将注释掉有此问题的代码.
I'll comment out the code with the problem.
我的主要表格代码是:
{
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();
}
}
}
推荐答案
该行为是因为您在调用newForm.ShowDialog(this);
That behaviour is because you have set this
as the parent of newForm
when you called newForm.ShowDialog(this);
因此,当您调用this.Close()
时,它将关闭this
表单及其所有子表单.
so when you call this.Close()
it will close the this
form and all its child forms.
更新
从更新后的问题(添加代码)中,我注意到您正在关闭CreatePassword表单的表单关闭事件中的整个应用程序.
From your updated question (code addition) i've noticed that you are closing the entire application in your form closing event of your CreatePassword form.
protected override void OnFormClosing(FormClosingEventArgs e)
{
Application.Exit(); //comment this out.
}
注释Application.Exit();
这篇关于当this.Close()执行时,所有表单都关闭吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!