views:

829

answers:

6

I'm writing a WinForms based application for Windows Mobile, targeting the CompactFramework 2.0 and coding in C#.

My application has a menu which the user can select severeral job functions from.

My question is this:

For the job specific screens, should I load a UserControl into a panel in the main form, or is it better to open a new form on top of the main form?

They both seem to meet my needs, and I don't know if there is a "right" answer here.

Is one method more correct than the other?

A: 

Depends on the complexity of the form/Control. Going the UserControl route will provide better performance as you will not have all of the form create functionality to also deal with.

+2  A: 

In my experience, depending on your hardware, loading controls far outperforms launching distinct forms. Our devices (Motorola WT4090 with 32meg) have no hardware acceleration, and drawing complete forms seems to really tax them. There is as much as 4-6 second delay any time a form is launched; controls, however, are almost instantaneous.

John Kraft
Not sure why that might be. A Form derives from Control and therefor is a Control itself. It's simply a container control. It really has very little overhead, unless the children it contains do a lot of work at construction (and that's no fault of the Form).
ctacke
@ctacke I'm not above saying we are doing something very wrong, but I did try a comparison with using Forms vs. Controls with only a couple of simple textboxes; similar results. On our devices specifically, it seems to be an issue with clearing the previoius form, and then displaying the new complete form. I've guessed that the improved speed is because the device only has to draw a much smaller rectangle and does not need to dispose of the previous for either. Purely guesses though.
John Kraft
A: 

My advice is to create a new Windows Form for every different logical action. If you need a form that is logically independent, then is better to have a separate Windows Form for it.

To speed things up you could construct all your forms in application's start up. This way you will have a delay when your application is launching (most users will be OK with this), but afterwards everything will run faster.

kgiannakakis
A: 

Maybe the best answer is to create a series of user controls and experiment with loading them onto the main form. It should be a fairly easy matter to then try to create a series of forms that have just the user control to see if you can improve performance.

If you don't see any performance benefit either way, it seems that it would just be a matter of preference.

Andy Stampor
+1  A: 

Forms are Controls (just look at the inheritance chain), and a Form itself doesn't have a whole lot of overhead in and of itself. It creates a native Window, but that's about it.

What should drive your decision is how you use and display your UI elements in your solution. Forms are conducive to many UI architectures, and they're what most developers are familiar with. The designer supports them well, and creating, showing, hiding and closing Forms is a well documented, often used paradigm that works just fine. Yes, you have the load penalty whenever a Form is created as it creates all of its contained controls, but you're going to pay that even if it's a UserControl. The child controls have to be created in either case.

One might argue that Forms require that you recreate them every time, but that's not true. If you use ShowDialog or Hide instead of Close, you can reuse your Forms and pay the price once. The advantage here is that the Form holds your Controls in teh collection and manages all of that for you, so you have little to worry about with the GC and remembering what roots you have alive.

The UserControl paradigm is more complex - you have to manage loading and unloading controls yourself to keep memory pressure low. Complexity also increases cost - cost to maintain, cost to support, and likely cost to develop. However there are some clear advantages to UserControls in some scenarios too. If you use an MVC/MVP pattern and some form of framework that handlesyour Views, a USerControl makes a really good View with the Form becoming the Workspace (the SCSF for the desktop is a classic example of this, and so is the OpenNETCF.IoC framework for the CF).

So which is "better"? It depends on how and where you're using the elements, how your team is already doing development, and how you've architected the solution you're plugging in to. In short, the is no one right answer.

ctacke
A: 

Use Forms. It's the way the other applications behave. It's the way the user expexts your application to work.

Look at the pre-installed "Contacts" application: On startup you get a contact list. Once you select a contact, a new form is being opened on top of the current window. It's showing all the contact's details. Selecting "edit" from the menu will open yet another form, allowing you to edit the contact. The "Tasks" applicatoin behaves just the same way.

So the user knows: Clicking somewhere will open up a new form, and closing that form will get me back to the last form. You should work with that knowlegde rather than against it. When you use a single form, the user might repeatedly close it while he actually wanted to get back to tha last form.

From a performance point of view I find that forms open fast enough (<1 sec) on my WM 5 and my WM 6 devices.

And while it's possible to achieve form-like behaviour with UserControls, it seems more straitforward to use forms when you need form-like behavior.

So my bottom-line is: Use Forms!

P.S.: There's a good article on CodeProject on : How to create MDI Application in Compact framework

Felix Alcala
All of that same behavior can be achieved through UserControls as well. You're used to programming in Forms, so it looks like Forms behavior, but that doesn't mean that's how it's actually implemented.
ctacke
@ctake: thanks for pointing that out. I edited the post accordingly.
Felix Alcala