Click here to Skip to main content
15,868,142 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Sharing Values Between Two Forms in an MDI Application

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
17 Aug 2011CPOL1 min read 12.4K  
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)


Written By
Software Developer
South Africa South Africa
My IT Career began in 1996 as I was studying to become a Chartered Accountant.

Between 1998 to Date I have worked as a Computer Technician, IT Manager, Tutor and to my present position of Developer in a company I co-own with a colleague based in Johannesburg South Africa.

We do Web Hosting, design and development, and have only recently added Dot.Net Enterprise Application development to the business portfolio of Services.

Comments and Discussions

 
-- There are no messages in this forum --