Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Check if form is open?

Check if form is open?

Scheduled Pinned Locked Moved C#
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Arcdigital
    wrote on last edited by
    #1

    I have a button that when clicked opens another form. I want it to check and see if the form is already open, and if it is maximize it/bring it to the front. Is this possible?

    L N 2 Replies Last reply
    0
    • A Arcdigital

      I have a button that when clicked opens another form. I want it to check and see if the form is already open, and if it is maximize it/bring it to the front. Is this possible?

      L Offline
      L Offline
      lisan_al_ghaib
      wrote on last edited by
      #2

      Hi ! Are you hiding or closing the form? here is a method if your are closing the form: public ParentForm : Form { ChildForm frm ; // I assume that your form is a member of the parent form public ParentForm() { // Components } public DisplayAChildForm() { if (frm == null) { frm = new ChildForm(); frm.OnFormClosed += new EventHandler (frmClosed_evnt); fmr.Show(); // you can use ShowDialog method also } } private frmClosed_evnt(object sender, EventArgs e) { frm.OnFormClosed -= new EventHandler (frmClosed_event); frm = null; } } that's it :)

      1 Reply Last reply
      0
      • A Arcdigital

        I have a button that when clicked opens another form. I want it to check and see if the form is already open, and if it is maximize it/bring it to the front. Is this possible?

        N Offline
        N Offline
        Natza Mitzi
        wrote on last edited by
        #3

        Hi, In this way you either create or show the form without using events. On click the ShowChild method is called. Test for null or disposed and show it if this is the case. If the form is ok we test if it is minimized then we restore it and then bring it to the front. Note that Show brings the form to the front only on the first call. Note - DO NOT CALL ShowDialog - it does not work if the form has been shown before Your application will crash with an InvalidOperationException public partial class Form1 : Form { private Form childForm; public Form1() { InitializeComponent(); } private void ShowChild() { if (childForm == null || childForm.IsDisposed) { childForm = new Form2(); childForm.Show(); } else { if (childForm.WindowState == FormWindowState.Minimized) { childForm.WindowState = FormWindowState.Normal; } childForm.BringToFront(); } } private void buttonShow_Click(object sender, EventArgs e) { ShowChild(); } } Natza Mitzi

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups