使用 System.Type 声明条件类型的对象

发布于 2024-09-04 19:07:07 字数 1106 浏览 1 评论 0原文

我试图根据双击事件上树视图的选定节点来启动特定表单。我需要用来启动表单的代码有点庞大,因为在启动新实例之前,我必须确保表单没有被释放,并且表单尚未打开。我希望所有这些检查都发生在函数末尾的一个位置,这意味着我必须能够将正确的表单类型传递给末尾的代码。我正在尝试使用 System.Type 来执行此操作,但这似乎不起作用。有人能给我指出正确的方向吗?

    With TreeView.SelectedNode
        Dim formType As Type
        Select Case .Text
            Case "Email to VPs"
                formType = EmailForm.GetType()
            Case "Revise Replacers"
                formType = DedicatedReplacerForm.GetType()
            Case "Start Email"
                formType = EmailForm.GetType()
        End Select
        Dim form As formType
         Dim form As formType
        Try
            form = CType(.Tag, formType)
            If Not form.IsDisposed Then
                form.Activate()
                Exit Sub
            End If
        Catch ex As NullReferenceException
            'This will error out the first time it is run as the form has not yet 
            ' been defined.
        End Try
        form = New formType
        form.MdiParent = Me
        .Tag = form
        CType(TreeView.SelectedNode.Tag, Form).Show()
    End With

I am attempting to launch a specific form depending on the selected node of a treeview on the doubleclick event. The code I need to use to launch the form is a little bulky becuase I have to ensure that the form is not disposed, and that the form is not already open, before launching a new instance. I'd like to have all of this checking happen in one place at the end of the function, which means that I have to be able to pass the right form type to the code at the end. I'm trying to do this with a System.Type, but that doesn't seem to be working. Could someone point me in the right direction, please?

    With TreeView.SelectedNode
        Dim formType As Type
        Select Case .Text
            Case "Email to VPs"
                formType = EmailForm.GetType()
            Case "Revise Replacers"
                formType = DedicatedReplacerForm.GetType()
            Case "Start Email"
                formType = EmailForm.GetType()
        End Select
        Dim form As formType
         Dim form As formType
        Try
            form = CType(.Tag, formType)
            If Not form.IsDisposed Then
                form.Activate()
                Exit Sub
            End If
        Catch ex As NullReferenceException
            'This will error out the first time it is run as the form has not yet 
            ' been defined.
        End Try
        form = New formType
        form.MdiParent = Me
        .Tag = form
        CType(TreeView.SelectedNode.Tag, Form).Show()
    End With

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

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

发布评论

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

评论(1

幽梦紫曦~ 2024-09-11 19:07:07

您无法新建类型。 Type是运行时类型信息,new需要在编译时知道类型。

使用反射(Activator)或泛型。

抱歉,我不懂VB,我无法给你VB代码示例。

:c#example

T CreateForm<T>() where T : Form, new()
{
  return new T();
}

时间: 2019-03-17 标签

Form CreateForm(Type t)
{
  return (Form)Activator.CreateInstance(t);
}

You can't new a Type. The Type is a runtime-type information, new needs to know the type at compile time.

Use either reflection (Activator) or generics.

Sorry I don't know VB, I can't give you a code example in VB.

c# example:

T CreateForm<T>() where T : Form, new()
{
  return new T();
}

or

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