-
Sep 4th, 2008, 10:51 PM
#1
Thread Starter
Junior Member
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!
-
Sep 4th, 2008, 11:20 PM
#2
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
-
May 9th, 2009, 02:19 AM
#3
New Member
Re: How to convert String to Form name?
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
Last edited by datajaya_ap; May 9th, 2009 at 02:31 AM.
-
May 10th, 2009, 10:20 PM
#4
Re: How to convert String to Form name?
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.
-
May 10th, 2009, 10:24 PM
#5
New Member
Re: How to convert String to Form name?
Originally Posted by Deepak Sakpal
Please post your code.
Sory,the problem can resolved.my assembly name is wrong.
Thanks
-
Feb 4th, 2013, 06:04 AM
#6
New Member
Re: How to convert String to Form name?
It is not working properly when it is calling a form from other project. Any solutions?
-
Feb 4th, 2013, 08:25 AM
#7
Re: How to convert String to Form name?
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.
-
Feb 4th, 2013, 09:39 AM
#8
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
-
Feb 12th, 2013, 01:35 AM
#9
New Member
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|