1

I'm designing a program now and I've come across a UI issue. I want to have a bunch of different screens, yet I don't want to have to use 20 different forms (I'm pretty sure that's bad practice anyways), and I don't want to lay down all the controls at once in the designer because then things get messy. I had an idea of running a sub on startup that would dynamically generate all the controls I need with all the right coordinates and settings, and then dump al the ones needed for a specific screen into a Panel. Then I return an ArrayList of panels, and pull out the correct panel to attach to the form when I need that screen. The only problem is that my form will not have any implicit knowledge of its controls, so I'm assuming I wouldn't be able to use the WithEvents / Handles.* keywords to declare handlers in the form class (which is what I'd rather do). I know this sounds a little convoluted; I'm still trying to understand what I'm saying :) If anyone has any ideas about what way is better, or if there's a better one that I haven't come across, let me know.

3 Answers 3

3

The thing you are talking about is a designer (static) vs. programmatic (dynamic) UI generation. This is less popular kind of holy war since there are fans on both sides. You can use events etc. with both approaches (since designer actually generates code in the end). It's up to you to decide. There are some benefits in programmatic approach, but it usually requires more time (esp. first time).

Charles Petzold is a known fan of programmatic UI creation, you can learn some tricks from this book for example (but bad book on C# in general, IMHO).

2
  • Thanks. I was actually unaware there was a holy war about this. I'm gonna go check out Petzold and anyone else I can round up.
    – Eliezer
    Nov 28, 2011 at 12:32
  • @Eliezer well, maybe holy war was exaggeration, but there is certanly some debate about it. Nov 29, 2011 at 1:32
2

I think that the answer is to do a bit of both. Having 20 forms is a maintenance nightmare and dynamically generating everything is as bad.

Try to work out what the common features are in your 20 design. Do they all use a 3-column layout? Do they all use a common set of controls? etc.

Then create those common elements. For layout put some container controls on your form and get them resizing the way you want. If you can, create some custom controls to display or handle common items of data.

Then you can dynamically add your custom controls into your layout.

Event handling is actually the easiest part. You can write an event handling procedure and then use AddHandler to link the event from the dynamically created control to the prewritten code.

AddHandler

1
  • I upvoted you for a good suggestion. Unfortunately it won't work in my case but thanks anyways :)
    – Eliezer
    Nov 28, 2011 at 12:34
0

I would just go for the 20 different forms. Different screens, different forms, nothing wrong with that.

It gives you all the designtime benefits, and avoids the hell of aligning and arranging everything in code, which will be an absolute maintenance nightmare.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.