C# WinForm; 将一组控件传递给另一个类

发布于 2024-07-29 13:03:40 字数 352 浏览 2 评论 0原文

这是我的场景... 我有一个 winForm tabControl,每个选项卡上都有各种字段(每个选项卡都有不同的字段)。 当用户单击“生成”按钮时,应该从 tabControl 的选项卡传递控件,以便被调用的函数可以根据传递的控件信息创建电子邮件。 每个选项卡都是不同的电子邮件模板。 该方法可以从 4 个不同的按钮调用,每个按钮位于不同的 tabControl 页面上,并且每个 tabControl 具有不同的字段。

我遇到的问题是我无法找到调用/传递这些控件的好方法。 由于直到运行后才知道按下哪个 tabControl 按钮,因此我尝试创建一个抽象方法来接受这些控件,因为每个选项卡都有一组不同的控件。

有任何想法吗?

This is my scenario...
I have a winForm tabControl that has a variety of fields on each tab (each tab has different fields). When the user clicks the 'Generate' button it is then supposed to pass controls from the tabControl's tab so that the called function can create an e-mail based off of the passed control's information. Each tab is a different e-mail template. The method can be called from 4 different buttons, each button being on a different tabControl page and each tabControl having different fields.

The problem I am having is that I cannot figure out a decent way to call/pass these controls. Since it is not known what tabControl button is being pressed until after runtime I am trying to create an abstract method to accept these controls because each tab has a different set of controls.

Any ideas?

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

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

发布评论

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

评论(1

念﹏祤嫣 2024-08-05 13:03:40

正如其他人提到的,您可以作弊并尝试使用

tabControl.Controls

快速而肮脏的修复方法。

如果您想要一些未来可行的东西,并让您接触到一些更好的设计实践,我建议您选择一种替代方案。 创建 EmailUserControl UserControl 保存电子邮件的所有表单字段。 将该用户控件放置在选项卡上,而不是所有单独的字段上。 然后在用户控件上提供一个方法,生成所需的电子邮件并考虑所有字段,然后您可以传递实际的电子邮件,而不是可用于创建电子邮件的所有单独的部分和部分。

当然,该设计还可以进行其他改进,但这对您来说是一个很好的机会来尝试一些更好的东西。 当您对 UserControl 的概念越来越熟悉时,您将认识到自己改进控件设计的更多机会。 :)

As someone else mentioned, you can cheat and try using

tabControl.Controls

for a quick and dirty fix.

If you want something that will be workable going forward, as well as expose you to some better design practices, I'd suggest an alternative. Create an EmailUserControl UserControl that holds all those form's fields for the email. Place that user control on the tab instead of all the individual fields. Then provide a method on the user control that generates the desired email with all the fields taken into account, and then you can pass your actual email about instead of all the individual pieces and parts that you can use to create it.

There are additional improvements available on that design, of course, but this is a great opportunity for you to get your feet wet and try something a little bit better. As you get more comfortable with the idea of UserControls, you'll recognize additional opportunities for improving your controls' design yourself. :)

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