<p>Using C# 2.0 what is the best way to implement dynamic form controls?</p>

<p>I need to provide a set of controls per data object, so should i just do it manually and lay them out while increment the top value or is there a better way?</p>

[{'answer_id': 43541, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 1, 'selected': False, 'text': '<p>What do you mean by “dynamic”? A new, fixed set of controls for each data row in the data set? Then use a <code>UserControl</code> that contains your controls.</p>\n\n<p>Or do you mean that, depending on your data layout, you want to provide the user with a customized set of controls, say, one <code>TextBox</code> for each column?</p>\n'}, {'answer_id': 43542, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 1, 'selected': False, 'text': "<p>Yeah, I've found manually layout out controls (incrementing their Top property by the height of the control plus a margin as I go) to be reasonably effective.</p>\n\n<p>Another approach is to place your controls in Panels with Dock set to Top, so that each successive panel docks up against the one above. Then you can toggle the visibility of individual panels and the controls underneath will snap up to fill the available space. Be aware that this can be a bit unpredictable: showing a hidden panel that's docked can sometimes change its position relative to other docked controls.</p>\n"}, {'answer_id': 43544, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 4, 'selected': True, 'text': '<p>You can use panels with automatic layout such as <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.flowlayoutpanel.asp" rel="nofollow noreferrer">FlowLayoutPanel</a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.aspx" rel="nofollow noreferrer">TableLayoutPanel</a>.  </p>\n\n<p>Unfortunately there are only 2 panels with automatic layout out of box but you can create custom layout panel.</p>\n\n<p>I would recommend you to read following articles:  </p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/d4d31tbk.aspx" rel="nofollow noreferrer">How to: Create a Resizable Windows Form for Data Entry</a>  </p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/991eahec.aspx" rel="nofollow noreferrer">Walkthrough: Creating a Resizable Windows Form for Data Entry</a></p>\n\n<p>Another option would be using of WPF (Windows Presentation Presentation).<br>\nWPF is a perfect match for your task.<br>\nWPF controls can be hosted in WinForms apps so you don\'t have to switch to it completely.</p>\n'}, {'answer_id': 43554, 'author': 'Mihai Lazar', 'author_id': 4204, 'author_profile': 'https://Stackoverflow.com/users/4204', 'pm_score': 1, 'selected': False, 'text': '<p>Well that\'s the way we are doing it right now on a project. but that\'s only useful for simple cases. I suggest you use some sort of template for more complex cases.</p>\n\n<p>For instance I used Reflection to map a certain type of control to a certain property on my domain objects on an older project.</p>\n\n<p>You could try generating the code from templates using t4 see <a href="http://www.pnpguidance.net/Screencast/T4TemplatesVisualStudioCodeGenerationScreencast.aspx" rel="nofollow noreferrer">T4 Templates in Visual Studio for Code Generation Screencast</a> for a simple example. You can apply this to WinForms.</p>\n\n<p>Also DevExperience has a nice ( expensive ) framework, see DevExpress <a href="http://www.devexpress.com/Products/NET/Application_Framework/" rel="nofollow noreferrer">eXpressApp Framework™</a> .</p>\n'}, {'answer_id': 43579, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 2, 'selected': False, 'text': '<p>@Sam I know this question was about Windows Forms, but you should definitely start looking at WPF. This sort of scenario is <em>really</em> easy in WPF with DataTemplates and TemplateSelectors.</p>\n'}]
['https://Stackoverflow.com/questions/43536', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/428/']