Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-23
3
Medium Priority
?
782 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 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Loops Section Overview
Suggested Courses

876 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