-1

Question:

Is there some effective way to hide some portions of the WinForm/WPF desktop program based on user settings/permissions?


Why I need this?

I'm starting a big accounting project which will contain hundreds of forms/dialogs.

The program is going to launch a main window which shows 1 to 4 divisions. The user selects each of those and it will then launch the a window which contains a sidebar with a bunch of buttons on sidebar (something like Microsoft Outlook). Now, when the user clicks on each of these buttons, it will open that section of the program and the user will work with that part. Based on the user permissions/settings, there's a need to sometimes hide some of these buttons though. For instance suppose I have 4 main divisions A, B, C and D. When you launch A, you'll get a sidebar containing A1, A2, ..., A100. A user might opt to see only A1 & A50!

Our initial approach was to use WinForms for this because the team was very familiar with it. I suspect that for doing so, we have to build some sort of model which contains information about user preferences and write lines of code like btnA1.Visible = false; a lot.

Frankly just thinking about doing that disgusts me. That's why I'm looking for a better way to achieve such result. I've searched around and found PRISM.

I'm not sure just yet but I think to use PRISM I need to make each of those buttons or their dialog a module and load them after I decide which of them is needed for the user.

It seems like a nice way to do this but considering the fact that this project is very urgent and we don't need to load different modules for different users (we just need to load them - ideally on demand - and sometimes hide some), I have some concerns:

  1. My team might need some time to learn WPF
  2. All of us don't know much about Unity and PRISM.
  3. This might be overly complex, i.e. there might be a more simple way to achieve this without going into such lengths.

Also, I'm watching Prism & Silverlight Series and PRISM5 for WPF from Channel9.

Community
  • 1
  • 1
Alireza Noori
  • 14,961
  • 30
  • 95
  • 179
  • 1
    Under WinForms you could put your sections within Panels. Each Panel then contains all the controls for that section. When you show or hide a panel, it will automatically show/hide all the controls contained within it. So, you don't have to show/hide all the individual controls, just the parent Panel control. – Brad Rem Dec 24 '14 at 15:12
  • @BradRem What I meant wasn't the fact that I need to hide each of the controls, just the button launching the form. Each window contains a lot of these and you could imagine, at least I'd be using 100 lines per form. It's not impossible but maybe there's a better way. – Alireza Noori Dec 24 '14 at 15:20
  • I guess I still don't understand, but what about tagging these controls in a certain way and then programatically enumerating all the controls on your form applying the proper visibility based on the tag? – Brad Rem Dec 24 '14 at 15:22
  • @BradRem What you're suggesting is the best approach for simple WinForms and that's what I would do. But then again, maybe there's a better way to just maybe lazy-load the parts too? – Alireza Noori Dec 24 '14 at 15:28
  • You should be use databinding when possible. You can do some really complex UI logic extremely cleanly. You shouldn't have one line of code like `btnA1.Visible = false`. – The Muffin Man Dec 24 '14 at 15:37
  • @TheMuffinMan I haven't used binding in Winforms not sure if it's possible, but using WPF + Binding is much more appealing for me. Not sure how the team feels about it though :D – Alireza Noori Dec 24 '14 at 15:49
  • Winforms absolutely supports databinding. Whatever you use, winforms or wpf, you should be databinding your UI. If you're making a webapp you should be databinding your UI. The faster you come to terms with that the faster you will start creating more maintainable apps. – The Muffin Man Dec 24 '14 at 16:13
  • @TheMuffinMan the amount of support for databinding in winforms is non existent compared to current technology like WPF. And there's no reason to stick to ancient technologies like winforms that don't support anything. You might as well want to recommend the OP to use COBOL for this project. – Federico Berasategui Dec 24 '14 at 16:23
  • 2
    I don't care what he uses. He's the one that tagged the question with Winforms... I was saying that it's possible. Databinding is databinding. My argument is for using it period. – The Muffin Man Dec 24 '14 at 16:25
  • @TheMuffinMan Please, please show me how to do anything like [this](http://stackoverflow.com/a/21898676/643085) in winforms. That's the first example off of the top of my head. I can give you countless others. – Federico Berasategui Dec 24 '14 at 16:27
  • 2
    Look at my first comment. It said nothing about winforms. It just said use databinding for your UI rather than explicitly setting your UI controls values. Why are you trying to create an argument out of a non existing topic? – The Muffin Man Dec 24 '14 at 16:31

1 Answers1

2

a window which contains a sidebar with a bunch of buttons on sidebar (something like Microsoft Outlook). Now, when the user clicks on each of these buttons, it will open that section of the program and the user will work with that part.

That sounds to me like a TabControl. You'd rather not try to reinvent the wheel as it's already been invented.

The only difference between that example and your requirement is that instead of hard coding the tabs you're going to bind to a collection of ViewModels, like this, and then have each instance of TabViewModel toggle it's own IsVisible property depending on user permissions / user selections.

Simple as that. No need for complex MVVM frameworks. No need for silly obsolete useless winforms stuff.

Community
  • 1
  • 1
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • The things you mentioned are quite nice. The only problem is, the program we're trying to create shows dialogs (which I hate) instead of flat tab content. I'm not sure I can convince the costumer to use this design. They're used to those dialogs :( – Alireza Noori Dec 24 '14 at 21:55
  • @AlirezaNoori That's not a problem at all. Simply change the TabControl for another Selector that does not show any flat content, then bind it's `SelectedItem` property to a property in the MainViewModel, which, when changed, is going to trigger the creation of the popup window with the corresponding DataTemplate and DataContext. – Federico Berasategui Dec 24 '14 at 22:05
  • Very nice point. Thank you. I think I have an idea of what to do for the whole application which is both efficient and easy. The only problem is to convince the team to use WPF. Thanks again. – Alireza Noori Dec 24 '14 at 22:09
  • @AlirezaNoori to "convince the team" you only need 1 argument: winforms sucks and is totally useless. Whatever you can do in winforms can be done in WPF with 10% the amount of code and in a much cleaner way to WPF being properly designed to support DataBinding. That said, the WPF tooling improvements in the upcoming Visual Studio 2015 discussed [here](http://channel9.msdn.com/Blogs/DevRadio/The-Future-of-WPF) will make winforms look like COBOL in comparison. – Federico Berasategui Dec 24 '14 at 22:15
  • I think after the first time I used WPF, I never created a single WinForms application. I'm all for it. I really hate both WinForms and WebForms (both got "forms"!!) but as I mentioned my team don't have enough experience with WPF but I think they'll go for it. Thanks – Alireza Noori Dec 24 '14 at 22:21
  • BTW, thank you for the video you posted. Interestingly I have downloaded it but never watched it and forgot about it :D Now, starting the video ... :D – Alireza Noori Dec 24 '14 at 22:22