I am changing the Visibility of a Form to false during the load event AND the form still shows itself. What is the right event to tie this.Visible = false; to? I\'d like to
Just create an instance of Form1
and do not call methods to show/display it. But I bet you're doing something wrong.
Having .Visible = false
or Hide()
in the Load
event will cause your form to show briefly, as there is time between when it becomes physically visible and when the Load
event gets fired, in spite of the fact that the documentation says the contrary.
Are you calling Show()
or ShowDialog()
somewhere? I'm not sure if this behavior is still present, but at least in past versions of the framework a call to ShowDialog()
did not trigger the Load
event, so perhaps that is your issue (though I think calling ShowDialog()
then hiding a modal form would be a bad practice!)
If you have to have the handle created (and the handles for controls) for whatever it is you're trying to do, a better idea would be to set the StartLocation
to Manual
, then set the Position
property to an offscreen location. This will create and show the form, while making it invisible to the user.
InitializeComponent() is setting this.Visible = true, since you specified that the form should be visible in the designer (or it defaulted to that). You should set Visible to false in the designer, and it won't be called by InitializeComponent(). You can then make it visible any time you like.
I agree that this can be really maddening, because Winforms usually don't look pretty while they're initializing a bunch of controls or populating a big DataGridView or whatever. Still you do need the window handle to exist before you can do that, creating all the issues that have been mentioned.
Here's something that has worked for me, and you have two choices: You can just hide your main form until it's ready, or you can show some kind of little splash screen to let your user know that you're working on it. Enjoy.
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
Size mDeferredSize;
protected override void OnHandleCreated(EventArgs e)
{
// Capture the "real" size...
mDeferredSize = Size;
// and set it to nothing...
Size = new Size(0, 0);
DoSomeUglyInitialization(showOptionalSplash: true);
Size = mDeferredSize; // ...and now put it back to the original size
base.OnHandleCreated(e);
}
private void DoSomeUglyInitialization(bool showOptionalSplash)
{
MySplash splash = null;
if (showOptionalSplash)
{
// We have made some borderless form class with a logo or something...
splash = new MySplash();
splash.Show();
}
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// Initialization that REQUIRES A HANDLE, e,g,
// we might be creating a bunch of controls, or
// populating a big DataGridView. Whatever it is,
// we don't want everyone looking at our biz.
System.Threading.Thread.Sleep(2500); // (Here simulated...)
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
splash?.Dispose();
}
}
Set the visibilty on the constructor, after init and then this.Show() later
What I would suggest would be to instantiate the form in an event the precedes the _Show event, such as the constructor, after the IntializeComponent() call.