0
votes

So it's been a very long time since I've worked with C# and am feeling like a newbie all over again. I'm creating a simple GUI in a project that uses an API, and I need to create a simple login form. I have secondary form pop up when someone clicks "Login" on the main form that prompts for the username and password, which would then close that form and stores the information in that class. Problem is, how would I know when that form is closed?

Is there an easier way to do this, or a way to return the username and password when the login button is clicked on the secondary form?

The basics of my code are as follows:

Main.cs

namespace My_Namespace {
    public partial class frmMain : Form {
        public frmMain() {
            InitializeComponent();
        }
    }

    private void mnitLogin_Click(object sender, EventArgs e) {
        Login login = new Login();
        login.ShowDialog();
    }
}

Login.cs

namespace My_Namespace {
    public partial class Login : Form {
        private String Username;
        private String Password;

        public Login() {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e) {
            Username = txtUsername.Text;
            Password = txtPassword.Text;
            this.Close();
        }

        public String getUsername() {
            return Username;
        }

        public String getPassword() {
            return Password;
        }
    }
}
4
I wouldn't recommend storing the password in the app like that. Once you're authenticated to the API, do you get some kind of token or cookie? - mason
@mason Yeah, I'm aware of all that, but that isn't my issue. - Spedwards

4 Answers

0
votes

Assuming Login form is a custom form and not a default dialog, it would be easiest to raise access to '.Click' event of "Login" button and add a handler before opening it.

Once the handler activates you can retrieve the details, validate them and close the login form (or bring up a message that details were not valid) from frmMain.

0
votes

Execution continues in the Click event after ShowDialog completes - which is when the window closes. The object is still valid so you can use login.getUsername() & login.getPassword()

0
votes

Figured it out through a good 30 minutes of searching.

By adding a DialogResult property to the button on the Login form, I was able to check when the form closed. I had to slightly change the way the form opened as well:

private void mnitLogin_Click(object sender, EventArgs e) {
    Login login = new Login();
    if (login.ShowDialog() == DialogResult.OK) {
        // Do stuff
        loginToAPI(login.getUsername(), login.getPassword());
    }
}
0
votes

In many windows forms projects I've ever worked we had a static or singleton class to store some data that are common for the whole application, like:

public class SessionInfo
{ 
   public static SessionInfo instance;

   public static SessionInfo GetInstance();
   {          
       if(instance == null)
          instance = new SessionInfo();

       return instance;           
   }

   public string UserName {get; set;}
   public string[] UserRoles {get; set;}
}