# Multiple forms or panels

**URL:** <https://www.ruby-forum.com/t/multiple-forms-or-panels/169267>\
**Category:** wxRuby\
**Created:** [June 1, 2009, 6:09pm UTC](https://www.ruby-forum.com/t/multiple-forms-or-panels/169267 "2009-06-01T18:09:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [June 1, 2009, 6:09pm UTC](https://www.ruby-forum.com/t/multiple-forms-or-panels/169267/1 "2009-06-01T18:09:45Z")

</div>

How do I organize widgets in a form when I need them placed in more than  
a single column or row? I leaned how to use BoxSizer and successfully  
displayed my widgets in a single column but can’t get the next column.

Here is my code, which puts a text, radio, button all in one column, but  
what if I wanted my radios over to the right?:

require “rubygems”  
require “wx”  
include Wx

class MyFrame \< Frame  
def initialize  
super(nil, :title =\> “My Test Frame”)

```
@panel = Panel.new(self)
@result = StaticText.new(@panel, :label=>'Result')
@y = TextCtrl.new(@panel, -1, '10')
shape_choice = %w(Circular Rectangular Trapezoidal )

submit_button = Button.new(@panel, :label => 'Calculate')
my_radio = RadioBox.new(
  @panel,
  :label => "Select Conduit Shape",
  :choices => shape_choice,
  :major_dimension => 1
)
@panel_sizer = BoxSizer.new(VERTICAL)
@panel.set_sizer(@panel_sizer)

@panel_sizer.add(@y, 0, GROW|ALL, 2)
@panel_sizer.add(my_radio, 0, GROW|ALL, 2)
@panel_sizer.add(submit_button, 0, GROW|ALL, 2)
@panel_sizer.add(@result, 0, GROW|ALL, 2)

evt_button(submit_button.get_id()) {|cmd_event|

```

on\_submit(cmd\_event)}  
evt\_radiobox(my\_radio.get\_id()) {|cmd\_event|  
on\_change\_radio(cmd\_event)}

```
show()

```

end

def on\_change\_radio(cmd\_event)  
@selected\_shape = cmd\_event.string  
end

def on\_submit(cmd\_event)  
y = @y.get\_value().to\_f  
area = case  
when @selected\_shape == ‘Circular’ then Math::PI \* y\*\*2 / 4.0  
when @selected\_shape == ‘Rectangular’ then y \* 5.0  
else (5.0 + 2.0 \* y) \* y  
end

```
@result.label = "#{area} square feet"

```

end  
end

class MinimalApp \< App  
def on\_init  
MyFrame.new  
end  
end

MinimalApp.new.main\_loop

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [June 1, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/multiple-forms-or-panels/169267/2 "2009-06-01T18:54:47Z")

</div>

Jason L. wrote:

> How do I organize widgets in a form when I need them placed in more than  
> a single column or row? I leaned how to use BoxSizer and successfully  
> displayed my widgets in a single column but can’t get the next column.

It sounds like you want either Wx::GridSizer or Wx::FlexGridSizer. The  
latter allows for variable width columns or rows, so I generally find it  
more useful.

There is also Wx::GridBagSizer, which allows cells to span rows or  
columns, but I find nesting other simpler sizers easier to use than the  
API for it.

