Solved

How do I pass in variable and use it to open form in VB.NET application?

Posted on 2008-06-23
3
713 Views
Last Modified: 2013-11-26
I have a VB.NET 2.0 WinForms application.  I have a procedure called LoadForm that allows me to pass in the parent form, child form name, and form caption and it opens up the child form within an MDI Container.

    Private Sub LoadForm(ByVal objParentForm As Object, ByVal strChildForm As String, ByVal strCaption As String)

        Dim typChildForm As Type = Type.GetType(strChildForm)
        Dim frmChildForm As Form = CType(Activator.CreateInstance(typChildForm), Form)

        frmChildForm.Text = strCaption
        frmChildForm.MdiParent = CType(objParentForm, Form)
        frmChildForm.WindowState = FormWindowState.Maximized
        frmChildForm.Show()

    End Sub

I would like to be able to pass in a primary key and open the form with the appropriate record.  Does anyone have an idea how I can update my procedure to allow me to pass in a primary key value and then use it on the form_load event to populate the form?

Thanks,
Sarah
0
Comment
Question by:spendergrass
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
ororiole earned 500 total points
Comment Utility
Why are you creating a typChildForm and then casting it to a Form. Isnt it already a Form of type typChildForm?

Anyway define a form that takes a parameter in its constructor. This parameter is the primary key. Like so:
Public Class MyForm
       Inherits Form
  Private pk As Integer
 
  Public  Sub New(ByVal pk As Integer)
    Me.pk = pk
  End Sub
End Class

then use this Activator:
Dim type As Type
Dim args As Object()
Dim returnValue As Object

returnValue = Activator.CreateInstance(type, args)
so you can pass the pk into the constructor.

Then when Form_Load occurs use the field pk.
0
 

Author Comment

by:spendergrass
Comment Utility
If I don't cast the typChildForm to a Form I get the error: Option Strict On disallows implicit conversion from 'Object' to 'System.Windows.Forms.Form'.
0
 

Author Closing Comment

by:spendergrass
Comment Utility
It's working!  Thanks for your help!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now