Results 1 to 9 of 9

Thread: How to convert String to Form name?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    29

    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!

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: How to convert String to Form name?

    You need to use Reflection.

    vb.net Code:
    1. Imports System.Reflection
    2.  
    3. Public Class Form2
    4.  
    5.     Private Sub Button1_Click( _
    6.         ByVal sender As System.Object, _
    7.         ByVal e As System.EventArgs _
    8.     ) Handles Button1.Click
    9.  
    10.         Dim frm As New Form
    11.         Dim formName As String = "Form1"
    12.  
    13.         formName = [Assembly].GetEntryAssembly.GetName.Name & "." & formName
    14.         frm = DirectCast([Assembly].GetEntryAssembly.CreateInstance(formName), Form)
    15.         frm.Show()
    16.     End Sub
    17.  
    18. End Class

  3. #3
    New Member
    Join Date
    May 2009
    Posts
    2

    Re: How to convert String to Form name?

    Quote Originally Posted by Deepak Sakpal View Post
    You need to use Reflection.

    vb.net Code:
    1. Imports System.Reflection
    2.  
    3. Public Class Form2
    4.  
    5.     Private Sub Button1_Click( _
    6.         ByVal sender As System.Object, _
    7.         ByVal e As System.EventArgs _
    8.     ) Handles Button1.Click
    9.  
    10.         Dim frm As New Form
    11.         Dim formName As String = "Form1"
    12.  
    13.         formName = [Assembly].GetEntryAssembly.GetName.Name & "." & formName
    14.         frm = DirectCast([Assembly].GetEntryAssembly.CreateInstance(formName), Form)
    15.         frm.Show()
    16.     End Sub
    17.  
    18. 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.

  4. #4

  5. #5
    New Member
    Join Date
    May 2009
    Posts
    2

    Re: How to convert String to Form name?

    Quote Originally Posted by Deepak Sakpal View Post
    Please post your code.
    Sory,the problem can resolved.my assembly name is wrong.

    Thanks

  6. #6
    New Member
    Join Date
    Feb 2013
    Posts
    2

    Re: How to convert String to Form name?

    It is not working properly when it is calling a form from other project. Any solutions?

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,683

    Re: How to convert String to Form name?

    Quote Originally Posted by nicedeveloper View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim FullTypeName As String = Application.ProductName & "." & TextBox1.Text
    3.         Dim FormInstanceType As Type = Type.GetType(FullTypeName, True, True)
    4.         Dim objForm As Form = CType(Activator.CreateInstance(FormInstanceType), Form)
    5.         objForm.Show()
    6. End Sub

  9. #9
    New Member
    Join Date
    Feb 2013
    Posts
    2

    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
  •  



Click Here to Expand Forum to Full Width