Results 1 to 4 of 4

Thread: Run a Form with a String Variable

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2003
    Location
    Amsterdam, Holland
    Posts
    36

    Question Run a Form with a String Variable

    Hi All,

    I have multiple forms created and my question is if it is possible to show a from via a string variable, something like this.

    Normal:
    Dim frmtest As New frmRegistry
    frmtest.ShowDialog()

    Something like this:
    dim runform as string = "frmRegistry"
    Dim frmtest As New runform
    frmtest.ShowDialog()

    Or should I do this on another way.

    Thanks,
    AXH

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yes you can use the Activator.Createinstance method to create objects from strings, but the string has to be the full namespace.
    VB Code:
    1. 'if you have option strict on then you may have to declare it as an object instead of form
    2. Dim frm As Form=Activator.CreateInstance("MyNamespace.frmregistry")
    3. frm.ShowDialog

    Here is an example from MSDN: http://msdn.microsoft.com/library/de...et10082002.asp

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2003
    Location
    Amsterdam, Holland
    Posts
    36
    This is the error message:

    Value of type 'String' cannot be converted to 'System.Type'.

    With the following code:

    Dim frmname As String = "frmRegistry"
    Dim frm As Form = Activator.CreateInstance(frmname)
    frm.ShowDialog()

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    As I said the string must be the full name of the object including namespace. Try reading the MSDN article for more information.

    Your project automatically has at least a root namespace so 'frmRegistry' is not the full name of the form.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width