# Set\_size doesn't work inside initialize block

**URL:** <https://www.ruby-forum.com/t/set-size-doesnt-work-inside-initialize-block/161726>\
**Category:** wxRuby\
**Created:** [March 1, 2009, 7:20pm UTC](https://www.ruby-forum.com/t/set-size-doesnt-work-inside-initialize-block/161726 "2009-03-01T19:20:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Peoples](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f6c823/32.png) [@David\_Peoples](https://www.ruby-forum.com/u/David_Peoples)\
**Post date:** [March 1, 2009, 7:20pm UTC](https://www.ruby-forum.com/t/set-size-doesnt-work-inside-initialize-block/161726/1 "2009-03-01T19:20:08Z")

</div>

I’m putting a ListCtrl in a Frame, creating the ListCtrl during  
Frame.initialize(). I want to adjust the ListCtrl’s height to fit the  
number of rows the control contains, not just hardcode the height  
(presuming the control’s font will be different on different systems). I  
get the row height with ListCtrl.get\_item\_rect, calculate the new size,  
and call set\_size. But that call has no effect. Is there a way around  
this?

(I suppose I can make a temporary ListCtrl in the block, calculate the  
size from it, then destroy the temporary control, to work around this.  
But I’d prefer something less of a hack.)

Here is sample code:

require ‘wx’  
include Wx  
class MinimalFrame \< Frame  
def initialize(title)  
super(nil, :title =\> title, :size =\> [400, 400])  
border\_box = BoxSizer.new(HORIZONTAL)  
self.sizer = border\_box  
@list\_ctrl = ListCtrl.new(self, ID\_ANY,  
DEFAULT\_POSITION, Size.new(100, 100),  
LC\_REPORT | LC\_SINGLE\_SEL | LC\_HRULES | LC\_VRULES,  
DEFAULT\_VALIDATOR, “list\_ctrl”)  
@list\_ctrl.set\_size(Size.new(300,300))  
border\_box.add(@list\_ctrl, 0, ALIGN\_TOP | ALL, 10)  
end  
end  
App.run do  
self.app\_name = ‘Minimal’  
frame = MinimalFrame.new(“Minimal wxRuby App”)  
frame.show  
end

# Calling set\_size outside the initialize block

# (such as in a menu event handler) _does_ cause

# the ListCtrl to resize.

David Peoples

---

<div class="post-metadata">

**Author:** ![David\_Peoples](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f6c823/32.png) [@David\_Peoples](https://www.ruby-forum.com/u/David_Peoples)\
**Post date:** [March 1, 2009, 8:27pm UTC](https://www.ruby-forum.com/t/set-size-doesnt-work-inside-initialize-block/161726/2 "2009-03-01T20:27:37Z")

</div>

David Peoples wrote:

> I’m putting a ListCtrl in a Frame, creating the ListCtrl during  
> Frame.initialize(). I want to adjust the ListCtrl’s height to fit the  
> number of rows the control contains, not just hardcode the height  
> (presuming the control’s font will be different on different systems).

You’re using Sizers, which means you’re renouncing explicitly setting  
pixel size in favour of allowing the sizer to manage platform  
differences and the user resizing the window. This is probably why your  
set\_size call isn’t working.

If you want to control the height allocated to the ListCtrl within the  
sizer, specify its minimum size like this:

@list\_ctrl.min\_size = 200, 200

And add it to the sizer with a proportion of 0 (ie, minimum size only) -  
as you have at the moment. Whenever you need to change the height, just  
call min\_size= again, and then layout() the sizer.

a
