0

I created a link label with the lnk.name = ItemCard

I have a Form called frmItemCard

When you click the linklabel I want it to open the form programmatically.

I am doing this because I am generating the linklabels from a list in an SQL table.

The Code I am using to Open the form is:

Private Sub lnk_LinkClicked(ByVal sender As System.Object, ByVal e As LinkLabelLinkClickedEventArgs)

    Dim lnk As LinkLabel = CType(sender, LinkLabel)
    Try
        Dim vForm As String
        vForm = "frm" + lnk.Name
        Call showFormDynamically(vForm)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try


End Sub

Public Sub showFormDynamically(frmForm As String)
    Dim obj As Object = Activator.CreateInstance(Type.GetType(frmForm))
    obj.MdiParent = ParentForm
    obj.Dock = DockStyle.Fill
    obj.show()
End Sub

The Error I get is: Value cannot be Null. Parameter name: type

Any Ideas what I am doing wrong?

Marzz
  • 13
  • 3
  • vForm = "frm" + lnk.Name you need it with a namespace – Gosha_Fighten Jan 26 '16 at 18:33
  • I don't know what that means. I have no formal training in Visual Basic. Just what I have taught my self. – Marzz Jan 26 '16 at 18:43
  • 1
    Since you've just taught yourself, and are using an advanced topic like Reflection, would you mind explaining to us at a higher level what you want your app to do? Chances are that there is a better way to achieve what you are trying to accomplish. – Jeremy Jan 26 '16 at 19:03
  • Clearly `Type.GetType(frmForm)` returns `null`. The reason it returns `null` is you need the fully-qualified type name (the name must include the full namespace path). Also, the `Call` keyword is a relic of the old VB6 era. There's no good reason to ever use it with modern VB.Net code. – Joel Coehoorn Jan 26 '16 at 19:45
  • My goal is to store all the form names in an SQL database. Then the main menu will create link labels with the name of the link label as the form name. then when you click on the link label it will open the form. I was able to get the forms to open (I was missing the assemblyname) how ever getting them to open in the parent container is my next issue. – Marzz Jan 26 '16 at 19:47

1 Answers1

2

The thing is, that Type.GetType() will return null if it cannot resolve the type at all.

So you have no type to create an instance from, now if you call Activator.CreateInstance(null) the exception is thrown, because that method does not allow the argument passed in to be null.

This has nothing to do with VB.NET at all, that's just how the .NET framework operates. Anyways, try it: call Type.GetType("anything") it will just return null.

So I'd say your type name is just wrong. It seems that you just use something like "frmItemCard" but you need to pass the full qualified one which could probably look like: "AnyApplication.AnyNamespace.frmItemCard".

There are several ways to find your type name. You might start here:

Community
  • 1
  • 1
Waescher
  • 4,506
  • 3
  • 27
  • 42
  • I made this change which opens the form now. Dim AssemblyName As String = [Assembly].GetEntryAssembly().GetName.Name() Dim obj As Object = Activator.CreateInstance(Type.GetType(AssemblyName + "." + frmForm)) How ever now I am having trouble with loading the form in the parent container. – Marzz Jan 26 '16 at 19:43
  • 1
    This requires that the form is placed inside of the entry assembly. By the way, if you are in the same project/assembly, you could also put the type of the form into the linklabel's `Tag` property, so you won't have to deal with the names ... just an idea ... – Waescher Jan 26 '16 at 19:45
  • I thought this obj.MdiParent = ParentForm would place it inside the ParentForm. – Marzz Jan 26 '16 at 19:50
  • Sure. If the parent is an MDIContainer (see `IsMdiContainer`) – Waescher Jan 26 '16 at 19:52
  • It is I already have the Main Menu opening in the MDI Container using this code: Dim frmMainMenu1 As New frmMainMenu() frmMainMenu1.MdiParent = Me frmMainMenu1.Dock = DockStyle.Fill frmMainMenu1.Show() – Marzz Jan 26 '16 at 19:54
  • If I comment out the obj.MdiParent = ParentForm the form will open normally. How ever if I use obj.MdiParent = ParentForm (ParentForm is the name of the mdicontainer form) then nothing happens. – Marzz Jan 26 '16 at 19:55
  • Is frmMainMenu1 set to be a MdiContainer? – Waescher Jan 26 '16 at 19:55
  • No ParentForm is the only mdicontainer – Marzz Jan 26 '16 at 19:56
  • Hard to discuss in the comments. Why don't you just set up another question where you can put in additional information? – Waescher Jan 26 '16 at 20:00
  • 1
    I figured it out. Thank you for your help Waescher. – Marzz Jan 26 '16 at 20:11