How to convert a string into a form object?

Hi,
Ive faced with problem where I cant manage to figure out how to convert a string into a form object. Im currently working on developing a form explorer using vb.net. My form explorer has 2 panes, left pane is a tree that shows all the form names, and right pane will be the place where the selected form will be shown. Currently, Im using hard code method to show the selected form on the right pane.
E.g. When the user select on particular node, my code behind will look like this;

Select Case Trim(SelectedNode)
Case " Registration "
Dim frmActive As FrmRegist = New FrmRegist ()
frmActive.ShowDialog()
Case "Renew"
Dim frmActive As FrmRenew = New FrmRenew ()
frmActive.ShowDialog()
.
IMHO, its not a good practice to code in this way, for me its not dynamic enough and modification on coding is needed every time a form is added in. So I think of keeping all the forms name in a table. So if any forms added in future, just need to add the info into table no need to modify the coding again.
The table format is basically like below: -

Node Caption(see by user) OriginalFormName
Registration FrmRegist
Renew FrmRenew

**Both columns is define under varchar type. (I wish to stored the OriginalFormName
column under form or object type, but those type are not supported in sql)

Here, when a user select on particular node, Ill compare the selected node name with the node caption value in the table, if match then load the form. The code is look like below:

**This is the part where problem occur.
If match then
StrOriginalFormName= Selected OriginalFormName FrmRegist
frmSelectedForm = CType(StrOriginalFormName, Form)
Dim frmActive As frmSelectedForm = New frmSelectedForm ()
frmActive.ShowDialog()
end if

The problem occurred when I try to convert a string (FrmRegist) into Form type.
The message shown:- Value of type 'String' cannot be converted to 'System.Windows.Forms.Form'.

May I know how to solve this problem? Or is there any other way to fulfill my concept? Thanx in advance.

Comments

  • : Hi,
    : Ive faced with problem where I cant manage to figure out how to convert a string into a form object. Im currently working on developing a form explorer using vb.net. My form explorer has 2 panes, left pane is a tree that shows all the form names, and right pane will be the place where the selected form will be shown. Currently, Im using hard code method to show the selected form on the right pane.
    : E.g. When the user select on particular node, my code behind will look like this;
    :
    : Select Case Trim(SelectedNode)
    : Case " Registration "
    : Dim frmActive As FrmRegist = New FrmRegist ()
    : frmActive.ShowDialog()
    : Case "Renew"
    : Dim frmActive As FrmRenew = New FrmRenew ()
    : frmActive.ShowDialog()
    : .
    : IMHO, its not a good practice to code in this way, for me its not dynamic enough and modification on coding is needed every time a form is added in. So I think of keeping all the forms name in a table. So if any forms added in future, just need to add the info into table no need to modify the coding again.
    : The table format is basically like below: -
    :
    : Node Caption(see by user) OriginalFormName
    : Registration FrmRegist
    : Renew FrmRenew
    :
    : **Both columns is define under varchar type. (I wish to stored the OriginalFormName
    : column under form or object type, but those type are not supported in sql)
    :
    : Here, when a user select on particular node, Ill compare the selected node name with the node caption value in the table, if match then load the form. The code is look like below:
    :
    : **This is the part where problem occur.
    : If match then
    : StrOriginalFormName= Selected OriginalFormName FrmRegist
    : frmSelectedForm = CType(StrOriginalFormName, Form)
    : Dim frmActive As frmSelectedForm = New frmSelectedForm ()
    : frmActive.ShowDialog()
    : end if
    :
    : The problem occurred when I try to convert a string (FrmRegist) into Form type.
    : The message shown:- Value of type 'String' cannot be converted to 'System.Windows.Forms.Form'.
    :
    : May I know how to solve this problem? Or is there any other way to fulfill my concept? Thanx in advance.
    :
    :

    Try this function ...

    Imports System.Reflection
    ...

    Function InstantiateForm( _
    ByVal currentProjectName As String, _
    ByVal strFormClassName As String) As Form

    Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
    Return CType( _
    asm.CreateInstance( _
    currentProjectName & "." & strFormClassName, True),Form)

    End Function

    I broken up some lines of code so that it would fit in the function return statement. Anyways, the first argument should be the name of the current VB project and the second argument should be the name of the form class inside the project that you want to instantiate.

    Basically the function loads the current assembly and uses the CreateInstance method of the assembly object to load an object type based on a string parameter. The type parameter is in the string format of Project.ObjectName. Notice that the functions return type is of a generic Form so that any form class can be instantiated and returned.

    Let me know if this helps you.



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion