Eliezer
Eliezer

Reputation: 7347

Design VB.NET UI Dynamically or at Compile Time

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.

Upvotes: 1

Views: 1179

Answers (3)

user1039415
user1039415

Reputation:

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

Upvotes: 2

pabdulin
pabdulin

Reputation: 35235

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

Upvotes: 3

Willem van Rumpt
Willem van Rumpt

Reputation: 6570

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.

Upvotes: 0

Related Questions