3 Replies - 2099 Views - Last Post: 22 November 2010 - 04:03 PM Rate Topic: -----

#1 endorush   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 11
  • Joined: 23-December 09

Add control to main form from outside the form class

Posted 22 November 2010 - 02:33 PM

The program reads data from a spreadsheet and passes that data to this Factory class which has a Create() method with the return type Control. One of the arguments is string "ControlType", and if it says "label", the Create() method will call the Label constructor and return the label as a Control. The Create() method also sets the coordinates, size, and text of the control before returning it (which it also receives from the spreadsheet).

That way the control layout is customizable through the spreadsheet without a recompile. Also, controls are dynamically created and destroyed based upon certain user actions at runtime.

This all works fine, but the thing is, I have controls that belong in the MainForm control collection, and I have a GroupBox with CheckBoxes inside of it so I want to add them to the GroupBox control collection. The Create() method in the Factory accepts a string for "Owner", so I can specify which Control Collection to add the Control to. However, It looks like the Form.Controls.Add() method is private? I was about to hack something up real quick and add a public method to the form class to call the this.Controls.Add() method, but figured this method must be private for a reason.

Is there some proper way to add controls to a collection from outside of the Form class? I was trying to keep all the creation details hidden inside the Create() method.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Add control to main form from outside the form class

#2 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Add control to main form from outside the form class

Posted 22 November 2010 - 02:40 PM

Hmm. That seems unconventional to me. It's private because it's typically not a good idea for a form to be getting it's display logic from another location. Forms should theoretically be self contained.

But if you have to work within this pattern, there's no reason you couldn't just expose the Controls.Add method via a public method. I'd just look real hard at my design to see if it's necessary.
Was This Post Helpful? 0
  • +
  • -

#3 endorush   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 11
  • Joined: 23-December 09

Re: Add control to main form from outside the form class

Posted 22 November 2010 - 03:22 PM

that's the feeling I was starting to get. I need to do some research on something like ".net best practice" or something. I think the last thread I made regarding the design of a different program also had people saying it was "unconventional".

Maybe I should have the MainForm request all the Controls from Create() then add them to the Collection itself.

I can write plenty of code that "does stuff", but now I'm trying to build larger systems with many components and the challenging part is deciding how they should communicate and dividing up responsibilities.

thanks

This post has been edited by endorush: 22 November 2010 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Add control to main form from outside the form class

Posted 22 November 2010 - 04:03 PM

Well, Patterns and Practices are a wonderful thing. But sometimes you see them taken a bit far. Do you really need a factory pattern here? I've never seen that for UI controls. You should probably use the factory to create the data elements or collections, then allow the forms to assign the data to controls. Display logic lives with the forms. That, of course, assumes that you wrote the factory class, and you're not working with someone else's code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1