Question login logic wrong?

Eiweisshakee

New member
Joined
Nov 22, 2022
Messages
3
Programming Experience
1-3
Hello, I'm currently trying something out with c Sharp and wanted to see if I could manage a login with multiple forms. Didn't work out so well. Attached is the source code, the point is that if you have successfully logged in, you cannot switch the forms because the bool is apparently read incorrectly. Would be nice if you can give me tips in which direction I could try it

Login class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Koloniya_ControlPanel
{
    public partial class Login : Form
    {

        bool loggedIn;
        bool isAdmin;
        bool isMod;

        public Login()
        {
            InitializeComponent();
            textBox2.PasswordChar = '\u25CF';
        }



        public bool Enabled
        {
            get
            {
                return this.loggedIn;
            }
            set
            {
                this.loggedIn = value;
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        public void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "Eiweisshakee" && textBox2.Text == "87237597485" || textBox1.Text == "Schokoshakee" && textBox2.Text == "77899652254" || textBox1.Text == "Cholian" && textBox2.Text == "36577546465")
            {
                this.Enabled = true;
                Console.Out.WriteLine(loggedIn);
                isAdmin = true;
                isMod = true;
                MessageBox.Show("Erfolgreich eingeloggt!");
            }
            else if(textBox1.Text == "Seylooo" && textBox2.Text == "82935976898" || textBox1.Text == "Snackii" && textBox2.Text == "46562835282" || textBox1.Text == "dia78" && textBox2.Text == "99885962382")
            {
                loggedIn = true;
                isAdmin = false;
                isMod = true;
                MessageBox.Show("Erfolgreich eingeloggt!");
            }
            else
            {
                MessageBox.Show("Falsche Accountdaten!");
            }
        }
    }
}


Main class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Koloniya_ControlPanel;

namespace Koloniya_ControlPanel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        public void loadForm(object Form)
        {
            if (this.mainpanel.Controls.Count > 0)
                this.mainpanel.Controls.RemoveAt(0);
            Form f = Form as Form;
            f.TopLevel = false;
            f.Dock = DockStyle.Fill;
            this.mainpanel.Controls.Add(f);
            this.mainpanel.Tag = f;
            f.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            loadForm(new Login());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Login f = new Login();
            if (f.Enabled.Equals(true))
            {
                loadForm(new Spielerabfrage());
            }
            else
            {
                MessageBox.Show("Nicht eingeloggt!");
            }
            
        }
    }
}
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,066
Location
Chesapeake, VA
Programming Experience
10+
Do not post your code as screenshots. Please post your code as text in code tags. Trying to read code in screenshots on small devices is very difficult.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,066
Location
Chesapeake, VA
Programming Experience
10+
First of all, creating a form and then adding that form as a child control of your main form is a recipe for disaster. Yes, it sort of works, but this is not what you want to do. You'll want to create an custom ApplicationContext and swap the main form around.


Also try to do some searches for Jessica Fosler's old blog posts about how to correctly implement a Notify Icon application using WinForms. Alternatively search for various blog posts about how to implement splash screens for WinForms. They both involve playing games with the ApplicationContext.

Another alternative is simply use ShowDialog() to show your login form instead of playing games with the ApplicationContext.

If you really want to have multiple login forms, consider using MDI mode, rather than SDI mode in your WinForms program.

you cannot switch the forms because the bool is apparently read incorrectly
Which boolean are you referring to? If you are referring to Enabled on line 42 of your main Form1 class, then that boolean is for the brand new instance of the Login form that you created on line 41. It won't be referring to the any of the login form that you added as child controls on lines 22-37. The new instance you create on line 41 is different from the new instance you created on line 36. And regardless of the different instances, Enabled would refer to whether the form is enabled or not, whether the user has successfully logged in or not (unless you have additional code that you aren't showing us where you are twiddling the Enabled value to correspond with the login state).
 
Top Bottom