none
Open form that has a custom property by the form name/string RRS feed

  • Question

  • Hi,

    I use the following code to open a form by it's name but need to modify it so that I can also reference a custom property in the form.  

    Public Sub OpenForm(strFormName As String) 'check form is already open and activate if so If FindMdiChild(strFormName) = False Then Dim asm = System.Reflection.Assembly.GetExecutingAssembly Dim myTypes As Type() = asm.GetTypes() Dim frm As Form 'find form and open For Each t As Type In myTypes If t.IsSubclassOf(GetType(System.Windows.Forms.Form)) AndAlso t.Name = strFormName Then frm = CType(Activator.CreateInstance(t), Form) frm.MdiParent = MainForm 'set custom property here

    frm.ReadOnlyData = true

    frm.Show() End If Next End If

    When I try to set the property ReadOnlyData to true, I get the following error in the IDE ReadOnlyData' is not a member of 'System.Windows.Forms.Form' in the debugger

    The message makes perfect sense because ReadOnlyData Isn't a Member of the generic form type however, I don't know how to dynamically reference the form type

    Can anyone help with this?

    Thursday, November 30, 2017 8:46 PM

Answers

  • Try this:

       Dim p = frm.GetType().GetProperty("ReadOnlyData")

       If p IsNot Nothing Then

          p.SetValue(frm, True)

       End If

    • Marked as answer by Phill D Friday, December 1, 2017 2:22 PM
    Friday, December 1, 2017 9:16 AM

All replies

  • Hi Phill,

    >>ReadOnlyData' is not a member of 'System.Windows.Forms.Form'

    Sure you can not do it, the Form does not have a property named ReadOnlyData by default.

    >>how to dynamically reference the form type

    Sorry that I can not understand what you mean about this purpose, could you please provide more details for it? What do you mean about "dynamically reference the form type"?

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 1, 2017 6:30 AM
    Moderator
  • Ok,

    I added the ReadOnlyData property to a form frmUsers when I call the OpenForm sub, I pass in the string "frmUsders" to the variable strFormName

    If I could adapt this line of code

    Dim frm As Form
     to something like
    Dim frm as strFormName
    my problem would be solved but, obviously, that doesn't work. So what I meant by how can I dynamically reference the form? ids how can I dynamically make frm = the correct form (passed in by using the string strFormName? 


    Friday, December 1, 2017 7:15 AM
  • Try this:

       Dim p = frm.GetType().GetProperty("ReadOnlyData")

       If p IsNot Nothing Then

          p.SetValue(frm, True)

       End If

    • Marked as answer by Phill D Friday, December 1, 2017 2:22 PM
    Friday, December 1, 2017 9:16 AM
  • oh wow, that worked thanks for helping, I had no idea you could set a property that way.

     
    Friday, December 1, 2017 2:22 PM