【问题标题】:Hide form then show the same form again隐藏表格然后再次显示相同的表格
【发布时间】:2015-05-30 12:57:44
【问题描述】:

当用户点击“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;
        }
    }

谢谢

【问题讨论】:

  • 您可以重复使用之前的对象'ticketForm',并且显示时应该是一样的。

标签: c# forms


【解决方案1】:

试试我下面的两个表单代码

表格 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";
        }

    }
}
​

【讨论】:

    【解决方案2】:

    我不确定您实际在哪里创建工单表单,但您检查其是否可见的代码不起作用。检查它是否不为空并不意味着它实际上是可见的。要检查它是否确实可见,您需要类似以下的代码:

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

    话虽如此 - 我会避免依赖 OpenForms 属性,而是在某处使用私有成员管理实例 - 可能在 menuForm 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      • 2014-05-16
      相关资源
      最近更新 更多