开发者

generic open form function

开发者 https://www.devze.com 2023-04-12 06:43 出处:网络
has anyone written a generic \"LaunchForm\" func开发者_如何学Gotion? 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

has anyone written a generic "LaunchForm" func开发者_如何学Gotion? 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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号