简单简介:我有一个程序,需要用户进行身份验证才能访问或创建带有用户名和密码的文件。我有表格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();
}
}
}
}
答案 0 :(得分:1)
我的猜测是你的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;
}
}