Click here to Skip to main content
15,900,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two forms in visual studio 2022, one with login and the other with mainwindow. The question then is, how do i call a method in mainwindow from a method in login class/form?

The programming language is c# and is in windows application (.Net).


private void Login_Load(object sender, EventArgs e)
       {


           if (Settings.Default.Email != string.Empty)
           {
               txt_Email.Text = Settings.Default.Email;

               Password.Text = Settings.Default.Password;
           }

           if (Settings.Default.boxchecked)
           {
               txtremember.Checked = true;

               txt_Next_Click(sender, e);
           }

           else
           {
               txtremember.Checked = false;
           }

       }


and

private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!(Settings.Default.boxchecked))
            {
                Settings.Default.Reset();
            }

            else

            {
                Settings.Default.Save();
            }


            Application.Exit();
        }




Hope the question makes sense.

What I have tried:

I have searched, but couldn't find an appropriate answer. Neither have worked.
Posted
Updated 18-Dec-22 18:33pm
v2

Check the Related questions on the right side. There are various questions with the same problem and a potential solution.

To call a method for a different window, you must pass the reference to that window. One of the ways to do that is by passing this from MainWindow to the Login page in the constructor. Then the Login page can store the reference and use the reference to call the method to mutate the UI/state of the MainWindow.

---
If you don't want to handle that, you can always change the current Page inside the MainWindow to show a login form and then, upon successful login, show the functional UI to the user.
 
Share this answer
 
Comments
Member 15862460 18-Dec-22 19:14pm    
Your first point, how would it look like in the constructor?
Please help
You login form shouldn't "know" what called it - it should communicate with whatever "parent" form via events and a Result code when it closes just as all other controls do. To start "calling methods" in another form breaks OOPs!

If that isn't followed, then you get maintenance problems because the two forms are "tied together" and you have to consider very carefully what the affects of any change on one might have on the other.

So have a look at this: Transferring information between two forms, Part 2: Child to Parent[^] and think about what you are trying to communicate between the two forms.

And ... you do realise that settings files are plain text, don't you? They are just stored as XML data, so your login data is accessible and readable by anyone with physical access to the computer? That does rather make your security redundant ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900