Instanciate a Form from a database Field

  • Thread starter Thread starter Al Reid
  • Start date Start date
A

Al Reid

I'm working on a simple workflow system. I need to show a form when the workflow step needs user input. The form name is stored in
a database table. Is there a way to do this? I can't seem to find the right combination.
 
Al Reid said:
I'm working on a simple workflow system. I need to show a form when the workflow step needs user input. The form
name is stored in
a database table. Is there a way to do this? I can't seem to find the right combination.

Public Sub ShowDialogByName(ByRef FormName As String)
Dim f As Windows.Forms.Form
Select Case FormName
Case "Form1"
f = New Form1
Case "Form2"
f = New Form2
Case Else
f = Nothing
End Select
If Not f Is Nothing Then
f.ShowDialog()
End If
End Sub
 
Thanks, but there has to be a better way and I'm sure there is. I believe
that one has to use reflection, but I haven't quite figures it out. I guess
I'll spend a little more time refining my Google search.
 
Dear Al Reid,

I am not sure if I understand what you mean, but if I do then I would
recommend doing the following:
Create a hashtable with the key being the a string with the form name and
the value the form object. You should limit
form creation to the object containing this hashtable in order to have all
of your forms contained by the hashtable (if this is wanted, that is).

Hope I helped you,

Michel van den Berg
 
Thanks. Essentially what I was after was a way to show a form from a string containing the name of the form.

After refining my Google search I came across a way to do it using Reflection that seems to be simple and efficient AND it works.
Here is what I found:

Imports System
Imports System.Windows.Forms
Imports System.Reflection

Public Class ObjectFinder
Public Shared Function CreateObjectInstance(ByVal objectName As String) As Object
' Creates and returns an instance of any object in the assembly by its type name.

Dim obj As Object

Try
If objectName.LastIndexOf(".") = -1 Then
'Appends the root namespace if not specified.
objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
End If

obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)

Catch ex As Exception
obj = Nothing
End Try
Return obj

End Function

Public Shared Function CreateForm(ByVal formName As String) As Form
' Return the instance of the form by specifying its name.
Return DirectCast(CreateObjectInstance(formName), Form)
End Function

End Class



Again, it's not my code, but it works and does exactly what I was looking for.


--
Al Reid

Michel van den Berg said:
Dear Al Reid,

I am not sure if I understand what you mean, but if I do then I would
recommend doing the following:
Create a hashtable with the key being the a string with the form name and
the value the form object. You should limit
form creation to the object containing this hashtable in order to have all
of your forms contained by the hashtable (if this is wanted, that is).

Hope I helped you,

Michel van den Berg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top