如何将表单传递给子表单以创建子表单? [英] How do I pass a form to a sub to create a child form out of?

查看:109
本文介绍了如何将表单传递给子表单以创建子表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个sub来创建子表单,但我遇到问题

I wanted a sub to create child forms but i am having issues

Private Sub CreateChild(frm As Form)

    Dim frmChild As New frm
    frmChild.MdiParent = My.Forms.frmNavigation
    frmChild.Show()
    frmChild.Tag = My.Forms.frmNavigation.intTabCounter
    frmChild.FormBorderStyle = FormBorderStyle.None
    frmChild.ControlBox = False
    frmChild.MaximizeBox = False
    frmChild.MinimizeBox = False
    frmChild.ShowIcon = False
    frmChild.Text = ""
    frmChild.Dock = DockStyle.Fill

End Sub





想知道是否有人可以朝着正确的方向推动我。



我觉得这是因为我正在尝试将Dim视为新的但我正在传递它的形式我想点一个新的。



我尝试过:



我想也许我可以将表单名称作为字符串传递,然后以某种方式遍历我的表单以匹配该名称,然后将Dim作为New。问题似乎是我只能通过打开表单循环。



Wondered if anyone can nudge me in the right direction.

I feel it's because i am trying to Dim as New but i am passing it the form i want to Dim a New of.

What I have tried:

I thought maybe i could pass the Forms name through as a string and then somehow loop through my forms for one matching that name, then Dim as New. Problem seems to be i can only loop through open forms.

推荐答案

尝试使用通用方法:

Try using a generic method:
Private Sub CreateChild(Of TForm As {Form, New})()
    Dim frmChild As New TForm()
    ...
End Sub



来电,您需要指定要创建的表单类型:


To call it, you need to specify the type of form you want to create:

CreateChild(Of YourCustomForm)()



类型必须从 Form 继承,并且必须有一个公共无参数构造函数。



Visual Basic中的通用类型(Visual Basic)|英特尔®开发人员专区Microsoft Docs [ ^ ]


The type must inherit from Form, and must have a public parameterless constructor.

Generic Types in Visual Basic (Visual Basic) | Microsoft Docs[^]


这篇关于如何将表单传递给子表单以创建子表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