vb.Net - 来自配置的动态WinForms [英] vb.Net - Dynamic WinForms from Configuration

查看:77
本文介绍了vb.Net - 来自配置的动态WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于XML的文章,动态添加表单并为vb.Net动态添加控件。但是,如果有一篇关于如何在XML或其他配置设置中动态定义表单的文章,要动态创建包含所有控件和事件的表单/子表单,我还没有找到它;至少没有任何东西给我留下深刻印象,作为一个干净,高效和可重复的选项。



概念是大多数数据库应用程序中的大多数表单只不过是支持表单。主窗体中使用的配置和列表。就像书籍表和作者表一样,可能是绑定表或纸张类型 - 我知道不好的例子,但重点是所有这些子表用于管理不相关的辅助表除了用于制作主表格的列表之外的业务应用程序

可理解。



主表格将始终需要要自定义,但是,所有查找表和表单都应该能够从某种设置存储库动态生成。无论是INI文件,XML文件,注册表,ConfigurationManager等等。 XML文件可以包含< Forms>的集合。使用它的设置,自定义属性等,以及< controls>的子集合具有所有设置(包括自定义控件或开箱即用控件)及其所有事件定义(可能调用一些通用子例程)。通过这种方式,您可以在整个过程中控制自己的应用程序外观。



我自己尝试了几种不同的方法,但是,我还没有走得太远足以让任何值得展示的东西。似乎总有一些路障会杀死那条特定的路径。最接近我使用过的东西:



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:

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







但是,一个这个方法的问题似乎要求你在Form.Designer.vb中有以下代码(经常被删除):






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

btnButtonArray = New ButtonArray(Me)







当然,这不是从设置中获得的,值是硬编码的,但是,

到建造一个你必须首先建造砖头的房子。



但是,上面的问题是,使用动态创建的表格,没有Form.Designer.vb。



所以,我正试图找到合适的部分来构建我的完整解决方案。







这是问题所在:



如果您知道已经做过类似的事情,请指出我的方向;否则,你们中的任何人都有可以满足我的意图的建议吗?



提前致谢,




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,

推荐答案

对不起,如果这篇文章没有回答你的问题;如果是这样,那是因为你没有解释你的顾虑。我只能猜测它。



基于我的猜测:你可能没有找到如何添加一个控件作为另一个控件的子项以及如何去掉。具体如下:

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:
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);



请参阅:

控制类(System.Windows.Forms) [ ^ ],

Control.Controls Property(System .Windows.Forms) [ ^ ],

Control.ControlCollection类(System.Windows.Forms) [ ^ ]。



这只是动态UI的一个方面,但却是最重要的一个方面。它适用于大多数情况,因为几乎所有UI元素都派生自 System.Windows.Forms.Control ,包括表单 。有些元素不是,例如菜单项,但您可以随时查看文档并找到更改菜单,条形和其他元素的类似方法。当然,您可以更改许多属性。此外,您通常需要向动态创建的控件添加事件处理程序,这通过运算符'+ ='完成。这是.NET编程的基础之一,无论如何你都需要很好地了解。



即使我没有解释与你有关的其他内容,你总是可以问你的后续问题。



-SA


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


Truett写道:



我非常感谢您回复我的帖子,非常感谢。



但是,因为我的.Net术语有限,我显然是错误地提出这个问题。



子表格不是我的问题;试图解释我动态创建表单的目标只是侧面说明我是否将其用作子表单或子表单(例如我可能如何使用它,而不是它被恰当地称为)。我的概念是我在XML,ConfigurationSettings等表单中定义我想要的所有对象,并动态创建表单并根据设置中的动态内容添加所有控件(文件,注册表,最重要的是适当的)构建表单,容器,控件等,包括我希望为每个属性定义的任何属性。



最终,我想编写一次代码,并且将其重新用于所有未来的应用程序。我已经编写了一个动态数据引擎来在表单上使用,所以我只需要有一些自定义属性来动态添加到表单和一些控件,然后将事件处理程序指向适当的子例程。我只关心动态创建表单添加控件并能够以编程方式引用它们。



我希望这有助于澄清我的问题。


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.

请参阅我对该问题的评论,特别是回复上述评论的评论。解决方案1描述了您正在研究的动态行为的核心。您没有提到您想要编写一次代码,并将其重新用于所有未来的应用程序。即使你的要求仍然很模糊,但它会产生很大的不同。现在,您不是在谈论需要动态设置控件的单个应用程序,而是在谈论您想要涵盖的一整类潜在应用程序。预测将来需要来自这类课程的应用程序并不简单,但首先,您需要尝试对其进行描绘。太糟糕了,你没有在原来的问题中提到这一切。你遇到了开发UI 框架的问题,你应该考虑它的架构,首先应该考虑你未来的预览应用。如果你为扩展你的框架留下了良好的空间,那么这种努力只能得到回报,换句话说,你不应该陷入这样的错觉,即你可以预测你将来可能需要的所有东西,而是在未知的情况下做好准备。 。为了获得成功,你应该创建一个不僵硬但又灵活且可扩展的架构。



对于一些基本的想法,请看我过去的答案:

如何恰当地划分我的代码? [ ^ ],

DLL引用 - 多个相关项目和库最佳实践C# [ ^ ],

项目和DLL:如何保持它们的可管理性? [ ^ ]。



我已经在评论和解决方案中给了你一些想法。你没有问过你想要达到的特效,甚至是你所知道的应该够了。毕竟,你是那个应该创造建筑的人,而不是我。如果您有一些特别的顾虑,我很乐意尝试提供建议。



-SA

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


这篇关于vb.Net - 来自配置的动态WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