0

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?

Conduit
  • 2,675
  • 1
  • 26
  • 39
  • `Have any of you found a more elegant/less ugly solution?` - Have you tried current Windows UI technologies instead? They're XAML-based, faster, more scalable and simply beautiful. – Federico Berasategui Apr 03 '13 at 19:38
  • @HighCore I assume you're referring to WPF? I'm pretty new to C# (programming in general, honestly) and haven't yet taken the plunge... – Conduit Apr 03 '13 at 19:56
  • yep, If you don't already know winforms, don't waste your time with it. Admittedly, WPF has a steeper learning curve, but it's much more powerful and scalable. Besides, the new Microsoft UI technology (WinRT) is also XAML-based, and shares most concepts with WPF. – Federico Berasategui Apr 03 '13 at 20:00
  • @HighCore Noted. I'll continue to work on a WinForms-based approach, but will definitely start playing with WPF. Any suggested reading for an interested newbie is welcome. – Conduit Apr 03 '13 at 20:02
  • 1
    http://stackoverflow.com/questions/15681352/transitioning-from-windows-forms-to-wpf# – Federico Berasategui Apr 03 '13 at 20:04
  • @HighCore Looks like the conceptual shift between WF and WPF might actually mesh better with the way I think about programming... Thanks for the link. – Conduit Apr 03 '13 at 20:17

1 Answers1

1

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.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • I've learned the basics of C# using WinForms and know nothing of XAML, so I'll likely create a WinForms version as a prototype. This sounds workable! I'll give it a shot. – Conduit Apr 04 '13 at 12:52
  • @CodyGray +1. I knew we both could come to agree about something someday. Thanks =) – Federico Berasategui Apr 04 '13 at 16:06