2016-08-04 69 views
-3

當您單擊註銷時,A會顯示另一種表單。當您點擊「是」時,我想要關閉或隱藏這兩個表單,並且它會轉到我的登錄表單。任何幫助,將不勝感激。在另一個窗體上關閉窗體?

enter image description here

+4

好的,但你有什麼嘗試?我建議閱讀:[問] –

+3

請顯示一些代碼,特別是顯示第二種形式的部分。 –

+0

winforms? WPF?別的東西? – stuartd

回答

2

您可以使用表格的Owner屬性設置子窗體的所有者:在您的輔助形式

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Form2 form2 = new Form2(); 
     form2.Owner = this; 
     form2.ShowDialog(); 
    } 
} 

然後關閉它:

public partial class Form2 : Form 
{ 
    public Form2() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     if (this.Owner != null) 
      this.Owner.Close(); 
    } 
} 
+0

謝謝,你真的幫了我。 – leimelson06

+0

您也可以使用Form的'Owner'屬性來完成這項工作,這將是一個更好的解決方案。看看最新的答案。 – Alex

0

這是一個WinForm的解決方案:

private void bLogout_Click(object sender, EventArgs e) 
{ 
    DialogResult result= MessageBox.Show("Are you sure want to logout?", "Confirm", MessageBoxButtons.YesNo); 
    if (result == DialogResult.Yes) 
    { 
    this.Hide(); 
    new frmLogin().Show(); 
    } 
} 
相關問題