3

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;
  }
Tomalak
  • 332,285
  • 67
  • 532
  • 628
Connel
  • 1,844
  • 4
  • 23
  • 36

3 Answers3

5

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();
Johannes Sasongko
  • 4,178
  • 23
  • 34
  • 3
    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()`. – ptomato Apr 07 '10 at 08:01
  • 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 Apr 07 '10 at 23:40
  • 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 – Connel Apr 07 '10 at 23:43
  • 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. – Johannes Sasongko Apr 08 '10 at 09:46
  • 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 – Connel Apr 08 '10 at 10:22
  • 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. – Johannes Sasongko Apr 08 '10 at 11:25
  • Yup that sorted it :D Thanks Johannes really appreciate your help :) – Connel Apr 08 '10 at 12:17
0

May be both the conditions get satisfied and hence you get two msgbox and it appears you've to give a double click to close it

Amsakanna
  • 12,254
  • 8
  • 46
  • 58
  • 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 – Connel Apr 06 '10 at 15:55
0

A sample class could be:

using System;
using Gtk; 
namespace Visitors.Clases.MessageBox
{   
    public static class MessageBox 
    {
        public static Gtk.ResponseType Show(Gtk.Window window, Gtk.DialogFlags dialogflags, MessageType msgType,ButtonsType btnType,string Message,String caption)
        {

            MessageDialog md = new MessageDialog (window,dialogflags,msgType,btnType, Message);
            md.Title = caption;
            ResponseType tp = (Gtk.ResponseType)md.Run();       
            md.Destroy(); 
            return tp;
        }
    }
}

The class in use:

ResponseType result = MessageBox.Show(this,DialogFlags.Modal,MessageType.Error,ButtonsType.Ok,Error,"ERROR");
if (result == Gtk.ResponseType.Yes)
{
    MessageBox.Show (this, DialogFlags.Modal, MessageType.Other,ButtonsType.Ok, "YES", "EJEMPLO");
}
else
{
    MessageBox.Show (this, DialogFlags.Modal, MessageType.Other,ButtonsType.Ok, "NO", "EJEMPLO");
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291