2

I have a login form in the MainWindow of my WPF application. If the user logs in successfully, I want to open the HomeWindow. My problem is that I need to pass the adminID variable from the MainWindow to the HomeWindow. How can I do this?

public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }

            private void btnLogin_Click(object sender, RoutedEventArgs e)
            {
                int errors = 0;
                if (txtUsername.Text == "")
                {
                    lblUsernameStatus.Content = "This field is required.";
                    errors = errors + 1;
                }

                if (txtPassword.Text == "")
                {
                    lblPasswordStatus.Content = "This field is required.";
                    errors = errors + 1;
                }

                if (errors == 0)
                {
                    Administrator TryLogin = new Administrator();
                    if (TryLogin.VerifyUser(txtUsername.Text, txtPassword.Text))
                    {
                        HomeWindow home = new HomeWindow();
                        int adminID = TryLogin.userID;
                        home.Show();
                        this.Close();
                    }
                    else
                    {
                        lblLoginStatus.Content = TryLogin.status;
                    }

                }
            }

PS: I haven't written anything in the HomeWindow.xaml.cs file.

1
  • 1
    Have you considered writing viewmodels instead of putting all your code in event handlers? Does your application have an App class? Commented May 23, 2016 at 23:49

3 Answers 3

5

Define an initializer in HomeWindow to accept the data you wish to send:

private int AdminID;

public HomeWindow()
{
    InitializeComponent();
}

public HomeWindow(int adminID) : base()
{
    AdminID = adminID;
}

Then you can just:

HomeWindow home = new HomeWindow(TryLogin.userID);
home.Show();
this.Close();
Sign up to request clarification or add additional context in comments.

Comments

1

There are different ways to achieve this.

Here is an example using singleton

public class User
{
    private static readonly User _instance;
    private static object syncRoot = new Object();

    public string Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string SomeOtherProperty { get; set; }

    private User()
    { 
        // Initialize defaults
    }

    public void Reset()
    {
        // Clear existing values
    }

    public static User Instance
    {
        get 
        { 
            if (instance == null) 
            {
                lock (syncRoot) 
                {
                    if (_instance == null) 
                        _instance = new User();
                    return _instance; 
                }
            }
        }
    }   
}

On your login form:

private void btnLogin_Click(object sender, RoutedEventArgs e)
{
    int errors = 0;
    if (txtUsername.Text == "")
    {
        lblUsernameStatus.Content = "This field is required.";
        errors = errors + 1;
    }

    if (txtPassword.Text == "")
    {
        lblPasswordStatus.Content = "This field is required.";
        errors = errors + 1;
    }

    if (errors == 0)
    {
        Administrator TryLogin = new Administrator();
        if (TryLogin.VerifyUser(txtUsername.Text, txtPassword.Text))
        {
            User.Instance.Reset(); // Make sure old data is removed
            User.Instance.Username = txtUsername.Text;
            User.Instance.Password = txtPassword.Text;

            HomeWindow home = new HomeWindow();
            int adminID = TryLogin.userID;
            home.Show();
            this.Close();
        }
        else
        {
            lblLoginStatus.Content = TryLogin.status;
        }
    }
}

On your home form:

You can retrieve the User credentials using User.Instance just make sure you reset it on logoff.

Comments

1

Declaring static variable would be the simplest and easiest way because once login is authorized, the value doesn't change until logged off(application is exited)

I also used a way of passing value through Window constructor but static variables are much easier to utilize many fixed data of the logged-in users like customized setting data for each users. I also have a WPF app and pass 11 values and utilize easiily everywhere inside application.

Declare as static variable in MainWindow like,

 public static int adminID;

 private void btnLogin_Click(object sender, RoutedEventArgs e)
{
    adminID= TryLogin.userID;
}

And usage in HomeWindow is like,

MainWindow.adminID

Hope this helps..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.