[英]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.