[wxruby-users] set_size doesn't work inside initialize block

David Peoples davidp at touringcyclist.com
Sun Mar 1 12:55:16 EST 2009


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




More information about the wxruby-users mailing list