简体   繁体   English

如何从C#中打开的窗口访问另一个窗口

[英]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. 我目前有一个登录窗口,该窗口打开带有btnClick事件的MainWindow来触发。 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 :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. 如果假设您使用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) (如果仍然打开窗口,则方法.Close()不会退出程序。Application.Exit()将退出)
Hope this helps ! 希望这可以帮助 !

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从C#中的另一个运行窗口窗体访问控件 - How can I access the controls from another running window form in c# 如何在C#中从IDataReader Func调用的另一个属性中访问类属性 - How can i access a class properties from within another one called from a IDataReader Func in C# 如何在C#中从一个类访问另一个类的字符串数组 - How can I access a string array from one class to another in c# 如何在C#中从另一台PC访问列表框 - How can i access a listbox from another pc in C# 如何在c#中引用另一个项目? - How can I refer to a project from another one in c#? 如何使用StreamReader在C#中从一个WPF窗口向另一个窗口输入文本? - How do I use a StreamReader to input text from one WPF window to another in C#? 如何使用 C# Selenium 连接到在 Chrome 会话期间打开的新窗口? - How can I connect to a new window opened during a Chrome Session using C# Selenium? 如何在WPF中将对象从一个窗口绑定到另一个窗口中的另一个控件? (C#) - How to bind an object from one window to another control in another window in wpf? (c#) 如何从另一类的一个类访问变量? [C#] - How to Access Variable From One Class in Another Class? [C#] 如何在C#中从另一个表单访问一个对象? - How to access one object from another form in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM