hello there,

anyone can help...
is this possible?
i have a string value of form names in my DB so when my condition passes, the variable string will take over the value , then that value will call the form(variable) as string.
i got an error when i use this code.

dim objForm as form
dim sValue as string

sValue="frmXXX"

set objForm=sValue
objForm.show

This is what the code looks like... i need to convert the variable string to form object

thanks a lot.

Dani AI

Generated

is on the right track with reflection: you need the form’s type, then create an instance. Two gotchas to make this robust: (1) store a fully qualified type name in the DB (for example, MyApp.Forms.Form2, MyApp) so renames/namespaces do not break lookups, and (2) reuse an existing instance so it only opens once, which is what and were aiming for.

Here is a compact utility that resolves a type name and shows a single instance of that form. If it is already open, it restores and activates it instead of creating a new one.

Public Sub ShowFormOnce(formTypeName As String)
    ' Expect "Namespace.FormName" or "Namespace.FormName, AssemblyName"
    Dim t As Type = Type.GetType(formTypeName, throwOnError:=False, ignoreCase:=True)
    If t Is Nothing Then
        ' Fallback to current assembly if only a short/namespace name was stored
        Dim asm = Reflection.Assembly.GetExecutingAssembly()
        t = asm.GetType(formTypeName, throwOnError:=False, ignoreCase:=True)
    End If
    If t Is Nothing OrElse Not GetType(System.Windows.Forms.Form).IsAssignableFrom(t) Then
        Throw New ArgumentException("Form type not found or is not a Form: " & formTypeName)
    End If

    Dim existing As System.Windows.Forms.Form = Nothing
    For Each f As System.Windows.Forms.Form In System.Windows.Forms.Application.OpenForms
        If f.GetType() Is t Then existing = f : Exit For
    Next

    If existing IsNot Nothing Then
        If existing.WindowState = FormWindowState.Minimized Then existing.WindowState = FormWindowState.Normal
        existing.BringToFront()
        existing.Activate()
    Else
        Dim f = DirectCast(Activator.CreateInstance(t), System.Windows.Forms.Form)
        AddHandler f.FormClosed, Sub() f.Dispose()
        f.Show()
    End If
End Sub

Notes:

  • The string in your DB should be the class name, not the instance Form.Name.
  • If your forms live in another assembly, store the assembly-qualified name to avoid reflection misses.
  • If this is actually ASP.NET (tag suggests it), this pattern will not work; web apps cannot Show Windows Forms.

Recommended Answers

All 4 Replies

hello there,

anyone can help...
is this possible?
i have a string value of form names in my DB so when my condition passes, the variable string will take over the value , then that value will call the form(variable) as string.
i got an error when i use this code.

dim objForm as form
dim sValue as string

sValue="frmXXX"

set objForm=sValue
objForm.show

This is what the code looks like... i need to convert the variable string to form object

thanks a lot.

Try using "New"

Dim objForm As New Form

You have to get the qualified type name first i.e. get the right namespace. Then you can create an instance of that name and convert it to a form (since you know it's a form)

Dim objForm As Form
Dim sValue As String
Dim FullTypeName As String
Dim FormInstanceType As Type

' Form class name
sValue = "Form2"

' Assume that form classes' namespace is the same as ProductName
FullTypeName = Application.ProductName & "." & sValue
' Now, get the actual type
FormInstanceType = Type.GetType(FullTypeName, True, True)
' Create an instance of this form type
objForm = CType(Activator.CreateInstance(FormInstanceType), Form)
' Show the form instance
objForm.Show()

If you have your forms in some other namespace, use

FullTypeName = Application.ProductName & "." & "MyNamespace" & "." & sValue

this code is working for me. but i want to open the form once. it seems that this code will open another new form. how this will be used to open a form once.

thanks

It open a new form because everytime you call this code and you don't save any handle, if you save the handles you can call them everytime you need it without create a new istance (like you are doing now)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.