简体   繁体   中英

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.

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 !

查看您的代码,看起来您是在登录后打开表单,而不是编写Mainfrmcs.Show您需要编写new Mainfrmcs().Show()并且由于this.Close()关闭程序,因此您需要进行更改它this.Hide()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM