Marzz
Marzz

Reputation: 13

I am trying to Open a Form Using data passed from a linklabel

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?

Upvotes: 0

Views: 61

Answers (1)

Waescher
Waescher

Reputation: 5737

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:

Upvotes: 2

Related Questions