How to convert String to Form name?
I have a table that contains a list of users and their access to certain forms.
Code:
User Form
Tom Form1
Jane Form1
Jane Form2
Peter Form3
This table is read when the application first loads. How can I convert the form string (Form1, Form2, Form3...) to a form name where I can do something like:
Form1.show
Form2.show
Thanks a lot!
Re: How to convert String to Form name?
You need to use Reflection.
vb.net Code:
Imports System.Reflection
Public Class Form2
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Button1.Click
Dim frm As New Form
Dim formName As String = "Form1"
formName = [Assembly].GetEntryAssembly.GetName.Name & "." & formName
frm = DirectCast([Assembly].GetEntryAssembly.CreateInstance(formName), Form)
frm.Show()
End Sub
End Class
Re: How to convert String to Form name?
Quote:
Originally Posted by
Deepak Sakpal
You need to use Reflection.
vb.net Code:
Imports System.Reflection
Public Class Form2
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Button1.Click
Dim frm As New Form
Dim formName As String = "Form1"
formName = [Assembly].GetEntryAssembly.GetName.Name & "." & formName
frm = DirectCast([Assembly].GetEntryAssembly.CreateInstance(formName), Form)
frm.Show()
End Sub
End Class
Hello,
I try your code,but not working,the error is can't create Instance.do you have solution for me?
Thanks
Re: How to convert String to Form name?
Quote:
Originally Posted by
datajaya_ap
Hello,
I try your code,but not working,the error is can't create Instance.do you have solution for me?
Thanks
Please post your code.
Re: How to convert String to Form name?
Quote:
Originally Posted by
Deepak Sakpal
Please post your code.
Sory,the problem can resolved.my assembly name is wrong.
Thanks
Re: How to convert String to Form name?
It is not working properly when it is calling a form from other project. Any solutions?
Re: How to convert String to Form name?
Quote:
Originally Posted by
nicedeveloper
It is not working properly when it is calling a form from other project. Any solutions?
This thread is 5 years old. If you have a problem then you should make a new thread and explain your problem carefully so that we may help you.
Re: How to convert String to Form name?
I think he wants to do what the thread title is asking.
In which case, you have to get the right namespace and then create an instance of the name, then convert it to a form object.
This assumes the name of the form is in a textbox, but you could also use a listbox, or a combobox or anything else capable of storing a string.
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FullTypeName As String = Application.ProductName & "." & TextBox1.Text
Dim FormInstanceType As Type = Type.GetType(FullTypeName, True, True)
Dim objForm As Form = CType(Activator.CreateInstance(FormInstanceType), Form)
objForm.Show()
End Sub
Re: How to convert String to Form name?
Dear Hack,
You understood what I was trying to do.
However, this only works when you are openning a form within a same project (since you are using Application.ProductName).
So I tried to use actual project name instead (perhaps "nCDS")
My code looks like this
sFormFullName = "n" & sProdType & "." & sFormName
Dim FormInstanceType As Type = Type.GetType(sFormFullName, True, True)
FRM = CType(Activator.CreateInstance(FormInstanceType), Form)
FRM.MdiParent = Me
FRM.Show()
I don't think my code is actually getting "FormInstanceType ".
Would you help?