If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Creating controls "on demand".

I'm facing this problem, I'm not sure if there is a possible solution:

I have 4 tabPages, each with 24 groupBoxes that are similar and contain textboxes, trackbars, etc. So that's a total of 96 groupBoxes, plus everything inside them!

Each tabPage has a numeric up down that controls the visibility of these groupBoxes. So, for example, if only 5 is selected, only 5 groupBoxes will appear, although 24 do exist. I know, kinda amateur approach

Problem is that I need a 5th tabPage, which will show the "visible" groupBoxes of all other tabPages together. So for example, if at tabPage1 user selected 2, tabPage2 user selected 5, tabPage3 user selected 1, tabPage4 user selected 3, tabPage 5 must show a CLONE of those groupBoxes, being a total of 11. (2 + 5 + 3 + 1 ).

An obvious and reaaaally "newbie" solution for that would be having all 96 groupBoxes sitting there and invisible... As they must be shown, they're turned into Visible and the Drawing Point is recalculated so they won't overlap or have a gap in between. Of course I'm looking for something more neater and professional!

I thought I could create the controls "on demand". I successfully created and drew it at the right spot with the code bellow:

The problem is that I don't know how to create the controls with the names in sequence... For example, if I already created "groupBox2", how could I write a code that knows that and then it creates by itself "groupBox3" and on and on? We're talking of 96 groupboxes here...

Re: Creating controls "on demand".

Originally Posted by fernando306

Each tabPage has a numeric up down that controls the visibility of these groupBoxes. So, for example, if only 5 is selected, only 5 groupBoxes will appear, although 24 do exist. I know, kinda amateur approach

Well, if it's actually possible to select any number of group boxes to display from 1 to 24, it would leave a lot of the form's real estate empty if a low number of controls is set. One imaginable approach to remedy this is to dynamically resize the form based on the number of controls to display, but, in particular as the spin control to select that number sits on the form itself, that may well look pretty awkward too. It also would still leave much empty space on some of the tab pages if there are significant differences between the number of controls to display on each of them.

Another possible approach may be putting the group boxes into a scrollable container control, directly or indirectly derived from ContainerControl or ScrollableControl, potentially combined with making the enclosing form user-sizable. In fact, the Form class itself is such a derivate, perhaps allowing for a really elegant and simple solution (in that case you may be scrolling the entire form contents including the tabs). I have never used that feature of Form myself yet, though.

Problem is that I need a 5th tabPage, which will show the "visible" groupBoxes of all other tabPages together. So for example, if at tabPage1 user selected 2, tabPage2 user selected 5, tabPage3 user selected 1, tabPage4 user selected 3, tabPage 5 must show a CLONE of those groupBoxes, being a total of 11. (2 + 5 + 3 + 1 ).

An obvious and reaaaally "newbie" solution for that would be having all 96 groupBoxes sitting there and invisible... As they must be shown, they're turned into Visible and the Drawing Point is recalculated so they won't overlap or have a gap in between. Of course I'm looking for something more neater and professional!

Are the clones supposed to actually be operable or are they just for display purposes? In the latter case things may perhaps be simplified by merely creating shadows of the controls using the Control::DrawToBitmap() method rather than using real controls. Otherwise, a possible approach might be to move the controls between their original tab page and tab page 5, in response to TabControl::Selected and related events, rather than actually creating clones, thereby exploiting the fact that only one tab page can be visible at a time. This approach feels somewhat haphazard to me and may become hard to maintain at some point, but it still may be easier than managing a bunch of dynamically created controls, most of which are sharing their manipulated data.

I thought I could create the controls "on demand". I successfully created and drew it at the right spot with the code bellow:

[...]

The problem is that I don't know how to create the controls with the names in sequence... For example, if I already created "groupBox2", how could I write a code that knows that and then it creates by itself "groupBox3" and on and on? We're talking of 96 groupboxes here...

Don't the dynamic control name creation techniques discussed in http://forums.codeguru.com/showthrea...59#post2088759 help here? Though not explicitly mentioned (at leat not in that specific post), of course they can be used for creating controls as well, not just for accessing existing ones. If they don't help, then why not?

Last edited by Eri523; December 30th, 2012 at 05:36 AM.
Reason: Changed the link in the last paragraph to directly point to the post I'm referring to

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.