如何解决Controls.Find 在特定的 if 语句中不起作用?
我在使用 controls.find
时遇到问题,但找不到错误。
我正在使用不同的面板创建一个 loginform
。
面板创建如下:
public Panel CreatePanel()
{
Panel login = new Panel();
TextBox Login_UsernameTB = new TextBox();
TextBox Login_PasswordTB = new TextBox();
Label label1 = new Label();
Label label2 = new Label();
Label loginstatus = new Label();
Button Login_Loginbtn = new Button();
Button Login_Registerbtn = new Button();
PictureBox Login_Logo = new PictureBox();
PictureBox Login_ECard = new PictureBox();
//creatingPanel
login.Location = new Point(0,0);
login.Name = "Login";
login.Size = new Size(1000,400);
//locations
Login_ECard.Location = new Point(500,250);
Login_ECard.SizeMode = PictureBoxSizeMode.StretchImage;
Login_ECard.Image = PatientSimulator.Properties.Resources.Ecard;
Login_ECard.Name = "Login_Ecard";
Login_Logo.Location = new Point(148,12);
Login_Logo.SizeMode = PictureBoxSizeMode.StretchImage;
Login_Logo.Image = PatientSimulator.Properties.Resources.Logo;
Login_Logo.Name = "Login_Logo";
label1.Location = new Point(31,200);
label1.Size = new Size(58,13);
label1.Text = "Username:";
label1.Name = "label1";
label2.Location = new Point(31,244);
label2.Size = new Size(56,13);
label2.Text = "Password:";
label2.Name = "label2";
Login_UsernameTB.Location = new Point(148,197);
Login_UsernameTB.Size = new Size(359,20);
Login_UsernameTB.Text = "Testfirma1";
Login_UsernameTB.Name = "Login_UsernameTB";
Login_PasswordTB.Location = new Point(148,241);
Login_PasswordTB.Size = new Size(359,20);
Login_PasswordTB.Text = "Hallo1234#";
Login_PasswordTB.PasswordChar = '*';
Login_PasswordTB.Name = "Login_PasswordTB";
Login_Loginbtn.Location = new Point(432,334);
Login_Loginbtn.Size = new Size(75,23);
Login_Loginbtn.Text = "Login";
Login_Loginbtn.Click += Login_Loginbtn_Click;
loginstatus.Location = new Point(323,360);
loginstatus.Size = new Size(300,20);
loginstatus.Text = "";
loginstatus.Name = "loginstatus";
Login_Registerbtn.Location = new Point(323,334);
Login_Registerbtn.Size = new Size(75,23);
Login_Registerbtn.Text = "Register";
Login_Registerbtn.Click += Login_Registerbtn_Click;
login.Controls.Add(Login_ECard);
login.Controls.Add(Login_Logo);
login.Controls.Add(label1);
login.Controls.Add(label2);
login.Controls.Add(Login_UsernameTB);
login.Controls.Add(Login_PasswordTB);
login.Controls.Add(Login_Loginbtn);
login.Controls.Add(Login_Registerbtn);
login.Controls.Add(loginstatus);
return login;
}
在 Form_load 中:
Panel login = CreatePanel();
login.Visible = true;
Controls.Add(login);
当我启动应用程序时,一切都会显示出来。如果我在 systemstatus.text
中输入一些文本,则不会显示。
在检查输入的密码和用户名是否正确时,奇怪的事情发生了。
private void Login_Loginbtn_Click(object sender,EventArgs e)
{
Patient.DBAccess db = new Patient.DBAccess();
Sha256 sha = new Sha256();
string username = login.Controls.Find("Login_UsernameTB",true)[0].Text;
string userpw = login.Controls.Find("Login_PasswordTB",true)[0].Text;
Patient.DatabaseRequestInterface.UserInterface user = new Patient.DatabaseRequestInterface.UserInterface();
user.Username = username;
if (db.IsUserExisting(user)){
user = db.ReadUserPassword(user);
string salt = Encoding.ASCII.GetString(user.PasswordSalt);
byte[] pw = sha.GetSha256(userpw,salt);
if (pw.SequenceEqual(user.PasswordHash))
{
//Programm starten
//token generieren
Patient.DatabaseRequestInterface.UserInterface useri = db.ReadFullUser(user);
UserAuthentificationToken = new UserInfo(useri.Username,useri.Uuid,DateTime.Now);
LoginSuccessEvent?.Invoke(this,new EventArgs());
login.Controls.Find("loginstatus",true)[0].Text = "Login successfull";
this.Close();
}
else
{
MessageBox.Show("Login failed,Username and/or password incorrect");
}
}
else
{
MessageBox.Show("Falscher User oder Passwort");
//Controls.Find("Login_StatusL",true)[0].Text = "Login failed,Username and/or password incorrect!";
}
}
我使用 controls.find
的前 2 次它有效,我在相应的 TextBoxes
处得到用户输入的字符串。当我尝试更改 loginstatus
时,我得到一个 System.IndexOutOfRangeException
。我对异常的解释是,没有找到 loginstatus
。我不明白为什么。 (在 if 中也找不到其他 2 个元素)
有人可以帮忙吗?
controls.find("string",true)[0].Text = "XXX"
有效,我在应用程序的其他部分经常使用它
有什么想法吗? 提前致谢, 大卫
解决方法
您错过了代码中的 Name
属性并且 Controls.Find
严格执行 Name
Login_UsernameTB.Name = "Login_UsernameTB";
所以你的代码应该是这样的:
public Panel CreatePanel()
{
Panel login = new Panel();
login.Size = new Size(900,900);
TextBox Login_UsernameTB = new TextBox();
Login_UsernameTB.Name = "Login_UsernameTB";
Login_UsernameTB.Location = new Point(50,50);
Login_UsernameTB.Text = "Username here";
TextBox Login_PasswordTB = new TextBox();
Login_PasswordTB.Name = "Login_PasswordTB";
Login_PasswordTB.Location = new Point(150,50);
Login_PasswordTB.Text = "password here";
//stuff is asigned to TBs...
Label loginstatus = new Label();
loginstatus.Location = new Point(150,150);
loginstatus.Size = new Size(300,20);
loginstatus.Text = "";
loginstatus.Name = "loginstatus";
login.Controls.Add(Login_UsernameTB);
login.Controls.Add(Login_PasswordTB);
login.Controls.Add(loginstatus);
return login;
}
另外,login
应该是类级别的,但不在 Form_Load
中定义
像这样:
public partial class Form1 : Form
{
Panel login;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs e)
{
login = CreatePanel();
login.Visible = true;
Controls.Add(login);
}
.....
我已经对其进行了测试并且运行良好。
我的测试代码是
using System;
using System.Drawing;
using System.Windows.Forms;
namespace SackOverflow
{
public partial class Form1 : Form
{
Panel login;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs e)
{
login = CreatePanel();
login.Visible = true;
Controls.Add(login);
}
public Panel CreatePanel()
{
Panel login = new Panel();
login.Size = new Size(900,900);
TextBox Login_UsernameTB = new TextBox();
Login_UsernameTB.Name = "Login_UsernameTB";
Login_UsernameTB.Location = new Point(50,50);
Login_UsernameTB.Text = "Username";
TextBox Login_PasswordTB = new TextBox();
Login_PasswordTB.Name = "Login_PasswordTB";
Login_PasswordTB.Location = new Point(150,50);
Login_PasswordTB.Text = "password";
//stuff is asigned to TBs...
Label loginstatus = new Label();
loginstatus.Location = new Point(150,150);
loginstatus.Size = new Size(300,20);
loginstatus.Text = "";
loginstatus.Name = "loginstatus";
login.Controls.Add(Login_UsernameTB);
login.Controls.Add(Login_PasswordTB);
login.Controls.Add(loginstatus);
return login;
}
private void button1_Click(object sender,EventArgs e)
{
if (true)
{
string username = login.Controls.Find("Login_UsernameTB",true)[0].Text;
string userpw = login.Controls.Find("Login_PasswordTB",true)[0].Text;
//UserInterface user = //some database stuff to get the password saved by the user
if ("123" == userpw)
{
login.Controls.Find("loginstatus",true)[0].Text = "Logging in";
Login();
}
else
{
login.Controls.Find("loginstatus",true)[0].Text = "Error";
}
}
}
private void Login()
{
MessageBox.Show("Login");
}
}
}
结果是:
我收到消息框 MessageBox.Show("Login");
shown 并且 Loginstatus
也shown。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。