C# WinForm; 将一组控件传递给另一个类
这是我的场景... 我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如其他人提到的,您可以作弊并尝试使用
快速而肮脏的修复方法。
如果您想要一些未来可行的东西,并让您接触到一些更好的设计实践,我建议您选择一种替代方案。 创建
EmailUserControl
UserControl
保存电子邮件的所有表单字段。 将该用户控件放置在选项卡上,而不是所有单独的字段上。 然后在用户控件上提供一个方法,生成所需的电子邮件并考虑所有字段,然后您可以传递实际的电子邮件,而不是可用于创建电子邮件的所有单独的部分和部分。当然,该设计还可以进行其他改进,但这对您来说是一个很好的机会来尝试一些更好的东西。 当您对
UserControl
的概念越来越熟悉时,您将认识到自己改进控件设计的更多机会。 :)As someone else mentioned, you can cheat and try using
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
UserControl
s, you'll recognize additional opportunities for improving your controls' design yourself. :)