Solved

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

Posted on 2008-06-23
3
760 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
ororiole earned 500 total points
ID: 21850579
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
ID: 21857281
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
ID: 31469853
It's working!  Thanks for your help!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 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