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.
Share
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).