# WX::Dialog won't close

**URL:** <https://www.ruby-forum.com/t/wx-dialog-wont-close/150048>\
**Category:** wxRuby\
**Created:** [October 2, 2008, 9:31pm UTC](https://www.ruby-forum.com/t/wx-dialog-wont-close/150048 "2008-10-02T21:31:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [October 2, 2008, 9:31pm UTC](https://www.ruby-forum.com/t/wx-dialog-wont-close/150048/1 "2008-10-02T21:31:22Z")

</div>

I have an interesting problem. I have a dialog which refuses to go away  
once it has finished it’s associated code.

Here is the code:

class DBPopulateDialog \< Wx::Dialog  
def initialize(parent, id, title, pos, size, style, name)  
super(parent, id, title, pos, size, style, name)

```
  sizer = Wx::BoxSizer.new(Wx::VERTICAL)

  @server = Wx::TextCtrl.new(self, -1, "Server", 

```

Wx::DEFAULT\_POSITION,  
Wx::DEFAULT\_SIZE)  
@username = Wx::TextCtrl.new(self, -1, “Username”,  
Wx::DEFAULT\_POSITION, Wx::DEFAULT\_SIZE)  
@password = Wx::TextCtrl.new(self, -1, “Password”,  
Wx::DEFAULT\_POSITION, Wx::DEFAULT\_SIZE, Wx::TE\_PASSWORD)  
@tree\_base = Wx::TextCtrl.new(self, -1, “LDAP Tree Base”,  
Wx::DEFAULT\_POSITION, Wx::DEFAULT\_SIZE)

```
  sizer.add(@server, 1, Wx::EXPAND|Wx::ALL, 5)
  sizer.add(@username, 1, Wx::EXPAND|Wx::ALL, 5)
  sizer.add(@password, 1, Wx::EXPAND|Wx::ALL, 5)
  sizer.add(@tree_base, 1, Wx::EXPAND|Wx::ALL, 5)

  button_sizer = create_button_sizer(Wx::OK|Wx::CANCEL)

  sizer.add(button_sizer, 0, Wx::EXPAND|Wx::ALL, 5)

  # Okay Button Event!!!!

  evt_button(self.get_affirmative_id()) { |event| on_okay() }

  set_sizer(sizer)
  show()
end # initialize

# Collect computer records from Directory and add to database

def on_okay()
  ldap = Net::LDAP.new :host => @server.get_value, :port => 389, 

```

:auth  
=\> { :method =\> :simple, :username =\> @username.get\_value, :password =\>  
@password.get\_value }

```
  filter = Net::LDAP::Filter.eq("objectcategory",

```

“CN=Computer,CN=Schema,CN=Configuration,#{@tree\_base.get\_value}”)

```
  # We don't want to return a result set as it could be pretty huge 

```

and  
we don’t need it.  
# We are treating the container name as the group name here.

```
  ldap.search(:base => @tree_base.get_value, :filter => filter,

```

:return\_result =\> false) do |record|  
computer\_name = record.cn.to\_s  
begin  
os = record.operatingsystem  
rescue  
os = ‘’  
end  
lab = record.dn.split(’,’)[1].split(’=’)[1]

```
    # Check db for lab and add it if it isn't already there

    unless Lab.find :name => lab
      lab = Lab.new(:name => lab)
      lab.save
      lab = lab.name
    end

    # Save machine record

    machine = Computer.new(:name => computer_name, :os => os, 

```

:location  
=\> lab)  
machine.save  
end  
log\_file = File.open(“log.txt”, “a”); log\_file.puts “\n\nFinished  
populate next instruction is close\n\n”; log\_file.close  
self.close()  
end # on\_okay  
end # DBPopulateDialog class

After filling out the proper information and clicking on okay the data  
is  
populated however when it finishes the dialog hangs around. Furthermore  
it  
won’t close when you click on Cancel or the Window close widget (the X  
in my  
case). The log line executes just fine, the dialog just refuses to  
close.

