Murf
Murf

Reputation: 146

Controls.Find not working within specific if-statement?

I´m stuck on a problem using controls.find and I can´t find the error.

I´m creating a loginform using different panels.

Panels are created as follows:

 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;
    }

In Form_load:

    Panel login = CreatePanel();
    login.Visible = true;
    Controls.Add(login);

When I start the application, everything is displayed. if I type some text into systemstatus.text isn't gets displayed.

When doing a check if the entered password & username are correct, the strange stuff happens.

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!";
        }
    }

The first 2 times I use controls.find it works, I get the strings entered by the users at the corresponding TextBoxes. When I try to change loginstatus, I get a System.IndexOutOfRangeException. My interpretation of the exception is, that there was no loginstatus found. And I don´t understand why. (the other 2 elements can´t be found within the if as well) Can anyone help?

the controls.find("string",true)[0].Text = "XXX" works, I use it a lot at another part of the application

Any ideas? Thanks in advance, David

Upvotes: 0

Views: 77

Answers (1)

Useme Alehosaini
Useme Alehosaini

Reputation: 3116

You miss the Name property in your code and Controls.Find strictly work on Name

    Login_UsernameTB.Name = "Login_UsernameTB";

So your Code should be something like this:

    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;
    }

Also, the login should be class level but not defined inside the Form_Load like this :

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);
    }
    .....

I have tested it and works fine.

My test code is

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");
        }
    }
}

Result is: I receive the message box MessageBox.Show("Login"); shown and Loginstatus is shown too.

Upvotes: 2

Related Questions