2015-05-30 45 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。

相关问题