How can I close Gtk# Window - c#

I have 2 fullscreen window in my Gtk# application, primary and secondary. I have buttonClose in second form's that click event handler must close this window and make some operation:
protected void OnButtonClose_Clicked (object sender, EventArgs e)
{
//some operation
this.Dispose ();
}
But window doesn't close! If I use this code in Primary window, that window will close and application will terminate. Why this code works for primary window and doesn't works for secondary? How can I close secondary window?

Try Window.Destroy() should destroy the window for the secondary window. You could also hide it with .Hide() but not sure that's your intention

After a ton of research on this I have found what seems to work correctly for me.
public WhateverWindow():base (Gtk.WindowType.TopLevel) {
DeleteEvent += delegate { Application.Quit(); };
}
try using the DeleteEvent += delegate { Application.Quit(); };
in your function see if that doesn't help.

use this:
protected void OnBtnCancelarClicked (object sender, EventArgs e)
{
this.Destroy ();
}

It seems that the only method that works as expected is calling:
RootWindow.Destroy ();
However, ain't sure if that's the right way to do it.

Related

How Do I Call Another Window With Button Press?

I want to be able to press a button and have the program open up a new window and close the old one.
I have followed solutions from this link but i have never has success with any of them How do I open a second window from the first window in WPF?
Here is my work soo far:
Window editor = new Window();
editor.Show();
this.Close();
But this does nothing.
The program should open up a new window and close the old one.
The functionality you described will work just fine. The Problem there is would more likely be the function or Methode in which you call this function.
To write a Methode that would handle a Button press as you want is pretty good described here: https://www.c-sharpcorner.com/forums/c-sharp-button-click-hold-and-release.
Hopefully, this will help you otherwise just ask
here is a small Implementation if that helps:
public partial class MainWindow : Window
{
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
Window editor = new MainWindow();
editor.Show();
this.Close();
}
private void MainWindow_KeyUP(object sender, KeyEventArgs e)
{
}
public MainWindow()
{
this.KeyDown += MainWindow_KeyDown;
this.KeyUp += MainWindow_KeyUP;
}
}
You have to call the second window from the first. This is something I did for a project where it popped up a new login panel window:
private void displayLoginPanel_Click(object sender, EventArgs e)
{
LoginPanel myLogin = new LoginPanel(this);
myLogin.Show();
this.Hide();
}
I used hide() instead of close() because you can see that I am sending a reference of the parent to the child LoginPanel in order to come back later. You can replace the Hide() with Close().

Best way to intercept closing of window?

I'm looking for the best way to intercept the closing of a window in my software. For example I want to intercept the closure of a form called "settings", and not the MainWindow. vb.net of the procedure was very simple, I had only to call its closing event, but with WPF I can not understand why this event is not captured, and as a result, the code inside is not executed.
In the form you want to intercept the closing for:
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
if(dontClose)
{
e.Cancel = true;
}
base.OnClosing(e);
}
Replace dontClose with your conditions for not closing.
In the cs file behind your XAML, add this:
// Constructor
public SettingsWindow()
{
InitializeComponent();
Closing += SettingsWindow_Closing; // Subscribe to window closing event.
}
// Window closing event handler.
private void SettingsWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Add method you want to run on close here.
}
You can acces it from the view model as well if you have one. Like this
Application.Current.MainWindow.Closing += (s, e) =>{ your code comes here};

Run code when window closes

I believe it is possible to run code when the close button is pressed in Windows Forms application in C#. Its a child form of the main window. I want to save some user settings when the user closes the window.
private void fileTypeDialog_FormClosing(Object sender, FormClosingEventArgs e)
{
int ArraySize = fileTypesData.Items.Count;
string[] fileTypesToSaveArray = new string[ArraySize];
for (int i = 0; i < ArraySize; i++)
{
fileTypesToSaveArray[i] = fileTypesData.Items[i].ToString();
}
string fileTypesToSave = String.Join(",", fileTypesToSaveArray);
MessageBox.Show(fileTypesToSave.ToString());
Properties.Settings.Default.fileTypes = fileTypesToSave;
Properties.Settings.Default.Save();
}
I have done this before i think, but i simply cannot remember how i did it. Can you guys assist me?
Your event isn't wired up. If you don't create the event with the designer, then you need to add it manually, usually in the constructor:
public Form1() {
InitializeComponent();
this.FormClosing += fileTypeDialog_FormClosing;
}
But a form shouldn't have to listen to its own events since it has access to its protected event methods. So simply start typing "override OnForm" and select "OnFormClosing" from intellisense. Your code block would look like this:
protected override void OnFormClosing(FormClosingEventArgs e) {
// your code here
base.OnFormClosing(e);
}
When overriding a base method, always include the base call as shown unless you have a specific reason not to.
Try to use FormClosed instead of FormClosing, I tried and worked very well for me :) Hope helped you :)

Detect when a form has been closed c#

