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 instantiate the Form1 without showing it.
using System;
using System.Windows.Forms;
namespace TestClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Visible = false;
}
}
}
Regardless of how much you try to set the Visible property before the form has been shown, it will pop up. As I understand it, this is because it is the MainForm of the current ApplicationContext. One way to have the form automatically load, but not show at application startup is to alter the Main method. By default, it looks something like this (.NET 2.0 VS2005):
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
If you instead do something like this, the application will start, load your form and run, but the form will not show:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 f = new Form1();
Application.Run();
}
I am not entirely sure how this is useful, but I hope you know that ;o)
Update: it seems that you do not need to set the Visible property to false, or supply an ApplicationContext instance (that will be automatically created for you "under the hood"). Shortened the code accordingly.
I know this is an old question, but I just stumbled upon it and am pretty surprised no one has mentioned SetVisibleCore:
bool isVisibleCore = false;
protected override void SetVisibleCore(bool value)
{
base.SetVisibleCore(isVisibleCore);
}
In that code snippet, as long as isVisibleCore remains false, the form will remain invisible. If it's set to false when the form is instantiated, you won't get that brief flash of visibility that you'd get if you set Visible = false in the Shown event.
It took me some time to find a properly working Solution.
Set the properties named WindowState to Minimized and ShowInTaskbar to False under properties window. Once your form is completly loaded, Call below lines of code.
this.ShowInTaskbar = true;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
//this.WindowState = System.Windows.Forms.FormWindowState.Normal;
PS: This solution is Tested on Visual C# 2008 Express Edition
How about setting the Opacity property to 0 on design and back to 100 when you want to show the form?
a solution i can live with
so the form is created and on_load is called on createtion.
set WindowState to minimize then on load set visible to false and windowstate to normal
private void Form1_Load(object sender, EventArgs e)
{
this.Visible = false;
this.WindowState = FormWindowState.Normal;
}
what did not worked:
the SetVisibleCore override solution did not created the form
as also the
Application {
Form1 f = new Form1();
Application.Run();
):
For a flicker-free Shown solution, also set the form's location off-screen during load:
private Point startuplocation;
private void Form1_Load(object sender, EventArgs e)
{
this.startuplocation = this.Location;
this.Location = new Point(-1000, -1000);
}
private void Form1_Shown(object sender, EventArgs e) //fires first time shown
{
this.Visible = false;
this.Location = this.startuplocation;
}
Just create an instance of Form1 and do not call methods to show/display it. But I bet you're doing something wrong.
Try on the VisibleChanged event.
The shown event may give you want you want. Although the form will "flash" for a second before hiding.
private void Form1_Shown(object sender, EventArgs e)
{
this.Visible = false;
}
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.
If this is your main form, there may not be a better place then the Shown event. But in that case you will get flicker.
I couldn't find a good place to stop a running main form from showing at least quickly. Even a timer activated in the load event won't do it.
If it is a secondary form just create it but don't show it.
Have you tried
this.Hide();
in the form_load or form_activated events
Set the visibilty on the constructor, after init and then this.Show() later
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.
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.
Yeah the really one elegant way in perspective to your code than your applications visual is to flicker the form by hiding in the constructor/load event.
I set these three property settings for the form:
ShowInTaskbar = false
ShowIcon = false
WindowState = Minimized
Related
Is there any way of stopping the default opening animation of a form from Windows? I mean stopping it just for one form, not all of them. I need the showing of a form to be instantly, without that growing effect.
I tried to document about WinApi, but I couldn't find anything. Thank you!
You have to take a couple of steps to achieve this:
In the constructor, set your BorderStyle to None. This will prevent the animation:
public Form1()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
}
Of course, now you want to show the border again, so you have to set it back as soon as your form is shown, so the user never sees this. Therefore create a method:
private void Form_Loaded(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.Sizable;
}
Now you have to set the Shown event of your form to your Form_Loaded method. You can do this from the form designer event properties window.
If you want to do it for the entire application you can change it using the SystemParametersInfo Method.
I had to set the location property after showing the form. It works!
this.Hide();
this.Size = this.Size;
this.Location = this.Location;
this.Show();
In somewhere of this page I read that the best way to "override" the minimize method is to use onResize() event.
I've done it and it works! I've coded that when the form is minimized it turns no visible and put an icon in the windows tray. As yet everything is working fine, but I've also programmed (or tried it at least) that when the icon in windows tray is clicked, the form turns visible again, but it doesn't work and I don't know why.
I've tried to code both events (Click and MouseClick), but the code is the same in both and it still doesn't work, so I must be doing something wrong, but obviusly I don't know what.
Remember that what I want is to code the click event on windows tray icon, not on task bar icon, so maybe that's why it isn't working, maybe is another event.
Here is my code:
private void onResize(object sender, EventArgs e)
{
this.ShowInTaskbar = false;
notifyIcon1.Visible = true;
this.Visible = false;
}
private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
{
this.ShowInTaskbar = true;
this.Visible = true;
}
private void notifyIcon_Click(object sender, EventArgs e)
{
this.ShowInTaskbar = true;
this.Visible = true;
}
Thanks.
EDIT: the problem was that I just assigned the image to the icon on the Form constructor like notifyIcon1.Icon = new Icon("greenCircle.ico"); but I didn't initialize any image on the notifyIcon properties... By initializing the image on notifyIcon properties it worked fine!
You probably don't want to hide it when they resize it, just when they minimize the Form:
private void onResize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized) // only hide if minimizing the form
{
this.ShowInTaskbar = false;
notifyIcon1.Visible = true;
this.Visible = false;
}
}
To show the minimized Form again, you'll need to restore it:
private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
{
this.ShowInTaskbar = true;
this.Visible = true;
WindowState = FormWindowState.Normal;
}
this.ShowInTaskbar = true;
this.Visible = true;
This doesn't do what you hope it does. A tricky problem in Windows that has bedeviled many programmers. The ShowInTaskbar property is "special", very unlikely most other Form properties. It is specified in the underlying CreateWindowEx() winapi function as a style bit. There are other properties like that, like FormBorderStyle, ControlBox, LeftToRight. Each corresponding with a style bit.
Trouble is, changing the property requires Winforms to perform considerable gymnastics, it has the destroy the native window and re-create it again. That tends to have side-effects, you found one.
Just swap the two statements and you'll see it working very differently, now it does manage to make the window visible again. But still not smoothly, it doesn't always manage to get into the foreground.
You might be puzzled at what looks like a massive bug. The real problem is that you are doing something that most other programmers never do, always a good way to get a problem that nobody else has. You don't actually have to change the ShowInTaskbar property at all. The taskbar button is already automatically invisible when you hide your window, no need to help at all.
Delete all ShowInTaskbar assignments in your program to fix your problem.
I have developed a C# Windows Form.
At first, the Windows Form works fine.
However, one day the Windows Form starts up always minimized and I have no idea.
I checked the WindowState is Normal not Minimized.
How Can I fix it, Thanks!
Edit:
I comment each code block to narrow the scope to locate the problem point.
And I found that I used a Drive Detector in my MainForm.
When that instance was created, the call Window form must be passed as a parameter to the constructor.
Otherwise, the Drive Detector will create a hidden form. However, the MainForm will be minimized.
The below code will NOT create a hidden form.
driveDetector = new DriveDetector(this);
The below code will create a hidden form, it will interfere the call Windows Form.
driveDetector = new DriveDetector();
try to add this code in form load event and test
this.WindowState = FormWindowState.Normal;
you should use WindowState = FormWindowState.Maximized if you wish to open your windows in full screen by default. You can do this programmatically in Form load event.
There are other various options available too from which you can control on How to open your windows form.
1.Check whether you have set the size of form to smaller one.
2.Try re-build your solution.
3.Add Form Load Event from your Events Properties of Form and add following code to it
this.WindowState = FormWindowState.Normal;
Try to do it in form activated event
bool bIsLoaded = false;
private void Form1_Activated(object sender, EventArgs e)
{
if (!bIsLoaded)
{
this.WindowState = FormWindowState.Maximized;
bIsLoaded = true;
}
}
Just try to add it from the code level to say the windows state as follows.
private void Form1_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
I comment each code block to narrow the scope to locate the problem point.
And I found that I used a Drive Detector in my MainForm.
When that instance was created, the call Window form must be passed as a parameter to the constructor.
Otherwise, the Drive Detector will create a hidden form. However, the MainForm will be minimized.
The below code will NOT create a hidden form.
driveDetector = new DriveDetector(this);
The below code will create a hidden form, it will interfere the call Windows Form.
driveDetector = new DriveDetector();
Try this:
Topmost = true;
In your Form_Load event
I have run my program step by step, but still, I can't find where the form.visible is set.
Here is the code of "Form1.cs":
public Form1()
{
InitializeComponent();
this.Hide();
}
I added this.Hide(); but it didn't work at all.
and I even modified "Program.cs" from:
Application.Run(new Form1());
to:
Form1 form1=new Form1();
form1.Visible = false;
Application.Run(form1);
But the main form still showed, even adding form1.Enable=false; won't stop the form from showing. I have also tried adding this.Hide(); in Form1_Load(), it worked but the main form appeared and flashed before it was finally hidden.
I'm totally confused now. So how can I load the main form without showing it? And keep it hidden until I call this.Show();?
Why not override the SetVisibleCore method:
bool showForm = false;
protected override void SetVisibleCore(bool value)
{
base.SetVisibleCore(showForm);
}
This will hide the form before it even opens up. With a lot of method's you see a brief flash of the form before its .Visible or similar property is set to "hide".
Obviously you will need another method to flip 'showForm' to true when you want to display it again i.e. NotifyIcon event.
you can not set form visible to false before load it try this code:
//In Main Function
Form1 form1 = new Form1();
form1.WindowState = FormWindowState.Minimized;
form1.ShowInTaskbar = false;
Application.Run(new Form1());
//In Form Shown
private void Form1_Shown(object sender, EventArgs e)
{
this.Visible = false;
}
The documentation for Application.Run(Form) clearly spells out that it will make the form visible. As long as you need to keep it hidden, you should therefore not pass that form to Application.Run. Depending on your needs, you may be able to use Application.Run() (no arguments) or Application.Run(context) (create a custom ApplicationContext) instead.
Edit: I just want to add that to not show the form, you should not show the form. I don't understand how so many users here think it's a good idea to show the form and then hide it really quickly, or make the code that shows the form not show the form. You shouldn't be doing the wrong thing and then fixing it after the fact, you should do the right thing in the first place.
You might be looking for the Opacity property. You can set Opacity to 0.0 from the IDE. It can take values between 0.0 and 1.0.
You could try something like this:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new Form1();
form.DoBeforeShow();
Application.Run(form);
}
First you can load the form you permanently want to show to the user. In the constructor only call the form which should be hidden. Afterwards you can close or hide it.
Is there a way to set a form to be active all the time? No matter if I open another forms, the form which I opened sets inactive and that other sets active.
For example:
I have two forms: "Main Form" and "Reminder Form". When I start the application it loads the "Reminder Form", then the "Main Form" loads and after that "Main Form" is active, but I want, that "Reminder Form" is active, but the sequence of how forms loads needs to be the same.
If by saying "active" you mean have a focus always, it's not possible in built-in way, it should be handled by you. If you want that it's always visible (it's on top of other forms), you can use TopMost=true attribute, so it will be always over all non TopMost forms, which, by the way, is not guranteed in case of appearance of another TopMost form.
Hope this helps.
You could use
public ReminderForm()
{
InitializeComponent();
MainForm frm2 = new MainForm();
MainForm.Show();
Activate();
}
If you want your form to stay always on top of other forms, set TopMost = true;
You could register to the Deactivate event and then call Activate to make the form active again:
public ReminderForm()
{
//other stuff
Deactivate += (s, e) => { if (/* check if really do it */) Activate(); };
}
If you just want the 'Reminder Form' to be visible, setting TopMost to true is the easier way.
set the TopMost property of the form to true.
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
this.Focus();
}
set TopMost property to True