Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
771 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 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

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

722 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