C#/MonoDevelop: GTK MessageDialogs require a doubleclick to close - why?

14,981

The response value given when you click on a dialog's "close window" button is not CLOSE, but DELETE_EVENT. That's why the destroy method is never called and the dialog lingers. The second time you close it (out of the context of the run method), the dialog is destroyed normally.

In short, you also need to check for ResponseType.DeleteEvent.

Update:

In code:

MessageDialog msdSame = ...
...
ResponseType response = (ResponseType) msdSame.Run();
if (response == ResponseType.Close || response == ResponseType.DeleteEvent) {
  msdSame.Destroy();
}

Or, as ptomato mentions, you don't need to check the response, considering the user only has one choice: "close".

MessageDialog msdSame = ...
...
msdSame.Run();
msdSame.Destroy();
Share:
14,981
Connel
Author by

Connel

Updated on June 05, 2022

Comments

  • Connel
    Connel almost 2 years

    I'm a newbie programmer writting a program in MonoDevelop in C# and have a porblem with my gtk MessageDialogs.

    The close button on the window boarders of my GTK Message dialogues require a double click to actually close them. The close button on the dialogue its self works fine. Could someone please tell me how I can fix this below is the code:

      if (fchDestination.CurrentFolder == fchTarget.CurrentFolder) {
       MessageDialog msdSame = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, "Destination directory cannot be the same as the target directory");
       msdSame.Title="Error";
       if ((ResponseType) msdSame.Run() == ResponseType.Close) {
        msdSame.Destroy();
       }
       return;
      }
    
      if (fchTarget.CurrentFolder.StartsWith(fchDestination.CurrentFolder)) {
       MessageDialog msdContains = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, "error");
       msdContains.Title="Error";
       if ((ResponseType) msdContains.Run() == ResponseType.Close) {
        msdContains.Destroy();
       }
       return;
      }
    
  • Connel
    Connel about 14 years
    hmmm but when I click on the close button on the msgbox its self the window closes straight away, its only the close button on the msgboxes window decoration that is a problem :S that why im so confused
  • ptomato
    ptomato about 14 years
    Agree with the explanation, but you don't need to check for any ResponseType since the user's only choice is "Close". Just call Run() and then Destroy().
  • Connel
    Connel about 14 years
    I tried the following code but still get the error :s where abouts am i going wrong? thanks for your help :) MessageDialog msdSame = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, "You cannot sync two folders that are the same"); msdSame.Title="Error"; //if user clicks close then close message box if ((ResponseType) msdSame.Run() == ResponseType.Close || (ResponseType) msdSame.Run() == ResponseType.DeleteEvent) { msdSame.Destroy(); }
  • Connel
    Connel about 14 years
    hi ptomato sorry didnt see your comment! I'm kinda new to programming :s whereabouts do I need to call the run() and destroy in my code? thanks
  • Johannes Sasongko
    Johannes Sasongko about 14 years
    I've updated the answer to explain it in code. You were almost correct, but you need to only call Run once and store the return value. The code that you had was actually causing the dialog to be run twice.
  • Connel
    Connel about 14 years
    Hi thanks for all your help I greatly appreciate it :D I got it to work in the end using ptomato's method, Johannes when I tired your code I got the following error on this line ResponseType response = msdContains.Run(); Error CS0266: Cannot implicitly convert type int' to Gtk.ResponseType'. An explicit conversion exists (are you missing a cast?) I was just wonndering what I can do to correct this as latter on in my program I may want a message box with multiple options so would like to understand how this works :) thanks
  • Johannes Sasongko
    Johannes Sasongko about 14 years
    Ah, my mistake. Apparently Run returns an int, which in this case we need to cast to ResponseType: (ResponseType) dlgSame.Run(). I've updated the answer again.
  • Connel
    Connel about 14 years
    Yup that sorted it :D Thanks Johannes really appreciate your help :)