WinForms UI design: displaying controls for only currently viewed hierarchy level

StackOverflow https://stackoverflow.com/questions/15795835

  •  01-04-2022
  •  | 
  •  

Pergunta

I am attempting to create a WinForms application which allows the user to display and edit data stored in a MSSQL database. The data being altered is hierarchical, and within a single level of the hierarchy the properties which may be altered are identical; in other words the controls for a single level of the hierarchy are the same, but they may differ from other levels.

I am trying to create the application in such a way that there is only a single form with controls that update based on the hierarchical level of the item being viewed by the user. I realize this is possible by putting all the controls for all levels on a single form and updating their 'Visible' property, but that method makes design of the form difficult due to clutter... Have any of you found a more elegant/less ugly solution?

Foi útil?

Solução

If you want to do this in WinForms, you can take advantage of the fact that visibility and enabled-ness are both "heritable" traits in the Windows model.

In other words, if you group all of your controls within a parent container (such a Panel or UserControl), then disable that container control and make it invisible, all of its child controls will also become likewise disabled and invisible.

I recommend creating UserControls for each level of the hierarchy. The line of thinking is pretty much the same as if you used separate Forms, except that they're not actually separate Forms. Multiple UserControl objects can be displayed on a single form, so you can have as many as you need. This keeps all related controls together, which makes management much easier. You can also interact individually with these UserControls in the WinForms designer, just like they were separate Forms, solving the "clutter" problem.

To toggle between "active" hierarchies, loop through all of your UserControl objects. Make the currently "active" one enabled and visible (all of its children will automatically become likewise). Make the rest of them disabled and hidden (and all of their children will automatically become likewise).

I won't argue with HighCore here, though. If you don't already know WinForms, you could just as easily spend your time learning WPF. If you decide to do so and want to know how to accomplish this same task in that UI framework, please be sure to ask a new question.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top