通用开放式函数

发布于 2024-12-09 13:53:57 字数 137 浏览 0 评论 0原文

有人写过通用的​​“LaunchForm”函数吗?对于我打开表单的所有菜单项,我想编写一个函数来启动表单,而不是多次编写相同的代码。

任何提示都会非常有帮助。

这是 winforms + vb.net

谢谢 TR

has anyone written a generic "LaunchForm" function? For all the menu items I have that open a form, I would like to write one function that will launch the form as opposed to writing the same code several times.

any tips would be very helpful.

It's winforms + vb.net

thanks
TR

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

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

发布评论

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

评论(2

不爱素颜 2024-12-16 13:53:57

你的意思是这样的吗?

C#

public F Launch<F>() where F : Form, new()
{
    F dlg = new F();
    dlg.MdiParent = this;
    dlg.Show();
    return dlg;
}

VB.NET

Public Function Launch(Of F As {Form, New})() As F
    Dim dlg As New F()
    dlg.MdiParent = Me
    dlg.Show()
    Return dlg
End Function

You mean something like this?

C#

public F Launch<F>() where F : Form, new()
{
    F dlg = new F();
    dlg.MdiParent = this;
    dlg.Show();
    return dlg;
}

VB.NET

Public Function Launch(Of F As {Form, New})() As F
    Dim dlg As New F()
    dlg.MdiParent = Me
    dlg.Show()
    Return dlg
End Function
一个人的旅程 2024-12-16 13:53:57

您可以将表单类型附加到菜单项的 .Tag 属性。将单个事件处理程序连接到所有菜单项单击事件,并将 .Tag 属性值(表单类型)传递给创建新实例并显示它的函数。

或者,如果每个表单都是单例,您可以创建一个(MenuItem,Form)字典,预填充表单实例并进行适当的查找/显示。或者甚至跳过字典并弹出表单实例到菜单项的 .Tag 属性中。

有各种各样的选择,在不知道预期用途的情况下,很难只推荐一种。

You could attach the form type to the menuitem's .Tag property. Hook up a single event handler to all the menu item click events and pass the .Tag property value (form type) to a function that created a new instance and displayed it.

Alternately, if each form is to be a singleton you could create a dictionary of(MenuItem, Form), prepopulate with form instances and do the appropriate lookup/show. Or even skip the dictionary and pop and instance of the form into the menuitem's .Tag property.

There's all kinds of options and without knowing the intended usage it's hard to suggest just one.

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