wxScrolledWindow not working with my custom controls.. Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
archangelmorph
In need of some credit
In need of some credit
Posts: 2
Joined: Wed Feb 04, 2009 3:21 pm

wxScrolledWindow not working with my custom controls..

Post by archangelmorph »

I have a problem with my application..

I have a wxScrolledWindow which contains a wxFlexGridSizer..

I then add several instances of my custom control (inherits from wxPanel) where each one occupies a fixed height but expands to the width of the parent (which in this case is the wxScrolledWindow..)

The problem I seem to be getting is that when I add more custom controls than can fit in the client window the scrollbars do not seem to appear..

I'm using xrc for my app so I can't post the UI setup code however here's where I grab the wxScrolledWindow in my frame init:-

Code: Select all

wxScrolledWindow* pMyScrolled = (wxScrolledWindow*)FindWindowById(wxXmlResource::Get()->GetXRCID("My_Scrolled"),NULL);
wxFlexGridSizer* pScrolledSizer = (wxFlexGridSizer*)pMyScrolled->GetSizer();

// here we grab the size which comes out at around (996,565)
wxSize siz = pMyScrolled->GetSize();
siz = pScrolledSizer->GetSize();

MyCustomPanel* pItem = new MyCustomPanel(pMyScrolled, wxID_ANY, wxT("Item1"), true);
pEntCtrlSizer->Add(pItem, 5, wxALL | wxGROW);
	
pItem = new MyCustomPanel(pMyScrolled, wxID_ANY, wxT("Item2"));
pScrolledSizer->Add(pItem, 5, wxALL | wxGROW);

pItem = new MyCustomPanel(pMyScrolled, wxID_ANY, wxT("Item3"));
pScrolledSizer->Add(pItem, 5, wxALL | wxGROW);

pItem = new MyCustomPanel(pMyScrolled, wxID_ANY, wxT("Item4"));
pScrolledSizer->Add(pItem, 5, wxALL | wxGROW);

// here we grab the size again after adding our custom 
// controls but the size is still (996,565) strangely?
siz = pMyScrolled->GetSize();
siz = pScrolledSizer->GetSize();
The strange thing is that the wxFlexGridSizer's wxSize seems to return a rather large height value (565) result before any custom controls have been added & on debugging it looks as though the size of the wxScrolledWindow just takes that value and add on it's own border (as if the client window is large than it is visibly on screen).
Since the wxScrolledWindow setup is all done automatically via the xrc i'm not sure why this value is coming out nor where it is coming from as in form builder no initial size is being set for it..

I'm at a loss as to where the issues are coming from & can't quite figure out what I need to do to get my scrolledwindow to show up the scrollbars where appropriate..
User avatar
Disch
Experienced Solver
Experienced Solver
Posts: 99
Joined: Wed Oct 17, 2007 2:01 am

Post by Disch »

You need to call SetVirtualSize with the size that the scrolled window is to scroll around (this would be greater than the client size of the wxScrolledWindow if there is to be a scroll bar)

Code: Select all

pMyScrolled->SetVirtualSize(pScrolledSizer->GetMinSize());
Do this after adding all your controls. You'll need to do this every time the sizer needs to grow/shrink (like if you add or remove controls at runtime).
archangelmorph
In need of some credit
In need of some credit
Posts: 2
Joined: Wed Feb 04, 2009 3:21 pm

Post by archangelmorph »

Cheers guys!

Works fine now!!
Post Reply