C# 使用用户控件更改用户
我在做什么:驻留在一个窗口中的多页程序 我想做什么:我有一个身份验证功能设置,我还想添加一个“更改用户”功能,这样我就不需要关闭程序并重新打开它 这是我的身份验证码:C# 使用用户控件更改用户,c#,winforms,authentication,C#,Winforms,Authentication,我在做什么:驻留在一个窗口中的多页程序 我想做什么:我有一个身份验证功能设置,我还想添加一个“更改用户”功能,这样我就不需要关闭程序并重新打开它 这是我的身份验证码: using System.Data.SqlClient; namespace Dictionary { public partial class Authenticator : UserControl { public Authenticator() { I
using System.Data.SqlClient;
namespace Dictionary
{
public partial class Authenticator : UserControl
{
public Authenticator()
{
InitializeComponent();
}
private void Exit_B_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Want to Exit?", "Exit",
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
== DialogResult.Yes)
((Form)this.TopLevelControl).Close();
}
private void Login_B_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\OverL\OneDrive\Documents\Authenticator01.mdf;Integrated Security=True;Connect Timeout=30");
SqlDataAdapter sda = new SqlDataAdapter("Select Permissions from Login Where Username = '" + UsrTxt.Text + "' and Password = '" + PwTxt.Text + "'", con);
sda.Fill(dt);
if (dt.Rows.Count == 1)
{
Program.MainForm.ShowControl(ControlsEnum.MainHub);
}
else
{
MessageBox.Show("Invalid Credentials!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
下面是我想要“更改用户”事件的代码:
namespace Dictionary
{
public partial class MainHub : UserControl
{
public MainHub(string permissions="")
{
InitializeComponent();
}
private void changeUserToolStripMenuItem_Click(object sender, EventArgs e)
{
Program.MainForm.ShowControl(ControlsEnum.Authenticator); //This is what i tried, it pretty much switches to the Authenticator interface and all the buttons work, but when i login it would say "invalid credentials"-this deriving from code, so it gets to the Authentication code but for some reason doesn't find the usernames-&passwords
}
}
}
{
public enum ControlsEnum
{
Authenticator,
MainHub,
YL_1,
YL_2
}
public partial class ETMainForm : Form
{
private IDictionary<ControlsEnum, Control> controls = new Dictionary<ControlsEnum, Control>();
public ETMainForm()
{
InitializeComponent();
ShowControl(ControlsEnum.Authenticator);
}
public void ShowControl(ControlsEnum ctrl)
{
Control new_ctrl = null;
if (controls.ContainsKey(ctrl))
{
new_ctrl = controls[ctrl];
}
else
{
new_ctrl = CreateControl(ctrl);
controls[ctrl] = new_ctrl;
}
new_ctrl.Parent = this;
new_ctrl.Dock = DockStyle.Fill;
new_ctrl.BringToFront();
new_ctrl.Show();
}
private Control CreateControl(ControlsEnum ctrl)
{
switch (ctrl)
{
case ControlsEnum.Authenticator:
return new Authenticator();
case ControlsEnum.MainHub:
return new MainHub();
case ControlsEnum.YL_1:
return new YL_1();
case ControlsEnum.YL_2:
return new YL_2();
default:
return null;
}
}
}
}
这里是MainHub代码,这样所有代码都可以通过:
namespace Dictionary
{
public partial class MainHub : UserControl
{
public MainHub(string permissions="")
{
InitializeComponent();
}
private void changeUserToolStripMenuItem_Click(object sender, EventArgs e)
{
Program.MainForm.ShowControl(ControlsEnum.Authenticator); //This is what i tried, it pretty much switches to the Authenticator interface and all the buttons work, but when i login it would say "invalid credentials"-this deriving from code, so it gets to the Authentication code but for some reason doesn't find the usernames-&passwords
}
}
}
{
public enum ControlsEnum
{
Authenticator,
MainHub,
YL_1,
YL_2
}
public partial class ETMainForm : Form
{
private IDictionary<ControlsEnum, Control> controls = new Dictionary<ControlsEnum, Control>();
public ETMainForm()
{
InitializeComponent();
ShowControl(ControlsEnum.Authenticator);
}
public void ShowControl(ControlsEnum ctrl)
{
Control new_ctrl = null;
if (controls.ContainsKey(ctrl))
{
new_ctrl = controls[ctrl];
}
else
{
new_ctrl = CreateControl(ctrl);
controls[ctrl] = new_ctrl;
}
new_ctrl.Parent = this;
new_ctrl.Dock = DockStyle.Fill;
new_ctrl.BringToFront();
new_ctrl.Show();
}
private Control CreateControl(ControlsEnum ctrl)
{
switch (ctrl)
{
case ControlsEnum.Authenticator:
return new Authenticator();
case ControlsEnum.MainHub:
return new MainHub();
case ControlsEnum.YL_1:
return new YL_1();
case ControlsEnum.YL_2:
return new YL_2();
default:
return null;
}
}
}
}
{
公共枚举控制
{
验证者,
主枢纽,
YL_1,
YL_2
}
公共部分类ETmain表单:表单
{
私有IDictionary控件=新字典();
公共ETMainForm()
{
初始化组件();
ShowControl(ControlsEnum.Authenticator);
}
公共无效显示控件(ControlsEnum ctrl)
{
控件新建\u ctrl=null;
if(controls.ContainsKey(ctrl))
{
new_ctrl=控件[ctrl];
}
其他的
{
新建\u ctrl=CreateControl(ctrl);
控件[ctrl]=新建\u ctrl;
}
new_ctrl.Parent=this;
new_ctrl.Dock=DockStyle.Fill;
新的_ctrl.BringToFront();
new_ctrl.Show();
}
专用控件CreateControl(ControlsEnum ctrl)
{
开关(ctrl)
{
病例对照组验证者:
返回新的验证器();
案例控制senum.MainHub:
返回新的MainHub();
病例对照SENUM.YL_1:
返回新的YL_1();
病例对照SENUM.YL_2:
返回新的YL_2();
违约:
返回null;
}
}
}
}
问题:为什么我实现的代码找不到正确的凭据
Program.MainForm.ShowControl(ControlsEnum.Authenticator)代码>以及如何修复它那么问题是什么?@ZoharPeled更新了,问题是为什么我实现的代码找不到正确的凭据“Program.MainForm.ShowControl(ControlsEnum.Authenticator);”以及如何修复问题是什么?@ZoharPeled更新了,问题是为什么我实现的代码找不到正确的凭证“Program.MainForm.ShowControl(ControlsEnum.Authenticator);”以及如何修复它