隐藏表单然后再次显示相同的表单

时间:2015-05-30 12:57:44

标签: c# forms

当用户点击“X”时,我需要隐藏ticketForm。单击“X”时,表单将被隐藏。隐藏后,用户拥有menuForm。这个表单包含一个按钮,当按下该按钮时,它应该在文本框中使用相同的文本重新打开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;
        }
    }

由于

2 个答案:

答案 0 :(得分: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";
        }

    }
}
​

答案 1 :(得分:0)

我不确定您实际创建故障单表单的位置,但不确定您的代码是否可见无效。检查它是否为空并不意味着它实际可见。要检查它是否确实可见,您需要与此类似的代码:

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

说完这个 - 我会避免依赖OpenForms属性,而是在某个地方用私有成员管理实例 - 也许在menuForm中。