# Detaching Controls

**URL:** <https://www.ruby-forum.com/t/detaching-controls/134359>\
**Category:** wxRuby\
**Created:** [April 10, 2008, 3:56am UTC](https://www.ruby-forum.com/t/detaching-controls/134359 "2008-04-10T03:56:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nilz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/34f0e0/32.png) [@nilz](https://www.ruby-forum.com/u/nilz)\
**Post date:** [April 10, 2008, 3:56am UTC](https://www.ruby-forum.com/t/detaching-controls/134359/1 "2008-04-10T03:56:37Z")

</div>

Hi,

Does anyone know how to detach controls from a panel or frame? What I  
want to do is click a button and redraw a new UI without the button. Is  
there any possible was to do this? Thank you for any help in advance. 😃

---

<div class="post-metadata">

**Author:** ![nilz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/34f0e0/32.png) [@nilz](https://www.ruby-forum.com/u/nilz)\
**Post date:** [April 10, 2008, 4:08am UTC](https://www.ruby-forum.com/t/detaching-controls/134359/2 "2008-04-10T04:08:39Z")

</div>

Nilu Senevi wrote:

> Does anyone know how to detach controls from a panel or frame? What I  
> want to do is click a button and redraw a new UI without the button.  
> If your layout is controlled by sizers, use Sizer#remove or  
> Sizer#detach, then call destroy on the removed Window. You may need to  
> call Sizer#layout afterwards to adjust to the new window contents.

If not, just call Window#destroy. It’s one of the rare occasions that  
calling this directly is useful.

[http://wxruby.rubyforge.org/doc/window.html#Window\_destroy](http://wxruby.rubyforge.org/doc/window.html#Window_destroy)  
[http://wxruby.rubyforge.org/doc/sizer.html#Sizer\_remove](http://wxruby.rubyforge.org/doc/sizer.html#Sizer_remove)

alex

---

<div class="post-metadata">

**Author:** ![nilz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/34f0e0/32.png) [@nilz](https://www.ruby-forum.com/u/nilz)\
**Post date:** [April 10, 2008, 7:40am UTC](https://www.ruby-forum.com/t/detaching-controls/134359/3 "2008-04-10T07:40:58Z")

</div>

Another way, if your planning to re-use the control, is to call  
hide()/show(false) on the button, then call layout() on the window, to  
re-draw the controls. Hide will make it invisible, so that it’s no  
longer  
there.

An example:

begin  
require ‘rubygems’  
rescue LoadError  
end  
require ‘wx’

class MyFrame \< Wx::Frame  
include Wx  
def initialize(\*args)  
super  
@button1 = Button.new(self,1000,“Hide Me!”)  
@button2 = Button.new(self,1001,“Show other button”)  
hbox = BoxSizer.new(HORIZONTAL)  
hbox.add(@button1,1,ALL)  
hbox.add(@button2,1,ALL)  
set\_sizer(hbox)  
evt\_button(1000) do  
@button1.show(false)  
layout()  
end  
evt\_button(1001) do  
@button1.show(true)  
layout()  
end  
end  
end

Wx::App.run do  
MyFrame.new(nil,:title=\>“Button Hiding  
Example”,:size=\>[200,60]).show()  
end

L8ers,

---

<div class="post-metadata">

**Author:** ![nilz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/34f0e0/32.png) [@nilz](https://www.ruby-forum.com/u/nilz)\
**Post date:** [April 10, 2008, 10:59am UTC](https://www.ruby-forum.com/t/detaching-controls/134359/4 "2008-04-10T10:59:24Z")

</div>

Thank you Mario for the quick reply. I will try it out:D

---

<div class="post-metadata">

**Author:** ![nilz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/34f0e0/32.png) [@nilz](https://www.ruby-forum.com/u/nilz)\
**Post date:** [April 10, 2008, 11:01am UTC](https://www.ruby-forum.com/t/detaching-controls/134359/5 "2008-04-10T11:01:52Z")

</div>

Thanks Alex. I will try it out 😃
