簡體   English   中英

如何從C#中打開的窗口訪問另一個窗口

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

我目前有一個登錄窗口,該窗口打開帶有btnClick事件的MainWindow來觸發。 通過單擊此窗口中的按鈕,該窗口應關閉並打開主窗口。

我嘗試過,但是我仍然不知道如何從當前窗口訪問主窗口。

這是下面的代碼。 希望能得到一些幫助。 謝謝! :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.");
        }  
    }
}

兩行顯示了錯誤:

只能將賦值,調用,遞增,遞減,等待和新對象表達式用作語句。

如果假設您使用WinForms。
在顯示或關閉所需的任何內容之前,必須對其進行定義。
實際上,您嘗試顯示一個已存在但沒有對象受其影響的表單,並且結束時的表單是相同的。 您所要做的就是:

.
.
.
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
.
.
.

(如果仍然打開窗口,則方法.Close()不會退出程序。Application.Exit()將退出)
希望這可以幫助 !

查看您的代碼,看起來您是在登錄后打開表單,而不是編寫Mainfrmcs.Show您需要編寫new Mainfrmcs().Show()並且由於this.Close()關閉程序,因此您需要進行更改它this.Hide()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM