Taskbar Button rightclick: Customized menu in windows forms - c#

I do not want the form to exit when the "close window" option is clicked in the menu that pops up when the taskbar button is right clicked. Instead, I want the application to be minimized to the system tray.
How do I change the behavior of the "Close Window"?

Add an override of OnFormClosing and look at the CloseReason of the event arguments parameter. Maybe something like this:
protected override OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Hide();
}
else
{
this.Close();
}
}
This way, the user cannot close your form (only hide it), but Windows still can for other reasons (e.g. shutdown).

Related

Open folder when a user clicks on a button of dialog box and prevent the dialog from closing

I have custom dialog box with a button and a link_label which i use as a button.
When the user clicks the label i want to open a certain folder at window explorer and keep the dialog open. I know which button the user clicks by determining what Dialog result to return.
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.DialogResult = DialogResult.No;
}
private void simpleButton1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
At another class i have a method which returns a bool value according to the dialogResult:
DialogResult res = dig.ShowDialog();
return res == DialogResult.No;
Then if this methods return true i need to open the win explorer and keep the dialog open until the button is clicked.
the problem is when im tring to prevent the dialog to closing with FormClosing event of MyDialog form, the explorer is not opened. And if i am not using FormClosing the explorer opens but the dialog is closed.
private void MyDialog_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.DialogResult == DialogResult.No)
{
e.Cancel = true;
}
}
EDIT: I cant use the Linkclicked event of the form because i want this dialog to be reusable and handle the result each time. (open different paths depends on where i use the dialog)
The reason why explorer is not opened is that when you reject the form close, the parent code doesnt continue to open explorer,
What you want to achieve is a bit unclear to me, but the way you do it is not possible, as a suggestion why not open the FolderBrowserDialog from inisde MyDialog based on label click and closing MyDialog on button click, rather than having the parent handle the decision.

c# create system tray application that remains in tray after application closed

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ShowInTaskbar = true;
this.WindowState = FormWindowState.Normal;
}
//Its not working to show my application icon in tray on application Exit.
When you exit the application, the tray icon goes away.
Also, when you close the last window, the application exits.
You want to minimize and hide the main window instead of exiting. You can do this with Hide(); somewhere in your Windows Forms code. Then, when the user wants to show the form, just run form.Show().

How to prevent OpenTK.GameWindow from closing

I have a game I've written in OpenTK but am looking for a way to add a handler to the close button of the game window without actually closing the game (e.g. invoking a "do you want to save before you quit?" sort of dialog). I can't seem to find any event handler or documentation that accomplishes this.
You can override the OnClosing method and show your message box there. If the user does not want to close, you can use e.Cancel = true, which will stop the form from closing:
protected override void OnClosing(CancelEventArgs e)
{
// ... show message box
if (/* wants to save*/)
{
// Cancel closing, the player does not want to exist
e.Cancel = true;
}
base.OnClosing(e);
}

C# close to tray (like msn messenger)

I have a c# .net app. So I created a notifyIcon that sits in the tray. What I want to do is when the user hits the "x" button on the form, I want it to close to the tray. They should only be able to exit program by using the context menu in the tray icon.
So what I did was, on the form close event, I check whether the form is visible. If its visible, i set it to invisible and set showInTaskbar to false (simulating minimize to tray) If the form is invisible already, then they are probably closing it from the tray, so I will exit the program in that case.
However, the problem I have is that if the window is visible, but they right click on the context menu of the tray icon and hit exit, I need to exit the program and not minimize.
How do I solve this problem?
try this:
bool _closingFromMenu;
void NOTIFYICON_EXIT_MENU_HANDLER(object sender, EventArgs e)
{
_closingFromMenu = true;
Close();
}
//form closing handler
FormClosing +=(a,b) =>{
if(_closingFromMenu){
Close();
}
else{
e.Cancel = true;
//do minimize stuff;
}
}
or if you have only one form you can call Application.Exit(); in context menu item handler
You probably want to track the state of the application based on the actions of the user as that's not necessarily reflected in the state of the window. So when the user selects Exit from the menu you need to set a flag to indicate that you're really exiting, not just hiding the window.
Just make your Context Menu close event call Application.Exit()

How to Disable Alt + F4 closing form?

What is the best way to disable Alt + F4 in a c# win form to prevent the user from closing the form?
I am using a form as a popup dialog to display a progress bar and I do not want the user to be able to close it.
This does the job:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
Edit: In response to pix0rs concern - yes you are correct that you will not be able to programatically close the app. However, you can simply remove the event handler for the form_closing event before closing the form:
this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Close();
If you look at the value of FormClosingEventArgs e.CloseReason, it will tell you why the form is being closed. You can then decide what to do, the possible values are:
Member name - Description
None - The cause of the closure was not defined or could not be determined.
WindowsShutDown - The operating system is closing all applications before shutting down.
MdiFormClosing - The parent form of this multiple document interface (MDI) form is closing.
UserClosing - The user is closing the form through the user interface (UI), for example by clicking the Close button on the form window, selecting Close from the window's control menu, or pressing ALT+F4.
TaskManagerClosing - The Microsoft Windows Task Manager is closing the application.
FormOwnerClosing - The owner form is closing.
ApplicationExitCall - The Exit method of the Application class was invoked.
I believe this is the right way to do it:
protected override void OnFormClosing(FormClosingEventArgs e)
{
switch (e.CloseReason)
{
case CloseReason.UserClosing:
e.Cancel = true;
break;
}
base.OnFormClosing(e);
}
Note that it is considered bad form for an application to completely prevent itself from closing. You should check the event arguments for the Closing event to determine how and why your application was asked to close. If it is because of a Windows shutdown, you should not prevent the close from happening.
You could handle the FormClosing event and set FormClosingEventArgs.Cancel to true.
I am using a form as a popup dialog to display a progress bar and I do not want the user to be able to close it.
If the user is determined to close your app (and knowledgeable) enough to press alt+f4, they'll most likely also be knowledgeable enough to run task manager and kill your application instead.
At least with alt+f4 your app can do a graceful shutdown, rather than just making people kill it. From experience, people killing your app means corrupt config files, broken databases, half-finished tasks that you can't resume, and many other painful things.
At least prompt them with 'are you sure' rather than flat out preventing it.
This is a hack to disable Alt + F4.
private void test_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.ModifierKeys == Keys.Alt || this.ModifierKeys == Keys.F4)
{
e.Cancel = true;
}
}
Subscribe FormClosing event
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = e.CloseReason == CloseReason.UserClosing;
}
Only one line in the method body.
This does the job:
bool myButtonWasClicked = false;
private void Exit_Click(object sender, EventArgs e)
{
myButtonWasClicked = true;
Application.Exit();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (myButtonWasClicked)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
Would FormClosing be called even when you're programatically closing the window? If so, you'd probably want to add some code to allow the form to be closed when you're finished with it (instead of always canceling the operation)
Hide close button on form by using the following in constructor of the form:
this.ControlBox = false;

Categories