使用 System.Type 声明条件类型的对象
我试图根据双击事件上树视图的选定节点来启动特定表单。我需要用来启动表单的代码有点庞大,因为在启动新实例之前,我必须确保表单没有被释放,并且表单尚未打开。我希望所有这些检查都发生在函数末尾的一个位置,这意味着我必须能够将正确的表单类型传递给末尾的代码。我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法
新建
类型。 Type是运行时类型信息,new需要在编译时知道类型。使用反射(Activator)或泛型。
抱歉,我不懂VB,我无法给你VB代码示例。
:c#example
时间: 2019-03-17 标签
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:
or