tags:

views:

111

answers:

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;
  }
A: 

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

Veer
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
+2  A: 

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
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
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
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
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
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 lineResponseType 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
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
Yup that sorted it :DThanks Johannes really appreciate your help :)
Connel
A: 

//THE EASY WAY IS THAT YOU BUILT YOUR OWN MessageBox.Show METHOD //I MADE MY OWN CLASS IN BASE WITH THE GTK CLASS //THIS WORKS PRETTY WELL, I SHARE WITH ANYBODY USE IT

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;                  
    }//end  Show            
}

}

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");  


    }
LUIS ADRIAN