C# - Prevent animation of a showing form - c#

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();

Related

C# Windows Form start up always minimized

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

How can I set the default visibility of the main Winform

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.

How to set active form for all time?

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

Closing dialog form closes parent form

I have been dealing with strange problem. I am using KryptonForm in a project. I have a form (say form1) and I need to open another form on a button click from this form. Here is the code:
void btn_click(object sender, EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
var f = new Form2();
f.ShowDialog();
Visible = true;
ShowInTaskbar = true;
}
The problem is that when the Form2 closes it closes the Form1 also. I have tried setting DialogResult = DialogResult.None from Form2 but of no avail. Please help me.
I am always using this technique and this thing has never happened.
Yes, this code is troublesome. It goes wrong when the user closes the dialog. Windows must then find another window to give the focus to. There isn't any left in your app, your main window is invisible. It then picks a window of another app. Odds are good, for example, that this will be a window inside Visual Studio. A big one. Your main form now disappears behind it.
You need to make sure that your main window is visible again before the dialog closes. You can do so by subscribing to the dialog's FormClosing event handler. For example:
private void button1_Click(object sender, EventArgs e) {
using (var dlg = new Form2()) {
dlg.StartPosition = FormStartPosition.Manual;
dlg.Location = this.Location;
dlg.FormClosing += (s, ea) => this.Show(); // <=== Here
this.Hide();
if (dlg.ShowDialog() == DialogResult.OK) {
// etc...
}
}
}
Bugged me for days!! Found this: https://bytes.com/topic/net/answers/769433-c-showdialog-inside-showdialog-closing-both-return
The result was being passed down and I dont know why. But if after the .ShowDialog() I just put this.DialogResult = DialogResult.None, it will fix it. This shouldnt happen in the first place, but this fixes it, so I am not too bothered.
You can also try changing the dialogResult on the button itself to "None" or deleting the this.Btn1.DialogResult... from the designer which worked for some people.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/543093ad-1860-4428-bae1-b0d4f112e04b/showdialog-closes-parent?forum=csharpgeneral
I know this is an old post, but I ran into this, and in my case the accepted answer (at the time I am writing this) is not helpful at all. The answer by #blind Skwirl led me to the culprit.
After 20 years of .Net programming (since it was introduced), I never noticed that BUTTONS have a "dialogresult" property. I always just set the forms "cancelbutton" and "acceptbutton" properties. What I found in my case was that (because I was doing a lot of copy-pasting of buttons), I had a bunch of buttons (not forms) that themselves had their "dialogresult" property set to "cancel", which meant that I would click a button on a dialog that would open another dialog, the "ok" button on the dialog had its result set to "cancel", and the button on the parent form ALSO had its result set to "cancel", so the dialog would close (with a result of cancel) and then the PARENT form would close with a result of cancel, confusing the heck out of me... so...
Just make sure all of your buttons have their dialogresult property set to NONE (or whatever the actual proper setting is that you want).
Bottom line, if a BUTTON (not the form) has its dialogresult property set to anything other than NONE, the form will close with that result when it is clicked (after any click event code has completed).
I hope that helps someone out there.

C#/.NET - WinForms - Instantiate a Form without showing it

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

Categories