Home > Blockchain >  C# - How to reference my current instance of Form1 from another class in my winforms application?
C# - How to reference my current instance of Form1 from another class in my winforms application?

Time:11-23

Apologies if this doesn't make sense! Fairly new to WinForms and this is for a uni assessment. My main form is as below with the method I want to call in another class. I've renamed from Form1 to LoginPage.

public partial class LoginPage : Form
    {
        public LoginPage()
        {
            InitializeComponent();
            Customer.LoadCustomerDB();
        }

        public string PinText
        {
            get { return PINNumTxt.Text; }
            set { PINNumTxt.Text = value; }
        }
    }

My other class looks to verify the PinText which I've made accessible with the function above.

    class BankCard
    {
        // Attributes
        private Customer customer;
        private int pinAttempts = 0;
        private bool cardUnusable = false;

        // Member functions
        public void VerifyPIN()
        {
            LoginPage loginPage = new LoginPage();
            foreach (Customer c in Customer.customers)
            {
                if (c.GetID().ToString() == loginPage.AccountNum())
                {
                    customer = c;
                }
            }

            if (cardUnusable == true)
            {
                MessageBox.Show("This card is currently blocked. Please contact your bank.");
                Environment.Exit(0);
            }
            else if (loginPage.PinText == customer.GetPIN())
            {
                MessageBox.Show("Success!");
            }
            else if (pinAttempts < 2)
            {
                MessageBox.Show("Incorrect PIN attempt "   (pinAttempts   1)   "/3");
                loginPage.PinText = "";
                pinAttempts  = 1;
            }
            else
            {
                MessageBox.Show("3 failed PIN attempts. Please contact your bank.");
                cardUnusable = true;
                Environment.Exit(0);
            }
        }
    }

My issue is that where I have the following:

LoginPage loginPage = new LoginPage();

This creates a new instance of the main page, doubles up the CustomerDB being loaded in and causes errors in my VerifyPin() function.

Is the issue that I need to somehow have LoginPage loginPage = current instance of LoginPage? And if so, how would I code that?

Thanks for any help

CodePudding user response:

Wyck's comment got it to run without any errors. I had to do:

LoginPage loginPage = Application.OpenForms.OfType<LoginPage>().FirstOrDefault();

Thanks all

CodePudding user response:

In most winforms projects, there is a Program class that has Application.Run(new Form1()); (or whatever your main form is called, LoginPage in your case). In the Program class you can create a static variable public static LoginPage loginPage; then in the Main function:

static void Main()
{
    loginPage = new LoginPage();
    Application.Run(loginPage);
}

then when you want the reference to loginPage in any of your classes you can just use Program.loginPage

  • Related