Forum: wxRuby set_size doesn't work inside initialize block

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
251086a67766003b417ef869c0121c7e?d=identicon&s=25 David Peoples (Guest)
on 2009-03-01 19:20
(Received via mailing list)
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
06f6780c99d4a8dd71f2b474082ea9ce?d=identicon&s=25 Alex Fenton (Guest)
on 2009-03-01 20:27
(Received via mailing list)
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
This topic is locked and can not be replied to.