2015-05-30 79 views
0

當用戶單擊「X」時,我需要隱藏ticketForm。當單擊「X」時,表單被隱藏。被隱藏後,用戶擁有menuForm。這個表單包含一個按鈕,當它被按下時,它應該用文本框內的SAME文本重新打開ticketForm(而不是一個全新的表單)。隱藏表單然後再次顯示相同的表單

如何「顯示」我正在處理的表單,而不是用新鮮文本框彈出表單?

這是按鈕的代碼:

private void btnTickets_Click(object sender, EventArgs e) 
    { 
     ticketForm tF = (ticketForm)Application.OpenForms["ticketForm"]; 


    if (tF != null) 
    { 
     MessageBox.Show("Ticket is already open!"); 
    } 
    else 
    { 
     tF.ShowDialog(); 
    } 
} 

這是ticketForm Closing EventHandler

private void ticketForm_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     if (MessageBox.Show("You may continue editing the ticket later by clicking 'ticket' at the menu", "", MessageBoxButtons.OK, MessageBoxIcon.Stop) == DialogResult.OK) 
      { 
       menuForm mF = (menuForm)Application.OpenForms["menuForm"]; 
       if (mF != null) 
       { 
        this.Hide(); 
        mF.btnTickets.Enabled = true; 
       } 
      } 
     else 
     { 
      e.Cancel = true; 
     } 
    } 

感謝的代碼

+0

您可以重新使用上一個對象'ticketForm',並且它在展示時應該相同。 –

回答

0

嘗試以下

表1我的兩個表單代碼

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     Form2 form2; 
     public Form1() 
     { 
      InitializeComponent(); 
      form2 = new Form2(this); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      form2.Show(); 
      string results = form2.GetData(); 
     } 
    } 
} 
​ 

從2

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form2 : Form 
    { 
     Form1 form1; 
     public Form2(Form1 nform1) 
     { 
      InitializeComponent(); 

      this.FormClosing += new FormClosingEventHandler(Form2_FormClosing); 
      form1 = nform1; 
      form1.Hide(); 
     } 
     private void Form2_FormClosing(object sender, FormClosingEventArgs e) 
     { 
      //stops form from closing 
      e.Cancel = true; 
      this.Hide(); 
     } 
     public string GetData() 
     { 
      return "The quick brown fox jumped over the lazy dog"; 
     } 

    } 
} 
​ 
0

我不知道,你實際上是創建您的機票形式,但你的代碼,以檢查其可見沒有去上班。檢查它是否不爲空並不意味着它實際上可見。要檢查其是否確實看到你需要類似下面的代碼:

if (tF != null && !tF.IsDisposed) 
{ 
    if (tF.Visible) 
     MessageBox.Show("Ticket is already open!") 
    else 
     tF.ShowDialog(); 
} 
else 
{ 
    //recreate your dialog 
} 

話雖如此 - 我會避免依靠OpenForms財產,而地方管理與私有成員的實例 - 也許在menuForm。

相關問題