user4973915
user4973915

Reputation:

How can I access another window from an opened one in c#

I currently have a login Window that opens the MainWindow with the btnClick event to trigger. By clicking a button from this Window, this Window should close and open the Main one.

I tried it but I still have no idea how to access Main Window from current one.

Here is the code below. Hope to get some help. Thanks! :P


using ....;

..........;

using ....;

namespace SampleWindowApp

{

    public partial class Login : Form

    {
        public Login()

        {

            InitializeComponent();
        }

        private void Login_Load(object sender, EventArgs e)

        {

        }

        private void loginbtn_Click(object sender, EventArgs e)
        {
            //ConnectionDAL obj = new ConnectionDAL();
            BL.LoginBL objBL = new BL.LoginBL();
            if(objBL.ValidateBL(txtUsername.Text, txtPass.Text))
                {
                    Mainfrmcs.Show;  <---
                    this.Close;      <---
                }
            else
                MessageBox.Show("Incorrect username or password.");
        }  
    }
}

The two lines show me the error:

Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement.

Upvotes: 0

Views: 79

Answers (2)

Tripesdeporc
Tripesdeporc

Reputation: 94

If suppose you work in WinForms.
Before showing or closing anything you want, you have to define it.
Actually, you try to show a form that exists but has no object affected to it and it's the same for the closing. All you have to do is :

.
.
.
if(objBL.ValidateBL(txtUsername.Text, txtPass.Text))
    {
        Form Mainfrmcs = new Mainfrmcs();
        // I suppose there is no MdiParent if you're closing the other but if there was :
        Mainfrmcs.MdiParent = this;
        Mainfrmcs.Show();
        this.Close();
    }
else
.
.
.

(the method .Close() does not exit the program if a window is still open. Application.Exit() will)
Hope this helps !

Upvotes: 1

Jonathan Camilleri
Jonathan Camilleri

Reputation: 631

Looking at your code, it looks like you are opening your form after logging in, instead of writing Mainfrmcs.Show you need to write new Mainfrmcs().Show() and since this.Close() closes the program, you need to change it this.Hide().

Upvotes: 0

Related Questions