Creating controls "on demand".
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Creating controls "on demand".

Hybrid View

  1. #1
    Join Date
    Jun 2012
    Posts
    37

    Question Creating controls "on demand".

    Hi There, how are you? (Eri523, do you hear me? )

    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:

    Code:
    	private: System::Void numericUpDown99_ValueChanged(System::Object^  sender, System::EventArgs^  e) 
    			 {
    				 String^ gbx_name;
    				 gbx_name = "groupBox2";
    
    				 GroupBox^ groupBox2;	//Cria novo GroupBox
    				 groupBox2 = (gcnew System::Windows::Forms::GroupBox());	//Seilá que porra é essa
    				 groupBox2->SuspendLayout();	
    				 tabPage2->Controls->Add(groupBox2);	//Adiciona o controle no tabPage2
    
    				 groupBox2->Location = System::Drawing::Point(128, 26);
    				 groupBox2->Name = L"groupBox2";
    				 groupBox2->Size = System::Drawing::Size(94, 400);
    				 groupBox2->TabIndex = 0;
    				 groupBox2->TabStop = false;
    				 groupBox2->Text = L"groupBox2";
    
    				 groupBox2->ResumeLayout(false);
    				 groupBox2->PerformLayout();
    			 }
    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...

    Any ideas? Was I clear on the explanation?

    Thanks a lot!
    Last edited by fernando306; December 27th, 2012 at 09:38 PM.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: Creating controls "on demand".

    Quote Originally Posted by fernando306 View Post
    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 04: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.

  3. #3
    Join Date
    Jun 2012
    Posts
    37

    Re: Creating controls "on demand".

    Hi Eri523! Very helpful as always! Good idea to move controls from one tabPage to another. I'll surely try this and get back here ASAP!

  4. #4
    Join Date
    Jun 2012
    Posts
    37

    Re: Creating controls "on demand".

    Hi Eri523! I had the opportunity to test this idea and it's working fine, thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center