alex

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [June 1, 2009, 9:24pm UTC](https://www.ruby-forum.com/t/multiple-forms-or-panels/169267/3 "2009-06-01T21:24:14Z")

</div>

Thank you for the pointer. I tried a simple example, doing my best to  
read through wxWidgets examples and wxruby docs and - no success. I  
created 4 buttons and they all show on top of each other. Can you see  
anything I’m doing wrong? Thank you.

I’m doing this:

class MyFrame \< Frame  
def initialize  
super(nil)  
panel = Panel.new(self)  
gs = FlexGridSizer.new(2,2,20,20)

```
b1 = Button.new(panel, :label=>"one")
b2 = Button.new(panel, :label=>"two")
b3 = Button.new(panel, :label=>"three")
b4 = Button.new(panel, :label=>"four")

gs.add(b1)
gs.add(b2)
gs.add(b3)
gs.add(b4)

gs.add_growable_row(1,1)
gs.add_growable_col(1,1)

show()

```

end  
end

class MinimalApp \< App  
def on\_init  
MyFrame.new  
end  
end

MinimalApp.new.main\_loop

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [June 2, 2009, 12:01am UTC](https://www.ruby-forum.com/t/multiple-forms-or-panels/169267/4 "2009-06-02T00:01:23Z")

</div>

ah, I see. Thank you for that clarification. Just what I needed!

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [June 1, 2009, 11:07pm UTC](https://www.ruby-forum.com/t/multiple-forms-or-panels/169267/5 "2009-06-01T23:07:24Z")

</div>

Jason L. wrote:

> Thank you for the pointer. I tried a simple example, doing my best to  
> read through wxWidgets examples and wxruby docs and - no success. I  
> created 4 buttons and they all show on top of each other. Can you see  
> anything I’m doing wrong? Thank you.
> 
> I’m doing this:
> 
> class MyFrame \< Frame  
> def initialize  
> super(nil)  
> panel = Panel.new(self)  
> gs = FlexGridSizer.new(2,2,20,20)
> 
> ```
> b1 = Button.new(panel, :label=>"one")
> b2 = Button.new(panel, :label=>"two")
> b3 = Button.new(panel, :label=>"three")
> b4 = Button.new(panel, :label=>"four")
> 
> gs.add(b1)
> gs.add(b2)
> gs.add(b3)
> gs.add(b4)
> 
> gs.add_growable_row(1,1)
> gs.add_growable_col(1,1)
> 
> show()
> 
> ```
> 
> end  
> end
> 
> class MinimalApp \< App  
> def on\_init  
> MyFrame.new  
> end  
> end
> 
> MinimalApp.new.main\_loop

Two things are not quite right. First, you don’t need the  
‘add\_growable\_row’ etc as you have declared your grid to be 2x2. Also,  
you have not told the panel about your sizer, so it does not know how to  
layout your widgets. Change the middle part to:

* * *

```
gs.add(b1)
gs.add(b2)
gs.add(b3)
gs.add(b4)

# gs.add_growable_row(1,1) # delete this
# gs.add_growable_col(1,1) # delete this
panel.sizer = gs # set the sizer

show()

```

* * *

You don’t need to create a separate panel to hold your widgets in, and  
can instead add the widgets to ‘self’. Personally, I prefer to use  
nested combinations of BoxSizers in most cases. The following example  
may be helpful.

* * *

require ‘wx’

class BoxFrame \< Wx::Frame  
def initialize  
super(nil, :title =\> “BoxSizers”)

```
# create two sub-sizers
left_sizer = Wx::BoxSizer.new Wx::VERTICAL
left_sizer.add(Wx::StaticText.new(self, Wx::ID_ANY, "Left side"))
left_sizer.add(Wx::Button.new(self, Wx::ID_ANY, "Button"))

right_sizer = Wx::BoxSizer.new Wx::VERTICAL
right_sizer.add(Wx::StaticText.new(self, Wx::ID_ANY, "Right side"))
right_sizer.add(Wx::Button.new(self, Wx::ID_ANY, "Button"))

# add left/right to main sizer with some padding
main_sizer = Wx::BoxSizer.new Wx::HORIZONTAL
main_sizer.add(left_sizer, 1, Wx::ALL, 10)
main_sizer.add(right_sizer, 1, Wx::ALL, 10)

set_sizer main_sizer

```

end  
end

class GridFrame \< Wx::Frame  
def initialize  
super(nil, :title =\> “FlexGridSizer”)

```
main_sizer = Wx::FlexGridSizer.new(2, 2, 10, 20)

main_sizer.add(Wx::StaticText.new(self, Wx::ID_ANY, "Left side"))
main_sizer.add(Wx::StaticText.new(self, Wx::ID_ANY, "Right side"))
main_sizer.add(Wx::Button.new(self, Wx::ID_ANY, "Left Button"))
main_sizer.add(Wx::Button.new(self, Wx::ID_ANY, "Right Button"))

set_sizer main_sizer

```

end  
end

## Wx::App.run do BoxFrame.new.show GridFrame.new.show end

Hope that helps,

```
Peter.

```
