Set_size doesn't work inside initialize block

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

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