I have an application that I would like to use strings from the textboxes to open forms, by storing the form name in the textbox on open or close.

Dim strName as string
Dim frm as Form
If strName = String.Empty then
strName = frmMain
Else
strName = frmMain.txtFormOpen.txt
frm = strName
End If

but I get an error that a string is not or can not be converted to a form :(

Any help would be greatly appreciated. Thank you in advance

Michael

Dani AI

Generated

A couple of practical options that fit what asked for and build on what , and suggested:

  • For a small, fixed set of forms, map readable keys to constructors (the Select Case idea suggested). That is the safest and easiest to maintain.
  • For flexible runtime creation from a stored string, use reflection to find the Type by name and instantiate it. The snippet below shows a robust pattern for VB.NET 2005 that first tries Type.GetType and then scans loaded assemblies for a matching type name (full name or short name). It also checks the type is a Form before creating it.
Dim name = txtFormName.Text.Trim()
Dim formType As Type = Type.GetType(name)

If formType Is Nothing Then
  For Each asm As System.Reflection.Assembly In AppDomain.CurrentDomain.GetAssemblies()
    For Each t As Type In asm.GetTypes()
      If String.Equals(t.FullName, name, StringComparison.OrdinalIgnoreCase) OrElse _
         String.Equals(t.Name, name, StringComparison.OrdinalIgnoreCase) Then
        formType = t
        Exit For
      End If
    Next
    If formType IsNot Nothing Then Exit For
  Next
End If

If formType Is Nothing OrElse Not GetType(System.Windows.Forms.Form).IsAssignableFrom(formType) Then
  MessageBox.Show("Form not found.")
Else
  Dim frm As System.Windows.Forms.Form = CType(Activator.CreateInstance(formType), System.Windows.Forms.Form)
  frm.Show()  ' or frm.ShowDialog() depending on modality
End If

Notes and cautions: persist the form’s FullName (use currentForm.GetType().FullName) so restoration is reliable; wrap reflection in Try/Catch; avoid letting untrusted input drive arbitrary type creation — use a whitelist or a dictionary of allowed names for security and maintainability; if performance is a concern, build a lookup (name -> Type or factory delegate) once at startup instead of scanning assemblies each time.

Recommended Answers

All 7 Replies

confusing...
open form or assign name to formName?
is this code for vb 6?

The application is in VB.NET 2005.

I would like to be able to create code to open the form based on the text in the textbox.

Assign name and open the form with code.

Michael

To open a form, you need to create an instance of the form object.

Ok, thank you, but how do I do that in code please?

The application is in VB.NET 2005.

I would like to be able to create code to open the form based on the text in the textbox.

Assign name and open the form with code.

I have an application that I would like to use strings from the textboxes to open forms, by storing the form name in the textbox on open or close.

Dim strName as string
Dim frm as Form
If strName = String.Empty then
strName = frmMain
Else
strName = frmMain.txtFormOpen.txt
frm = strName
End If

but I get an error that a string is not or can not be converted to a form

Any help would be greatly appreciated. Thank you in advance

Ok, thank you, but how do I do that in code please?

Dim myForm As New Form1
myForm.ShowDialog()

you have confusing program.
iaeDave already answer the question.

Dim myForm As New Form1
myForm.ShowDialog()

that code used to call other form

I think the only way you can do it is via a select case statement and hard code your form names in there, i.e.

Select Case strName
  Case "frm1"
    dim frm as new frm1
  Case "frm1"
    dim frm as new frm2
end select
frm.showdialog()

This is not as restricting as it seems as you will have a finite number of forms at design time, you only need to remember to add them in to the select statement.

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.