-Glen

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [October 2, 2008, 10:44pm UTC](https://www.ruby-forum.com/t/wx-dialog-wont-close/150048/2 "2008-10-02T22:44:08Z")

</div>

Glen H. wrote:

> I have an interesting problem. I have a dialog which refuses to go  
> away once it has finished it’s associated code.
> 
> Here is the code:

…

> After filling out the proper information and clicking on okay the data  
> is populated however when it finishes the dialog hangs around.  
> Furthermore it won’t close when you click on Cancel or the Window  
> close widget (the X in my case). The log line executes just fine, the  
> dialog just refuses to close.

I haven’t tried out your code, but maybe you need to call end\_modal, if  
you’re calling show\_modal to start the dialog?

alex

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [October 2, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/wx-dialog-wont-close/150048/3 "2008-10-02T23:18:14Z")

</div>

If it is modal I’m not intending it to be but I’ll check.

On Thu, Oct 2, 2008 at 2:42 PM, Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > populated however when it finishes the dialog hangs around. Furthermore it  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [October 3, 2008, 7:46pm UTC](https://www.ruby-forum.com/t/wx-dialog-wont-close/150048/4 "2008-10-03T19:46:08Z")

</div>

On Thu, Oct 2, 2008 at 2:45 PM, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > Here is the code:  
> > > you’re calling show\_modal to start the dialog?  
> > > –  
> > > “Hey brother Christian with your high and mighty errand, Your actions speak  
> > > so loud, I can’t hear a word you’re saying.”
> 
> -Greg Graffin (Bad Religion)

Well end\_modal doesn’t do anything either. Oddly before clicking on the  
“okay” button the “cancel” button works, however after the on\_okay event  
is  
processed the “cancel” button does nothing.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [October 8, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/wx-dialog-wont-close/150048/5 "2008-10-08T15:58:58Z")

</div>

On Fri, Oct 3, 2008 at 11:39 AM, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > > once it has finished it’s associated code.
> 
> “okay” button the “cancel” button works, however after the on\_okay event is  
> processed the “cancel” button does nothing.
> 
> –  
> “Hey brother Christian with your high and mighty errand, Your actions speak  
> so loud, I can’t hear a word you’re saying.”
> 
> -Greg Graffin (Bad Religion)

It would appear that the only instruction that doesn’t run at the end of  
on\_okay is close, exit works fine. This kind of blows, I suppose I’ll  
have  
to start another instance of the app and exit after the populate  
finishes,  
not the cleanest solution.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [October 10, 2008, 11:54am UTC](https://www.ruby-forum.com/t/wx-dialog-wont-close/150048/6 "2008-10-10T11:54:31Z")

</div>

Hi Glen

Glen H. wrote:

> It would appear that the only instruction that doesn’t run at the end  
> of on\_okay is close, exit works fine. This kind of blows, I suppose  
> I’ll have to start another instance of the app and exit after the  
> populate finishes, not the cleanest solution.

I’m sure you won’t need to do this.

Generally, if you want help, please reduce the code to a standalone,  
runnable, and ideally short sample. I’m very happy to help, but it’s too  
much to expect people to install extra libraries and second-guess what  
the rest of your app is doing.

I did try running your code, pruned of the LDAP stuff. It seems fine to  
me - there’s nothing in the dialog layout itself. I would suggest  
checking that the dialog has an explicit parent, not nil - it is better  
in most cases that the dialog is linked to a parent frame. In the  
special case that an application consists only of a single dialog, you  
must use Dialog#destroy to end the app, close is not sufficient.

If this doesn’t help, I would look further into net/ldap and see whether  
this is causing it to hang - ie, if ok does nothing but ‘close’ does it  
work fine?

cheers  
alex

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [October 10, 2008, 10:56pm UTC](https://www.ruby-forum.com/t/wx-dialog-wont-close/150048/7 "2008-10-10T22:56:04Z")

</div>

On Fri, Oct 10, 2008 at 3:48 AM, Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m sure you won’t need to do this.  
> application consists only of a single dialog, you must use Dialog#destroy to
> 
> * * *
> 
> wxruby-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

Hey Alex,

Thanks for the reply, destroy did the trick. I don’t know why I didn’t  
try  
that before, according to my browser I’d looked at the method definition  
in  
the API docs. Odd that close doesn’t work, I’m creating this dialog the  
same way I do all my others and close works for them.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [October 10, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/wx-dialog-wont-close/150048/8 "2008-10-10T23:20:40Z")

</div>

On Fri, Oct 10, 2008 at 2:54 PM, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > 
> > 
> > the dialog has an explicit parent, not nil - it is better in most cases that  
> > alex  
> > Thanks for the reply, destroy did the trick. I don’t know why I didn’t try  
> > that before, according to my browser I’d looked at the method definition in  
> > the API docs. Odd that close doesn’t work, I’m creating this dialog the  
> > same way I do all my others and close works for them.
> 
> –  
> “Hey brother Christian with your high and mighty errand, Your actions speak  
> so loud, I can’t hear a word you’re saying.”
> 
> -Greg Graffin (Bad Religion)

I have another dialog behaving the same way now. My application has one  
frame and from that frame I’m passing self as the parent argument for  
the  
Dialog Windows. I do this for other dialogs created by button events  
and it  
works okay. The dialogs that are unresponsive after pressing okay are  
created through menu events should I be doing something differently when  
creating a dialog from a menu option?

I will try to pair my code down and post a small working example but it  
will  
take a while. If anyone has any ideas off the top of their head I’m all  
ears. Dialog#destroy does work so I can always do that.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [October 11, 2008, 12:18am UTC](https://www.ruby-forum.com/t/wx-dialog-wont-close/150048/9 "2008-10-11T00:18:44Z")

</div>

Another Idea for you Glen,

Instead of using Close, and Destroy to get rid of the dialog, if it’s a  
Dialog that is used quite often, I would opt to use show\_modal, and  
end\_modal, and after end\_modal is called, just call hide, instead of  
destroy.

An Example of this:

class MyDialog \< Wx::Dialog  
def initialize(\*args)  
super  
ok = Wx::Button.new(self,Wx::ID\_OK,"&Ok")  
cancel = Wx::Button.new(self,Wx::ID\_CANCEL,"&Cancel")  
# … Add your own controls …  
evt\_button(Wx::ID\_OK) do  
# … do whatever processing you need …  
end\_modal(Wx::ID\_OK)  
end  
evt\_button(Wx::ID\_CANCEL) do  
# … Ask any, “Are you sure you want to cancel” here …  
# Or don’t even put this handler in, as it’s automatically  
handled by  
Wx::Dialog  
end  
end  
end

class MyFrame \< Wx::Frame  
def initialize(\*args)  
super  
# We create our dialog that we are going to use a lot, and show only  
# when needed.  
@dlg = MyDialog.new(self,-1,“Information Report”)  
# … add the rest of the stuff …  
evt\_button(ID\_SHOW\_DIALOG) do  
@dlg.show\_modal do |response|  
# … process response …  
end  
@dlg.hide  
end  
end  
end

This is just a bare bone example, that will not work, least you actually  
put  
it together. But it gives you an idea of how to use it, especially when  
you  
re-use a dialog multiple times.

Hope this helps,

Mario S.
