如何以dinamically方式创建表单实例? [英] How to create an instance of a form dinamically?

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

问题描述

亲爱的所有

我有一个项目在设计时创建了所有表单,并将其名称保存在数据库中,现在当应用程序运行时,它会从数据库和存储中加载所有表单名称数组中的每一个作为字符串。

现在我将表单名称作为字符串,我想通过运行时名称创建表单。你可以帮我吗?



ex:



Dear all
I have a project that created all forms at design time, and saved its names in database, now when the application is running, it loads the all forms names from the database and store each one in array as a string.
Now I have the form name as a string, I want to create the form by the name which I have at run time. Can you help me please?

ex:

Dim strFormName As String = "frmMyForm"

推荐答案

尝试使用以下方法



try with below method

Public Function TryGetFormByName(frmname As String) As Form
    Dim formType = Assembly.GetExecutingAssembly().GetTypes().Where(Function(a) a.BaseType = GetType(Form) AndAlso a.Name = frmname).FirstOrDefault()

    If formType Is Nothing Then
        Return Nothing
    End If

    Return DirectCast(Activator.CreateInstance(formType), Form)
End Function



参考: Winforms,按表单名称获取表单实例 [ ^ ]


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

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