动态或在编译时设计 VB.NET UI

发布于 2024-12-18 19:46:05 字数 371 浏览 0 评论 0原文

我现在正在设计一个程序,遇到了 UI 问题。我想要有一堆不同的屏幕,但我不想使用 20 种不同的形式(我很确定这无论如何都是不好的做法),而且我不想立即放下所有控件设计师,因为这样事情就会变得混乱。我有一个想法,在启动时运行一个子程序,它将动态生成我需要的所有控件以及所有正确的坐标和设置,然后将特定屏幕所需的所有控件转储到面板中。然后,我返回面板的 ArrayList,并在需要该屏幕时拉出正确的面板以附加到表单。唯一的问题是我的表单不会对其控件有任何隐式了解,因此我假设我无法使用 WithEvents / Handles.* 关键字在表单类中声明处理程序(这就是我的内容) d 宁愿这样做)。我知道这听起来有点复杂;我仍在试图理解我在说什么:)如果有人对哪种方法更好有任何想法,或者如果有我没有遇到的更好的方法,请告诉我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鱼忆七猫命九 2024-12-25 19:46:05

您谈论的是设计师(静态)程序化(动态) UI 生成。这是一种不太受欢迎的圣战,因为双方都有粉丝。您可以将事件等与这两种方法一起使用(因为设计器最终实际上生成代码)。由您决定。程序化方法有一些好处,但通常需要更多时间(尤其是第一次)。

Charles Petzold 是众所周知的程序化 UI 创建爱好者,您可以从这本书中学习一些技巧例如(但恕我直言,总的来说,关于 C# 的书很糟糕)。

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

冬天的雪花 2024-12-25 19:46:05

我认为答案是两者都做一点。拥有 20 个表单是维护的噩梦,动态生成所有内容也同样糟糕。

尝试找出您的 20 个设计中的共同特征。它们都使用 3 列布局吗?他们都使用一组通用的控件吗?等等。

然后创建那些公共元素。对于布局,在表单上放置一些容器控件,并按照您想要的方式调整它们的大小。如果可以的话,创建一些自定义控件来显示或处理常见的数据项。

然后,您可以动态地将自定义控件添加到布局中。

事件处理实际上是最简单的部分。您可以编写一个事件处理过程,然后使用AddHandler 将事件从动态创建的控件链接到预先编写的代码。

AddHandler

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

凝望流年 2024-12-25 19:46:05

我只会选择 20 种不同的形式。不同的屏幕,不同的形式,没有什么问题。

它为您提供了所有设计时的好处,并避免了在代码中对齐和安排所有内容的麻烦,这将是绝对的维护噩梦。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文