将表单传递给函数 [英] Pass form to function

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

问题描述

早上好,
我正在尝试编写一个函数,可以向其传递未实例化的表格和选项卡.然后,该函数检查表单是否尚未加载,如果未加载,则将表单加载到选项卡中.
我可以使其正常工作,但这意味着为加载到MDI父级中的每种表单编写以下代码.
在MDI父级的开头,我引用了设备表格:
昏暗的frm设备作为设备
然后在检查表单是否存在后,再进行实际的实例化.这是我不知道该怎么办的地方.


Good day,
I am trying to write a function to which I can pass an un-instantiated form and a tab. The function then checks if the form is not already loaded and then if not loaded it loads the form into the tab.
I can get it to work fine but it means writing the code below for each form loaded into the MDI parent.
At the beginning of the MDI parent I have a reference to the equipment form:
Dim frmEquipment As Equipment
and then later after I check if the form exist I do the actual instantiation. This is where I do not know what to do.


;Tool strip button
    Private Sub tsbtnEquipment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnEquipment.Click
        'frmEquipment = New Equipment
        Add_FormTab(frmEquipment, CalMan.Equipment, tpEquipemnt, "Equipment")

    End Sub
<pre>
Sub Add_FormTab(ByRef FormName As Equipment, ByRef parentclass As Windows.Forms.Form, ByVal TabName As TabPage, ByVal Tabtext As String)
        If FormName Is Nothing OrElse FormName.IsDisposed Then
            'This will always be one bigger than
            Dim tabpageIndex As Integer = tcMain.TabCount
            FormName = New parentclass 'the compiler does not like this
            tcMain.Controls.Add(TabName)
            TabName.Text = Tabtext
            With FormName
                .TopLevel = False
                .MdiParent = Me
                .FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
                .WindowState = FormWindowState.Maximized
                tcMain.TabPages(tabpageIndex).Controls.Add(FormName)
                .Show()
                tcMain.SelectedTab = TabName
            End With
        Else
            tcMain.SelectedTab = TabName
        End If
    End Sub

推荐答案

您应该使用Type作为参数类型,并可以使用Activator.CreateInstance方法创建该实例的实例.请查看下面的链接以获取更多信息:

http://msdn.microsoft.com/en-us/library/d49ss92b (v = VS.71).aspx [
You should use Type as parameter type and can create an instance of that using the Activator.CreateInstance method. Have a look at this link below for more info:

http://msdn.microsoft.com/en-us/library/d49ss92b(v=VS.71).aspx[^]

Good luck!


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

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