在表单A之前启动表单B,为什么表单A看不到用户认证信息?
本文关键字:表单 用户 看不到 认证 为什么 信息 启动 | 更新日期: 2023-09-27 18:16:34
简单的简介:我有一个程序,需要认证从用户获得访问或创建文件与用户名和密码。我有表格A和表格B;表单A是我的程序的主窗口,其中有一个按钮指向表单B,这是登录表单。就目前而言,我必须启动表单A,然后单击按钮转到表单B,因为必须登录访问文件。我的程序正在识别是否有人已经登录,它启用按钮,让我创建一个新文件或访问已经创建的一个(他们是默认禁用的,只有在身份验证首次成功时才启用)。
我如何在打开表格A之前检查身份验证?
如果我说得不够清楚,请告诉我……
现在,我已经尝试在窗体a之前初始化窗体B:
public MainWindow()
{
AuthenticationWindow login = new AuthenticationWindow();
login.ShowDialog();
InitializeComponent();
}
问题是,当我这样做时,我的程序在身份验证到位后没有启用我的按钮。
我试图在初始化我的表单之前检查身份验证:
public MainWindow()
{
AuthenticationWindow login = new AuthenticationWindow();
login.ShowDialog();
if (storedAuth != null)
{
// Making Deleting and Adding possible
// when file was opened.
tsmiOpen.Enabled = true;
tsmiNew.Enabled = true;
}
InitializeComponent();
}
但是我仍然不能打开或创建文件。似乎程序没有检查已验证的用户。
这是我的代码,使我的按钮认证后:
private void tsmiAuthenticate_Click(object sender, EventArgs e)
{
AuthenticationWindow authWindow = new AuthenticationWindow();
authWindow.ShowDialog();
storedAuth = authWindow.Result;
if (storedAuth != null)
{
tsmiOpen.Enabled = true;
tsmiNew.Enabled = true;
}
}
我的收缩代码:
namespace Password_Manager
{
public partial class MainWindow : Form
{
private AuthenticateUser storedAuth;
private HashPhrase hash = new HashPhrase();
private bool newSelected, openSelected;
public MainWindow()
{
AuthenticationWindow login = new AuthenticationWindow();
login.ShowDialog();
if (storedAuth != null)
{
// Making Deleting and Adding possible
// when file was opened.
tsmiOpen.Enabled = true;
tsmiNew.Enabled = true;
}
InitializeComponent();
}
private void tsmiAuthenticate_Click(object sender, EventArgs e)
{
AuthenticationWindow authWindow = new AuthenticationWindow();
// Displaying Authenticate Window.
// Not allowing switching between forms.
authWindow.ShowDialog();
storedAuth = authWindow.Result;
if (storedAuth != null)
{
// Making Deleting and Adding possible
// when file was opened.
tsmiOpen.Enabled = true;
tsmiNew.Enabled = true;
}
}
private void tsmiAddEntry_Click(object sender, EventArgs e)
{
// Checking if the file is new or opened.
// This matter because we need to
// have appropriate path to the file.
if (openSelected)
{
AddEntryWindow addWindow = new AddEntryWindow
(this, storedAuth.UserName, storedAuth.Password,
ofdOpenFile.FileName);
// Displaying Add Entry Window.
// Not allowing switching between forms so I am using ShowDialog().
addWindow.ShowDialog();
}
if (newSelected)
{
AddEntryWindow addWindow = new AddEntryWindow
(this, storedAuth.UserName, storedAuth.Password,
sfdNewFile.FileName);
// Displaying Add Entry Window.
// Not allowing switching between
// forms so I am using ShowDialog().
addWindow.ShowDialog();
}
}
private void tsmiDeleteEntry_Click(object sender, EventArgs e)
{
// Checking if the file is new or opened.
// This matter because we need to
// have appropriate path to the file.
if (openSelected)
{
// When open file.
DeleteEntryWindow deleteEntyWindow = new DeleteEntryWindow
(this, storedAuth.UserName,
storedAuth.Password, ofdOpenFile.FileName);
deleteEntyWindow.ShowDialog();
}
else if (newSelected)
{
// When new file.
DeleteEntryWindow deleteEntyWindow = new DeleteEntryWindow
(this, storedAuth.UserName,
storedAuth.Password, sfdNewFile.FileName);
deleteEntyWindow.ShowDialog();
}
}
}
}
我猜你的InitializeComponent()代码是重置你的按钮的。enabled属性为false。此方法中的代码是表单设计器在VS.
中创建的代码。试试这个…
public MainWindow()
{
InitializeComponent();
AuthenticationWindow login = new AuthenticationWindow();
login.ShowDialog();
storedAuth = login.Result;
if (storedAuth != null)
{
// Making Deleting and Adding possible
// when file was opened.
tsmiOpen.Enabled = true;
tsmiNew.Enabled = true;
}
}