1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Snippet C#: Hiding a form within the form constructor.

Discussion in 'Programming & Webmastering' started by FordGT90Concept, Jul 30, 2009.

  1. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    15,287 (6.16/day)
    Thanks Received:
    4,878
    Location:
    IA, USA
    I ran into a little nucience of .NET today: hiding a form from within a form. It should be really simple like this:

    Code:
    public Main()
    {
            InitializeComponent();
            this.Hide();
    }
    You'd think it would work, but it doesn't. Handles (which .Hide() uses) don't work until the form leaves the constructor.

    So, how to work around this? Make a second thread to order the form to hide itself, of course! Here's the important code:

    Code:
    public delegate void AutomaticHideHandler(); // This is what we trigger and place on the event stack.
    
    public class Main : Form
    {
        public event AutomaticHideHandler AutomaticHide; // This holds all triggered events.
        private Thread _AutoHide = null; // This makes sure the thread is alive (in the memory) as long as we need it.
    
        public Main()
        {
            AutomaticHide += new AutomaticHideHandler(Main_AutomaticHide); // Add an instance of the of the event to the event stack.
    
            InitializeComponent();  // This initializes the form.
    
            _AutoHide = new Thread(Main_AutomaticHide);  // This creates a new thread and saves it into our class variable.
            _AutoHide.Start();  // This starts the thread we just created.
        }
    
        private void Main_AutomaticHide()
        {
            // Because another thread is going to call this method, we have to catch the cross-threaded reference before it happens.
            if (this.InvokeRequired)
            {
                this.Invoke(new AutomaticHideHandler(AutomaticHide)); // Make the form's thread retrigger the event
                _AutoHide = null; // Free up the memory from _AutoHide which is no longer needed.
            }
            else
                this.Hide();  // Finally hide the form.
        }
    }
    Basically, all the above code does is create a "helper" thread which tells our form thread that it needs to hide. The form thread won't allow this so we have to make the form thread tell itself it needs to hide.

    This isn't as good as not creating the form in the first place but it is a quick fix. You will see the form blash for less than a second before it hides.


    If you want more information about how this works, do some searching on multi-threaded programming in C#. This trick can also be done in VB.NET.


    Note: This method does not appear to work in Server 2003.
     
    Last edited: Jul 30, 2009
    Crunching for Team TPU
  2. Braveheart

    Braveheart New Member

    Joined:
    Mar 26, 2008
    Messages:
    1,115 (0.42/day)
    Thanks Received:
    46
    have you tried the this.hide() in C# 4.0?
     
  3. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    15,287 (6.16/day)
    Thanks Received:
    4,878
    Location:
    IA, USA
    Nope, did they fix it?

    I'm running Visual Studio .NET 2008 with .NET Framework 2.0 on that app.
     
    Crunching for Team TPU
  4. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.48/day)
    Thanks Received:
    5,624
    Location:
    Cheeseland (Wisconsin, USA)
    Just out of curiosity, why do you want to hide the main form in its constructor?
     
  5. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    15,287 (6.16/day)
    Thanks Received:
    4,878
    Location:
    IA, USA
    Primarily because the application was made over a year ago (closer to two years) and is rather extensive. I've been making small improvements to it throughout that period and it is now at version 0.25.0.# (25 major feature changes since original build).

    The app creates the form, the form creates the notify icon, and once that notify icon is created, I no longer need to be able to see the form. Instead of having to click somewhere to hide it every time the app starts, I wanted to make it a setting the application could apply.

    In order to move the notify icon code out of the Main form I would have had to recode half the GUI. So I had to choose between recoding half of the GUI, use a second thread to hide it, or do nothing at all. Since Server 2003 is where I use this application, the second option is useless. The first option is more work than I care to commit to right now so, I ultimately took the third route and removed all code.

    If I were to primarily run it in Windows XP, I would have left it. It does work as a quick fix to a deeper problem but apparently it doesn't work on all platforms. Maybe someone will find it useful (it almost was to me).


    Its getting to the point where I need to recode it from scratch. I want to make the options a PropertyGrid, add more features to the timeline (like the ability to make one event relative to another), add more optional settings, make the tray icon separate from all other GUIs, change the GUI, clean up the code, and maybe get creative with the tray icon (have it show lunar and day/night cycles). Time is of the essence and all those features have actual little practical use for me.
     
    Last edited: Jul 30, 2009
    Crunching for Team TPU

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page