Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Sharing Values Between Two Forms in an MDI Application

0.00/5 (No votes)
17 Aug 2011CPOL1 min read 12.5K  
We usually build MDI Applications with several forms. Some of the forms we require them to display certain filtered data from the results of a query. We might be interested in pulling data from one of the rows on the filtered form to be displayed on some controls from the calling form.

Scenario:

I have an MDI parent form with two child forms. In VB.NET the two forms do not know each other at runtime therefore they can share values with each other by routing them via the parent form (i.e. the MDI form) I have tried this method and believe you me when you have say ten forms sharing data in some way at runtime having them share data or values becomes a headspinner.

Lets call Our Child Forms ChildA and ChildB.

ChildA opens a new instance of ChildB, and ChildB displays a list of values let's says Products by ManufacturerB, ChildA is interested in one Product's data from the list displayed by ChildB.

To be able to know ChildB, in VB.NET we will have ChildA declare an instance of ChildB as follows:

VB
Public Class ChildA
WithEvents newChildB as New ChildB
End Class


Now this will expose ChildB's events to ChildA

Suppose ChildA has a textbox control called txtProductID and they wish to get the productID from the datagrid control on ChildB called dgvProducts and the productID is displayed in Column with index 0 in the gridview

ChildA will then require to have the following code:

VB
Public Class ChildA
WithEvents newChildB as New ChildB
.
.
.
.
Private Sub newChildB_ShareData(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs)Handles newChildB.FormClosing
txtProducts.Text = NewChildB.dgvProducts.CurrentRow.Cells(0).Value.ToString
End Sub


At runtime the user has to select the row with the product of interest and click the OK button which then closes the form and viola the ProductID is displayed on ChildA's txtProductID

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)