I have a WinForm that I create that shows a prompt with a button. This is a custom WinForm view, as a message box dialog was not sufficient.
I have a background worker started and running. I also want to exit the while(aBackgroundWorker.IsBusy) loop if the button on myForm was clicked.
//MyProgram.cs
using(CustomForm myForm = new CustomForm())
{
myForm.Show(theFormOwner);
myForm.Refresh();
while(aBackgroundWorker.IsBusy)
{
Thread.Sleep(1);
Application.DoEvents();
}
}
Right now, in the CustomForm the Button_clicked event, I have
//CustomForm.cs
private void theButton_Click(object sender, EventArgs e)
{
this.Close();
}
Do I need to add more code to the CustomForm class, or the location where I declare and initialize the form in order to be able to detect a closure?
To detect when the form is actually closed, you need to hook the FormClosed event:
this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
// Do something
}
Alternatively:
using(CustomForm myForm = new CustomForm())
{
myForm.FormClosed += new FormClosedEventHandler(MyForm_FormClosed);
...
}
void MyForm_FormClosed(object sender, FormClosedEventArgs e)
{
// Do something
}
You might be going overkill. To show a form like a dialog window and wait for it to exit before returning control back to the calling form, just use:
mySubForm.ShowDialog();
This will "block" the main form until the child is closed.
Make sure your background worker supports cancellation and as others have pointed out use the form closed event handler. This code should point you in the right direction:
using(CustomForm myForm = new CustomForm())
{
myForm.FormClosed += new FormClosedEventHandler(ChildFormClosed);
myForm.Show(theFormOwner);
myForm.Refresh();
while(aBackgroundWorker.IsBusy)
{
Thread.Sleep(1);
Application.DoEvents();
}
}
void ChildFormClosed(object sender, FormClosedEventArgs e)
{
aBackgroundWorker.CancelAsync();
}
Handle the FormClosing event of the form to be notified when the form is closing, so you can perform any cleanup.
You should be able to hook into the FormClosing and FormClosed events.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosed.aspx
Closing is before it's closed.
Closed is after it's closed.
A couple things...
First, it appears that loop is there in order to prevent execution form proceeding while the dialog is open. If that is the case, change you .Show(parent) to .ShowDialog(parent). That will also take care of the rest of your question.
Note that this.Hide(); is not the same as this.Close(); in the actual dialog your overriding the closed event

Can't close C# application

I'm developing a C# application and when the user clicks on the X, the application gets minimized inside a trayicon. Like so:
private void frmChat_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
}
The application is really simple (only one form). The problem is that I can't manage to properly close the application. When the user rights clicks on the tray icon and he chooses "exit" he should be able to close the application. The problem is that even if the tray icon gets unloaded and the form is closed, the application still shows in the Task Manager as an active application. I'm closing the application like this:
private void chiudiToolStripMenuItem_Click(object sender, EventArgs e)
{
trayIcon.Dispose();
this.Close();
Application.Exit();
}
What am I missing here?
I did something similar a while back.
You need to know what is causing the form to close. So when you click on the X, there is a specific reason passed to the FormClosing event. Like so:
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
// don't close just yet if we click on x
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Hide();
}
}
Also, I have other code from the context menu Exit click:
private void tsmiExit_Click(object sender, EventArgs e)
{
// close the application forefully
TerminateApplication();
}
/// <summary>
/// Closes the Application.
/// </summary>
private void TerminateApplication()
{
// need to forcefully dispose of notification icon
this.notifyIcon1.Dispose();
// and exit the application
Application.Exit();
}
Edit:
Note: When you click on the X button, the close reason will be a CloseReason.UserClosing. When Application.Exit is called, the FormClosing is called again with a CloseReason.ApplicationExitCall.
End Edit:
Hope this helps
Andez
The e.Cancel = true line in frmChat_FormClosing is blocking the app from shutting down.
You can solve this easily enough by adding a boolean field to your form class, named TerminatingApp. Set this to true before calling this.Close(). Inside frmChat_FormClosing check for the value of TerminatingApp and only set e.Cancel = true if TerminatingApp is false.
Something like this:
private void frmChat_FormClosing(object sender, FormClosingEventArgs e)
{
if (!TerminatingApp)
{
e.Cancel = true;
Hide();
}
}
private void chiudiToolStripMenuItem_Click(object sender, EventArgs e)
{
TerminatingApp = true;
trayIcon.Dispose();
this.Close();
Application.Exit();
}
Ciao, how does it work if you replace Application.Exit with Application.ExitThread ?
I am actually afraid that with your code, when you call this.Close you are getting into the previous method above with the cancelling...
This is where the documentation on Application.Exit() comes in handy (my emphasis in bold):
The Exit method stops all running
message loops on all threads and
closes all windows of the application.
This method does not necessarily force the application to exit. The
Exit method is typically called from
within a message loop, and forces Run
to return. To exit a message loop for
the current thread only, call
ExitThread.
You could call Environment.Exit(0) where the parameter is the exit code.
When you are calling "this.Close" its going to call "frmChat_FormClosing" and in that you are setting "e.Cancel = true" which is creating the problem, use some variable to identify from where close event is being called and set e.Cancel accordingly.
I have tried Environment.Exit(0). It works in this case, and it worked fine for me.
Everyone else is over doing it.
Just do Close();

Categories

Resources