How to hide a form???

B

Bob Rock

Hello,

sorry for the simple question BUT how do I hide a form?????
I've seen that this.Visibility = false and this.Hide() do not work ... so
how do I hide a form???

Thx.

Bob Rock
 
H

Herfried K. Wagner [MVP]

* "Bob Rock said:
sorry for the simple question BUT how do I hide a form?????
I've seen that this.Visibility = false and this.Hide() do not work ... so
how do I hide a form???

'this.Visible = false' or 'this.Hide()' whould work.
 
B

Bob Rock

'this.Visible = false' or 'this.Hide()' whould work.

I must insist, it does not.
Create a new Windows Application, in the InitializeComponents() or in the
form constructor add either this.Hide() or this.Visible = false and startup
the application. The form will be show up anyway.

Bob Rock
 
N

Nevyn Twyll

You might also try setting the Visible property on the form itself to False?

What I've done, also, is to have a class be the main object in a project, so
by default, no form shows...
 
B

Bob Rock

Nevyn Twyll said:
You might also try setting the Visible property on the form itself to
False?

Where??? I can't find any such property in the form properties pane???
What I've done, also, is to have a class be the main object in a project, so
by default, no form shows...

Now, really, I can't believe this. Where has the ShowWindow() Win32 API
gone?????
I just want to hide a form, this cannot be that hard!!!

Bob Rock
 
H

Herfried K. Wagner [MVP]

* "Bob Rock said:
I must insist, it does not.
Create a new Windows Application, in the InitializeComponents() or in the
form constructor add either this.Hide() or this.Visible = false and startup
the application. The form will be show up anyway.

At this time, the form isn't yet shown, so hiding it doesn't make
sense. Throw an exception to cancel loading of the form, or don't call
its 'Show' or 'ShowDialog' method.
 
B

Bob Rock

At this time, the form isn't yet shown, so hiding it doesn't make
sense. Throw an exception to cancel loading of the form, or don't call
its 'Show' or 'ShowDialog' method.

Visible is a property just as any other.
I don't see why setting it to false inside InitializeComponent() as done
with any other form property "doesn't make sense".

I'm not calling any Show or ShowDialog. Take time to create a new Windows
Application an look for yourself what calls are being made!!

Bob Rock
 
H

Herfried K. Wagner [MVP]

* "Bob Rock said:
Visible is a property just as any other.
I don't see why setting it to false inside InitializeComponent() as done
with any other form property "doesn't make sense".

At the time of the execution of 'InitializeComponent', the form /is/
invisible. If you call its 'Show' or 'ShowDialog' method, its 'Visible'
property is set to 'True' automatically.
I'm not calling any Show or ShowDialog. Take time to create a new Windows
Application an look for yourself what calls are being made!!

Mhm... Is the form set as startup form in the project properties?
 
B

Bob Rock

Visible is a property just as any other.
At the time of the execution of 'InitializeComponent', the form /is/
invisible. If you call its 'Show' or 'ShowDialog' method, its 'Visible'
property is set to 'True' automatically.


Mhm... Is the form set as startup form in the project properties?

This is the code generated by the windows application project.
As you can there is no Show or ShowDialog, yet the form is displayed.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}

[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}



Bob Rock
 
H

Herfried K. Wagner [MVP]

* "Bob Rock said:
Visible is a property just as any other.
I don't see why setting it to false inside InitializeComponent() as done
with any other form property "doesn't make sense".

At the time of the execution of 'InitializeComponent', the form /is/
invisible. If you call its 'Show' or 'ShowDialog' method, its 'Visible'
property is set to 'True' automatically.
I'm not calling any Show or ShowDialog. Take time to create a new Windows
Application an look for yourself what calls are being made!!

Mhm... Is the form set as startup form in the project properties? [...]
This is the code generated by the windows application project.
As you can there is no Show or ShowDialog, yet the form is displayed. [...]
Application.Run(new Form1());

The line above will display it. That's by design. You can call
'Application.Run()' (without specifying a form) if you don't want to
show a form.
 
H

Herfried K. Wagner [MVP]

* "Bob Rock said:
No. The default windows application project does not set any startup object.

I agree. In C#, it sets a sub main that will show the main form by
calling 'Application.Run'.
 
S

Stu Smith

(inline)

Bob Rock said:
At the time of the execution of 'InitializeComponent', the form /is/
invisible. If you call its 'Show' or 'ShowDialog' method, its 'Visible'
property is set to 'True' automatically.


Mhm... Is the form set as startup form in the project properties?

This is the code generated by the windows application project.
As you can there is no Show or ShowDialog, yet the form is displayed.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}

[STAThread]
static void Main()
{
Application.Run(new Form1());

From the docs:

"Application.Run(Form)
Begins running a standard application message loop on the current thread,
and makes the specified form visible."

If you need a messageloop but no form, just do Application.Run().

Stu
 
B

Bob Rock

If you need a messageloop but no form, just do Application.Run().

I don't need a formless application, I only want the form to be displayed
when I say it!!

Bob Rock
 
J

james

Bob,

Ok, but can you tell us when that would be ? If I knew what event you were
waiting for
to show the form I could tell you where to put the code to Show the form.
Thats all
you really need to to run the app without specifying your form and then
handle the particualr event your waiting for and show the form in that event

JIM
 
B

Bob Rock

Bob,
Ok, but can you tell us when that would be ? If I knew what event you were
waiting for
to show the form I could tell you where to put the code to Show the form.
Thats all
you really need to to run the app without specifying your form and then
handle the particualr event your waiting for and show the form in that event

JIM

James, all this thread started out when I was trying to display a splash
form and then the main form. But I did not want the splash to display if the
main form for some reason failed to be created (maintaining the main form
all the time invisible). Since I had problems creating a hidden main form
and then create a splash form (with the main one as the owner) I posted the
first message of this thread.
BUT James, I have understood that the problems I had can be solved simply by
having another class (not a form) contain the main method for the
application.
The following code works fine:

class MainApplication
{
[STAThread]
public static void Main()
{
// Instantiate a new instance of the class MainForm.
MainForm f1 = new MainForm();

// Doanything you want

// Show the instance of the form modally.
f1.ShowDialog();

// Start the application message pump
Application.Run();
}
}

This code solves the problem I had ... and there is no need to set the
MainApplication class as the startup object for the project!!


Bob Rock
 
M

Mark Broadbent

Hi Bob. I've been looking through these posts and trying a few things
myself. It is possible to hide the form in several of its events (such as
it's paint or visiblechanged) but that only works after the form is
displayed (however briefly) plus you then need to handle these events being
raised by another scenario other that the first time it is shown.

The solution is actually quite simple. Dont hide the form......

.....no seriously.... all you need to do is (and this can be done at design
time) set the
form's WindowState property to FormWindowState.Minimized;

and the ShownInTaskbar property to false;

when you want the form to reappear the instance must have these properties
set to FormWindowState.Normal; (and if you want) ShownInTaskbar property to
true;

I do have reservations about exactly what you are trying to do and whether
what you are doing is really the best way to go about it, however Ive tried
the above and it works a treat.

--

--


Br,
Mark Broadbent
mcdba , mcse+i
=============
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top