简体   繁体   中英

Hide form then show the same form again

I need to hide an ticketForm when the user clicks 'X'. When 'X' is clicked the Form is hidden. After being hidden, the user have the menuForm . This form contains a button which when pressed, it should re-open ticketForm with the SAME text inside the textboxes (and not a completely new form).

How can I 'Show' the Form which I was working on, instead of popping a form with fresh textboxes?

This is the code of the button:

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();
    }
}

This is the code of the 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;
        }
    }

Thanks

Try my two form code below

Form 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();
        }
    }
}
​

From 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";
        }

    }
}
​

I'm not sure where you actually creating your ticket form but your code to check if its visible is not going to work. Checking if its not null does not mean its actually visible. To check if its indeed visible you need code similar to this:

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

Having said this - I would avoid relying on the OpenForms property and rather manage the instance with a private member somewhere - maybe in the menuForm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM