Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There are lots of articles about XML, dynamically adding forms and dynamically adding controls for vb.Net. But, if there's an article on how to dynamically define a form in XML, or other configuration settings, to dynamically create forms/subforms with all the controls and events, I've yet to find it; at least nothing that's impressed me as a clean, efficient and repeatable option.

The concept is that most of the forms in most database applications are nothing more than support forms. Configurations and lists used in the main form. Like a "Books" table, and an "Authors" table, maybe a "Bindings" table or "Paper Type" - bad examples, I know, but the point is that all these sub-forms are used to manage ancillary tables not related to the business application other than that they are used for the lists to make the main form
intelligible.

The main form will always need to be custom, but, all the lookup tables and forms should be able to be generated dynamically from some sort of setting repository. Be it an INI file, XML File, Registry, ConfigurationManager, whatever. An XML File could have a collection of <Forms> with it's settings, custom properties, etc., and a sub-collections of <controls> with all their settings (be it custom controls or out-of-box controls) with all their event definitions (probably calling some generic sub-routine). This way you can control your own look and feel for your application throughout.

I've tried several different methods on my own, but, I've not gotten far enough to have anything worth showing. There always seems to be some road-block that kills that particular chosen path. The closest I've gotten used something like:

VB.NET
Dim aButton As New System.Windows.Forms.Button()

Me.List.Add(aButton)

HostForm.Controls.Add(aButton)

aButton.Width = 49
aButton.Top = Count * 25
aButton.Left = 100
aButton.Tag = Me.Count
aButton.Text = "Button " & Me.Count.ToString

AddHandler aButton.Click, AddressOf ClickHandler



However, one problem with this method seems to require that you have the below code in your Form.Designer.vb (which gets wiped out often):

VB.NET
btnButtonArray = New ButtonArray(Me)



Granted, that's not obtained from settings, the values are hard-coded, but,
to build a house you have to build bricks first.

But, the problem with the above is that with a dynamically created form, there is no Form.Designer.vb.

So, I'm trying to find the pieces to fit together to build my complete solution.



Here's the question:

If you know of something that already does something similar, please point me in that direction; otherwise, do any of you have suggestions that will accommodate my intentions?

Thanks in advance,
Posted
Updated 29-Dec-15 6:54am
v2
Comments
Sergey Alexandrovich Kryukov 29-Dec-15 13:10pm    
There is no such thing as "sub-form". And everything in System.Windows.Forms is dynamic. You can add/remove any control at any time, change its property anything. You did not find any article, because it does not require any article. Also, you don't need to "find" anything. All elements are on the surface.
I have no idea what is your problem.

What have you tried so far? If you didn't, yes, it could be your problem.

—SA
Truett 29-Dec-15 14:25pm    
I concede my limited experience with .Net makes it difficult to ask intelligent questions. But, I'm not as ignorant as you suggest. I create sub-forms by putting all controls of a form in a container and changing that container's parent to be a container in another form .... boom, sub-form!! So, if it's my nomenclature that you're having an issue with, I'm sorry.

The title describes my goal. And I keep hitting the wall with the methods I've tried, so I tried to give one example of an attempt I've made. I've made other attempts as well, some efforts were more ignorant attempts than others.

I don't know enough to argue with your suggestion that everything is dynamic, I only know that my efforts can't prove you to be correct. Every effort I've made to be dynamic has run into some sort of wall. Maybe you can provide an example.

Thanks for the reply.
Sergey Alexandrovich Kryukov 29-Dec-15 14:36pm    
First, if you want to talk to me, you have to comment to my post; only them I'll get a notification. I spotted your comment only because I decided to check on mine.

Second, I never suggested that you are "ignorant".

Now, I repeat: there are no "sub-forms". What you uses is just some other control (Panel, TabPage, whatever), not a form, thus not a "sub-form". I really don't see what you are missing. If you tried something, you should need to explain.

Okay, you still did not explain where you got stuck. I can only guess. So, I posted an answer based on my guess, but, due to uncertainty of your question, I cannot be 100% sure. Feel free to ask your follow-up questions.

—SA
Truett 29-Dec-15 15:44pm    
Your efforts to respond to my posts are greatly appreciated, and as such, thank you.

But, because my .Net lingo is limited, I'm obviously asking the question incorrectly.

Sub-Forms are not my issue; only a side-note trying to explain my goal of dynamically creating forms whether I'll be using it as a child form or sub-form (used for example of how I might use it, not as what it's appropriately called). The concept is that I define all the objects I want on a form in XML, ConfigurationSettings, whatever, and dynamically create the form(s) and add all the controls according to the dynamic content in the settings (file, registry, whatever is most appropriate) to build the form, containers, controls, etc., including whatever properties I wish to define for each.

Ultimately, I want to write code once, and re-use it for all future applications. I've already written a dynamic data engine to use on the forms, so I only need to have a few custom properties to add to the form and some of the controls dynamically and then point the event handlers to the appropriate sub-routines. I'm only concerned about dynamically creating the forms adding the controls and being able to reference them programmatically.

I hope that helps clarify my question somewhat.
Sergey Alexandrovich Kryukov 29-Dec-15 15:55pm    
You can do it, too. First of all, you can create classes derived from System.Windows.Form and place them in separate assemblies, such classes can be abstract, intended for begin derived in application or other assemblies. Alternative approach would be creation of flexible form classes, which can be turned into complete application-specific views by populating some container controls inside form with custom elements, and a lot more. You can encapsulate the population and/or customization methods in that form classes themselves.

You have too many possibilities to list them all. Again, your question is overly generic. You need to approach your learning from some different side. For example, you can learn the basics and ask question on particular techniques. Based on that, you can develop your own UI architecture.

If you want some help on such architecture, we need to start with something. Perhaps you can explain your ideas in detail, even formulate exact requirements.

Anyway, before we can continue, I would ask you 1) are you going to accept the existing solution formally? 2) can you explain your idea in more particular detail, so we could start with something essential? Otherwise our exchange of comments may last forever...

—SA

Sorry if this post does not answer your question; if so, this is because you did not explain your concerns. I can only guess about it.

Based on my guess: tt's possible that you did not find out how to add one control as a child of another control and how to remove. This is how:
C#
Control myParent = //... could be Panel, TabPage, custom control, anything
Button button = new Button();
// set button location, name, event handler, anything...
myParent.Controls.Add(button);
// or, alternatively:
button.Parent = myParent;

//...

myParent.Remove(button);

Please see:
Control Class (System.Windows.Forms)[^],
Control.Controls Property (System.Windows.Forms)[^],
Control.ControlCollection Class (System.Windows.Forms)[^].

This is only one aspect of "dynamic" UI, but one of the most important ones. It works on most cases, because almost all UI elements are derived from System.Windows.Forms.Control, including Form. There are some elements which are not, such as menu items, but you can always look at documentation and find similar ways to change menus, bars and other elements. Of course, there are many properties which you can change. Also, you usually need to add event handlers to the dynamically created controls, which is done via the operator '+='. This is one of the basics of .NET programming you need to know very well anyway.

Even if I did not explain something else which concerns you, you can always ask your follow-up questions.

—SA
 
Share this answer
 
Truett wrote:

Your efforts to respond to my posts are greatly appreciated, and as such, thank you.

But, because my .Net lingo is limited, I'm obviously asking the question incorrectly.

Sub-Forms are not my issue; only a side-note trying to explain my goal of dynamically creating forms whether I'll be using it as a child form or sub-form (used for example of how I might use it, not as what it's appropriately called). The concept is that I define all the objects I want on a form in XML, ConfigurationSettings, whatever, and dynamically create the form(s) and add all the controls according to the dynamic content in the settings (file, registry, whatever is most appropriate) to build the form, containers, controls, etc., including whatever properties I wish to define for each.

Ultimately, I want to write code once, and re-use it for all future applications. I've already written a dynamic data engine to use on the forms, so I only need to have a few custom properties to add to the form and some of the controls dynamically and then point the event handlers to the appropriate sub-routines. I'm only concerned about dynamically creating the forms adding the controls and being able to reference them programmatically.

I hope that helps clarify my question somewhat.
Please see my comments to the question, in particular, the one in response to your comment quoted above. Solution 1 describes the core of of the "dynamic" behavior you are looking at. You did not mention that you "want to write code once, and re-use it for all future applications". Even though your request is still quite vague, it make a big difference. Now, you are not talking about a single application which need controls dynamically set up, you are talking about a whole class of potential application you want to cover. It's not simple to predict what the applications from such class my require in future, but, first of all, you need to try to picture it. Too bad you did not mention it all in your original question. You come the the problem of developing a UI framework, and you should think about its architecture, which should, first of all, take into account your preview of the future set of application. This effort can only pay off, if you leave a good room for extension of you framework, in other words, you should not fall into the illusion that you can predict all you may require in future, and, instead, make provision into the unknown. To be successful, you should create architecture, which is not rigid, but is flexible and extendable.

For some basic ideas, please see my past answers:
How Do I Divide My Code Appropriately?[^],
DLL referencing - multiple related projects and libraries Best Practices C#[^],
Projects and DLL's: How to keep them managable?[^].

I already gave you some ideas in my comments and Solution 1. You did not ask about particular effects you want to achieve, but even what you know should be enough. After all, you are the one who is supposed to create the architecture, not me. If you have some particular concerns, I'll gladly try to advise.

—SA
 
Share this answer
 
Comments
Truett 30-Dec-15 8:23am    
Sergey,
You replied many times, and wrote many words. Obviously you're trying to help. I feel it warrants a reward for your efforts so I accepted your solution.

In truth, I still see nothing explaining how to build a form from some settings repository (as title suggests).
Sergey Alexandrovich Kryukov 30-Dec-15 11:40am    
I just though "from settings repository" is obvious. The data in repository. You read data and make data control properties. Is there anything unclear? After all, you are the one to design it all?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900