?
Solved

Help Displaying Message on Correct Child Form in MDI Parent

Posted on 2009-12-17
6
Medium Priority
?
389 Views
Last Modified: 2013-11-07
I am trying to get a VB .Net application working that has the following:

MDI Parent Form

4 Child Windows Forms

One socket that listens for incoming messages on a separate thread

Parsing the incoming socket message will tell me on which of the 4 child windows to display the message

Assuming the incoming message looks something like:

{ScreenName}^{Message}
Screen1^This is a test message

When the message is received, how would I then get the message back to the correct form

I plan on using a delegate to pass the message back to the GUI thread.  Something like:

ChildForm.DisplayMessage(messageToDisplay)


Each of the four child forms has a handler for the delegate.  Something like:

Private Delegate Sub DisplayMessageHandler(ByVal messageToDisplay As String)


My question is how to get the incoming message to the correct form.  For example, if the message is "Screen1^This is a test message", how can I get it to display on child form 1, and not on child forms 2, 3, and 4?
0
Comment
Question by:Ignyte_Software
  • 4
  • 2
6 Comments
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 26075618
You can set the Name property of each of the childforms, and then use that to find the correct one.

For each frm as Form in MainForm.MdiChildren

Next
0
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 26075626
Sorry, I wasn't finished yet:

You can set the Name property of each of the childforms, and then use that to find the correct one.

For each frm as Form in MainForm.MdiChildren
   if frm.Name = screenName then
      frm.DisplayMessage(messageToDisplay)
      exit for
   end if
Next
0
 

Author Comment

by:Ignyte_Software
ID: 26075664
Where would I put the For each frm . . . code?

Would I need to move the Private Delegate Sub DisplayMessageHandler(ByVal messageToDisplay As String) to the MDI Parent Form, instead of having it in each of the four child forms?  

Or would it remain in each child form?  If it remains in the child form, then when the incoming message is passed to the delegate, do the delegate handlers on all four child forms receive the same message?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 26075765
When the message is received, you can loop through the collection of childforms and only call the DisplayMessage method on the one with the corresponding name.

Of course, the Child form needs to have the DisplayMessage method, so you will probably have to cast the frm variable (in my example) to the correct type before you can call its DisplayMessage method.
I suppose each of your Child forms has the same type?

For each frm as Form in MainForm.MdiChildren
   if frm.Name = screenName then
      DirectCast(frm, MyChildFormType).DisplayMessage(messageToDisplay)
      exit for
   end if
Next

Or do you have different types for each child form?
0
 

Author Comment

by:Ignyte_Software
ID: 26076005
Below is how I am creating each form.  It is really four instances of the same form:

        Dim Screen1 As New Form1()
        Dim Screen2 As New Form1()
        Dim Screen3 As New Form1()
        Dim Screen4 As New Form1()

        Screen1.Name = "Screen1"
        Screen2.Name = "Screen2"
        Screen3.Name = "Screen3"
        Screen4.Name = "Screen4"

        Screen1.Text = "Screen1"
        Screen2.Text = "Screen2"
        Screen3.Text = "Screen3"
        Screen4.Text = "Screen4"

        Screen1.MdiParent = Me
        Screen2.MdiParent = Me
        Screen3.MdiParent = Me
        Screen4.MdiParent = Me

        Screen1.Show()
        Screen2.Show()
        Screen3.Show()
        Screen4.Show()

If I understand you correctly, the "For each frm as Form . . . " code will be inside my socket listener code.  When a message is received, and I know the screen where it is to be delivered, I will loop through all Child Forms until I find the match.  I will then use the DirectCast to call the DisplayMessage delegate handler for only that Child Form.

Is this correct?  I hope so, because it makes sense to me :-)
0
 
LVL 10

Accepted Solution

by:
joriszwaenepoel earned 2000 total points
ID: 26076046
That is correct.  Or you could use

For each frm as Form1 in mainForm.ChildForms

Where you specify the correct type immediatly, so you can avoid the DirectCast.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

578 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