CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: hide main form

  1. #1
    Join Date
    Jan 2007
    Posts
    11

    hide main form

    hi

    I’m new in c sharp so I think I’ll use this forum many times in future

    My question is about when the main form is loading I want to check if a file exist, if the file not exist I want that second form ( another form) will load and the first form (main form) will hide. How should I solve this?
    file checking is ok used file.exist .... but I can’t hide the main form.

    allready used mainform.hide();
    mainform.visible=false;
    activeform.hide();

    it's all doesn't work

    Thanks by advance

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: hide main form

    Quote Originally Posted by MSDN
    To make the main form of a Windows application invisible when the application starts, you have to move the application's startup logic into a separate class. You cannot simply set its Visible property to false.

    After you have separated the lifetime of the application from the lifetime of the form, you can make forms visible (and invisible), because the application will end when you "close" the class that was used for application startup.

    Note Because a module is invisible when its code is running, the procedure that follows includes a step for adding a message box to the startup module simply to demonstrate that the application is running.

    To set a form to be invisible at its inception

    Do one of the following:
    In C#, create a new class.

    Within the module or class, develop a Main subroutine that can act as the startup object for the project. For example, the following code shows how you might approach this.

    Code:
    // This class is added to the namespace containing the Form1 class.
    class MainApplication
    {
       public static void Main()
       {
          // Instantiate a new instance of Form1.
          Form1 f1 = new Form1();
          // Display a messagebox. This shows the application 
          // is running, yet there is nothing shown to the user. 
          // This is the point at which you customize your form.
          System.Windows.Forms.MessageBox.Show("The application "
             + "is running now, but no forms have been shown.");
          // Customize the form.
          f1.Text = "Running Form";
          // Show the instance of the form modally.
          f1.ShowDialog();
       }
    }
    Change the startup object for the project to be Sub Main instead of Form1. For C#, set the startup object to be ApplicationName,MainApplication (as per the naming of the class in the code above).

    When the application runs, the code within Main() executes first while the instance of Form1 lingers, hidden, until the code to show it is run. This allows you to do whatever you like within the instance of Form1 in the background without the user's knowledge.
    Or a bit easier, if you don't want to use a seperate Startup Object. Replace your Current Main (in the Main Form) with :
    Code:
    	static void Main() 
    		{
    		// Instantiate a new instance of Form1.
                                   Form1 f1 = new Form1();
                                   // Display a messagebox. This shows the application 
                                   // is running, yet there is nothing shown to the user. 
                                  // This is the point at which you customize your form.
                                 System.Windows.Forms.MessageBox.Show("The application "
                                 + "is running now, but no forms have been shown.");
                                 // Customize the form.
                                 f1.Text = "Running Form";
                                // Hide the Form.
                                f1.Visible = false;
    
    	             }
    I hope it helps
    Last edited by HanneSThEGreaT; January 10th, 2007 at 08:43 AM.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284
    .... And this will show Form2, and Hide Form1
    Code:
    		static void Main() 
    		{
    		// Instantiate a new instance of Form1.
    		Form1 f1 = new Form1();
    		Form2 f2 = new Form2();
          // Display a messagebox. This shows the application 
          // is running, yet there is nothing shown to the user. 
          // This is the point at which you customize your form.
          System.Windows.Forms.MessageBox.Show("The application "
             + "is running now, but no forms have been shown.");
          // Customize the form.
          f1.Text = "Running Form";
          // Show the instance of the form modally.
          f1.Visible = false;
    	  f2.ShowDialog();
    		}

  4. #4
    Join Date
    Jan 2007
    Posts
    11

    Re: hide main form

    Thank You for reply I have it now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured