Solved

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

Posted on 2008-06-23
3
754 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…

738 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