This question already has answers here:
How to hide close button in WPF window?
(23 answers)
Closed 9 years ago.
Is it possible to disable the close button in a WPF form?
How can I disable the close button?
I have been searching around and found the solution below. But that works only in Windows Form!
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
in wpf this event called Closing :
public Window4()
{
InitializeComponent();
this.Closing += new System.ComponentModel.CancelEventHandler(Window4_Closing);
}
void Window4_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
}
You need to implement a windows hook to accomplish that. See this MSDN post for details.
Related
This question already has answers here:
How to detect when an application loses focus?
(2 answers)
Closed 6 years ago.
We have form_load event in c# forms. I want to do some job when my form1 will go in background and form2 will come to foreground. I have wasted my time in searching that, but could not found any help in this regard.
Form.Activated Event
DOC
Occurs when the form is activated in code or by the user.
Form.Deactivate Event
DOC
Occurs when the form loses focus and is no longer the active form.
UPDATE
to start and stop the timer:
class Form1: Form{
void Form_Load(object sender, EventArgs arg)
{
this.Activated += form_Activate;
this.Deactivate += form_Deactivate;
}
void form_Activate(object sender, EventArgs arg){
timer.Start();
}
void form_Deactivate(object sender, EventArgs arg){
timer.Stop();
}
}
This question already has answers here:
How can I prevent a user from closing my C# application?
(4 answers)
Closed 7 years ago.
I want to close a application when a button is pressed but I wanted to disable the close button (X button upper right).
I disabled the close button with this code:
protected override void OnFormClosing(FormClosingEventArgs e)
{
e.Cancel = true;
}
But now when I try to close the program with this code it wont work.
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
Is there a way to close this program when the button is clicked?
In the event handler, check the CloseReason property of the FormClosingEventArgs:
This allows you to behave differently depending on how the close was initiated, so in the case of Application Exit (or Windows Shutdown) you can allow the form to close.
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.ApplicationExitCall
&& e.CloseReason != CloseReason.WindowsShutDown)
{
e.Cancel = true;
}
}
You're cancelling ALWAYS the closign of the form, so that's why it doesn't works.
Try this:
bool blockClosing = true;
protected override void OnFormClosing(FormClosingEventArgs e)
{
e.Cancel = blockClosing;
}
private void button1_Click(object sender, EventArgs e)
{
blockClosing = false;
Application.Exit();
}
In this way, when you press your button it will allow tha pp to be closed.
FormClosingEventArgs has a Reason member that tells you what exactly is trying to close your form. Simply allow ApplicationExitCall through without canceling it.
This question already has answers here:
C# Cancel Windows Shutdown
(3 answers)
How to detect Windows shutdown or logoff
(3 answers)
Closed 8 years ago.
I have a FormClosing method for my program to ask the user if he want to exit the program before closing the form. This works great, but I don't want the dialog to show up when the program closes because of a system shutdown / logout. How can I detect that the kill command is sent by the system and not by the user clicking the x of my form? Envrionment.HasShutdownStarted does not work.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Need to check for system shutdown here before the next if is activated
if (MessageBox.Show(...) == DialogResult.No)
{
e.Cancel = true;
this.Activate();
}
}
Try checking e.CloseReason, e.g.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.WindowsShutDown)
{
if (MessageBox.Show(...) == DialogResult.No)
{
e.Cancel = true;
this.Activate();
}
}
}
This question already has answers here:
How do I minimize a WinForms application to the notification area?
(4 answers)
Closed 9 years ago.
my app is for chatting, and i think if someone needs to hide it quick, but doesn't want to close it, i came up with this:
private void button6_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
however, instead of going to the taskbar, i want it to appear (no popup) in the tray, just the apps icon, and when someone clicks it it needs to set this
this.WindowState = FormWindowState.Normal;
Is this possible, how?
Also by system tray i mean the one in the bottom right corner, next to the time
I still can't get this to work, nothing appears in the notification bar if I do what you guys said (btw: this is the full code to minimise)
private void button6_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void Form_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
private void notifyIcon_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
Why isn't this working?
Handle the form’s Resize event. In this handler, you override the
basic functionality of the Resize event to make the form minimize to
the system tray and not to the taskbar. This can be done by doing the
following in your form’s Resize event handler:
Check whether the form’s WindowState property is set to FormWindowState.Minimized. If yes, hide your form, enable the NotifyIcon object, and show the balloon tip that shows some information.
Once the WindowState becomes FormWindowState.Normal, disable the NotifyIcon object by setting its Visible property to false.
Now, you want the window to reappear when you double click on the NotifyIcon object in the taskbar. For this, handle the NotifyIcon’s MouseDoubleClick event. Here, you show the form using the Show() method.
In the form resize event, do the check there and hide the form
private void Form_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
Then when clicking on the taskbar icon just restore it.
private void notifyIcon_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
Refer:
How do I minimize a WinForms application to the notification area?
minimize app to system tray
Use following code:
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
When you minimize the form, simply hide it.
You will have to implement above code in Form_Resize event.
Then on clicking taskbar icon just restore its state as follows:
private void notifyIcon_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
You will need to use notifyIcon_Click event for this purpose.
Hope its helpful.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Single Form Hide on Startup
I want to hide my WinForm after it run (Not minimizing).
I used:
this.Load += new System.EventHandler(this.Form1_Load);
private void Form1_Load(object sender, EventArgs e)
{
Hide();
}
But it's not working. Can you help me do it?
In the form Load override you can use one of the following tricks:
Make the form completely transparent:
private void OnFormLoad(object sender, EventArgs e)
{
Form form = (Form)sender;
form.ShowInTaskbar = false;
form.Opacity = 0;
}
Move the form way off the screen:
private void OnFormLoad(object sender, EventArgs e)
{
Form form = (Form)sender;
form.ShowInTaskbar = false;
form.Location = new Point(-10000, -10000);
}
Try to hide the form after it has been shown and not after it has been loaded, use the Shown event instead of the Load event
You can't easily hide the form but what you can do is set the Opacity to 0, for example:
this.Opacity = 0;
If you Don't want the user to be able to see the app at all set this:
this.ShowInTaskbar = false;
Then they won't be able to see the form in the task bar and it will be invisible.
I believe this solved your "no use minimized" requirement??