VB.Net. Transfer data between two mdi child forms

Hi Experts.    I have an MDI App with a number different child forms.   Two of the child forms I have allowed only one instance of each.  The first child form receives data on a continuous basis and I would like to update a listbox on the second child form, with this data.  This second child form can be opened and closed by the user at any time so cannot be dependent on the first form.   Many Tahnks.
LVL 2
PNRTAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Norman MainaCommented:
in vb 2008/2010 you can simply do this in form1.
frm2.listbox1.items.add(me.listbox1.items)

Open in new window

PNRTAuthor Commented:
Hi, I tried exactly that -     frm2.listbox.items.add(data to add)

This doesnt work though.   When I tried to google "accessing controls on a form from another form", the solutions got very complicated and I was hoping that there was an easier way.  One suggestion was to create a global variable and have the control access that, but I dont see what would fire the updat each time.  Thanks
Norman MainaCommented:
I see- ive tried this right now and it works.
With Form2
   .ListBox1.DataSource = Me.ListBox1.Items
   .Show()
End With

Open in new window

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Norman MainaCommented:
hey,did it work for you???
PNRTAuthor Commented:
Hi, many thanks for the reply.   The code does some very strange things.     Firstly it creates a new instance of the form but although its the same, its a seperate form, not an MDI form.   Then it only shows the first line of data in the list box, not the rest.   But if you close that form, when the form is created the second time, all the data is now there.  Not quite sure whats going wrong.  Thanks again for your help.
Norman MainaCommented:
You can make it an mdi child.-see code below

This code assumes that the first forms listbox is already populated with items.Is that your case?


With Form2
   .ListBox1.DataSource = Me.ListBox1.Items
   .mdiparent=me
   .Show()
End With

Open in new window

PNRTAuthor Commented:
Actually no,  I had the listbox on the first form but the form was getting to big and crowded so I wanted to add a second mdi form that the user could have appear if they wanted to see the data.  

The data is streaming in continuosly and should be added from the first form to the listbox on the second form on a continuous bases if there is a second form showing.   When the data is created it triggers a "data received" event on the first form, which I was using on the listbox on the first form.

I have added a button to simulate the data received.  On the first press of the button, the form appears with the first line of data but nothing else is added when you keep pressing the button.   If you close the form and press the button again, the form appears again but now with all the missing data.  The listbox only seems to update when its closed and reopned
Norman MainaCommented:
Try this:
Dim f As New Form2
 f.Owner = Me
 f.ShowDialog(Me)

Open in new window

PNRTAuthor Commented:
Sorry, couldnt get this to run at all.  I have the error
"A circular control reference has been made. A control cannot be owned by or parented to itself"

I took out the (me) reference and it works the same as before - only updates the listbox when you close and reopen the form
 
This wont work with a dialog form anyway as the rest of the cant be accessed while this is open, which kinda defeats the whole object.

++++++++++++++++++++++
Just to test, I created a test project with two forms, opened the second form and updated a listbox from the first form.  Everything worked the way you would expect.   So surely the problem must be that the fact that its an MDI project.   Maybe the form is not being identified correctly ???
++++++++++++++++++++++++++

Jacques Bourgeois (James Burger)PresidentCommented:
When a form is too crowded, instead or adding extra data to a second form, it is usually a better design to put a TabControl on the form and split the form controls between different tabs.

It also make things easier for you. Since all the controls end up on the same form, you do not have the type of problem you are having with 2 forms.
PNRTAuthor Commented:
Thanks very much for the advice James.  Very good points.   But in this case, this is an MDI application.   There is a lot of financial data from different sources being streamed into the app on a continuous basis, and the app analyses and prepares the data in differemnt ways, showing it then on different forms - graphs, gauges, tickertape etc, there are many forms  for different uses and the user gets to choose what he wants to see and how to arrange his own person screen    So as you can see, its important that it appears as a seperate MDI form is required by the user.  

If I have two seperate forms and run this from form1

Form2.ListBox1.Items.Add("aaaaa")

It works correctly

If I have two MDI forms, both created with -
       
        Dim frm As Form1
        frm = New Form1()
        frm.MdiParent = Me
        frm.Show()

and then on Form1 run the same thing -

Form2.ListBox1.Items.Add("aaaaa")

It doesnt work

I think it has to do with the MDI way the form is created?

Any ideas?

Many Thanks for the assistance



PNRTAuthor Commented:
Sorry should have pointed out that the two firms are created with

        Dim frm As Form1
        frm = New Form1()
        frm.MdiParent = Me
        frm.Show()

and

        Dim frm As Form2
        frm = New Form2()
        frm.MdiParent = Me
        frm.Show()

Not both Form1's

Thanks

CodeCruiserCommented:
When you instantiate the form2, you can add its reference to form1 (declare a variable of type form2 in form1. Then in form1, check that form2 exists and update it

If Not IsNothing(frm2) Then
   frm2.listbox1...
End If

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PNRTAuthor Commented:
Hi CodeCruiser.  Thanks but as you can see from above, with two MDI forms
frm2.listbox1.items.add("12345")  doesnt work.   It seems to work fine with two normal forms,
but not MDI Forms.  

The problem doesnt appear to be ascertaining if the form exists, but actually getting it to update the listbox

Thanks again
Norman MainaCommented:
PNRT>should work with MDI forms.There is something else.
PNRTAuthor Commented:
Hi Norman.  Researching google, I get the impression that transfering data between MDI forms is a big deal     About hald way down this site they get into MDI forms buy its all WAY beyond me

http://www.vbforums.com/showthread.php?t=466253

I've even tried creating a global variable but form1 is unable to get form2 update when the value of the variable is chnaged
PNRTAuthor Commented:

Hi everyone,   many thanks for all the suggestions.   I adapted this solution below and it does what I needed.   Thanks again

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim form2 As Form2 = DirectCast(Me.MdiParent.MdiChildren(1), Form2)
form2.ReceiveValue(TextBox1.Text)
End Sub

Public Sub ReceiveValue(ByVal value As String)  
TextBox1.Text = value
ListBox1.Items.Add(value)
Me.Activate()
Me.Refresh()
End Sub
Norman MainaCommented:
PNRT>i use MDI forms all the time and pass data from form to form whenever i need.Sometimes its good to proof a concept by starting a new project to test the concept.

From your answer above,its clear we didnt have all information of how the app works.
PNRTAuthor Commented:
Hi Norman.   I mentioned above that if I used standard code between two non MDI forms it worked correctly, but if I created the MDI forms using the code above it didnt work.     I still havent seen the difference but if you use MDI all the time and and are not doing anything different from two non MDI forms, please can you advise ?   Many thanks again for ther advice.
Norman MainaCommented:
PNRTDate>could be the version of .net you using?Am using vb 2008 -and before i commented,i had tried it out -using three forms 1)form1-mdi container,2)form 2)mdi child of form1 with a listitem control and form 3)mdi child with with a listitem.

Firing up the app,form1 load...i call form2...and then call form3 and pass the listitem control from form 2 to form3. works fine.

Took me 2mins to test that and was surprised to see you insisting it cant work for mdi apps-and its something i do all the time.

Anyway glad you resolved the issue. Happy New year.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Networking

From novice to tech pro — start learning today.