Solved

Accessing mdiChild controls from mdiParent

Posted on 2010-09-19
18
884 Views
Last Modified: 2013-12-25
VB .Net 2008

I have two open mdiChild forms (frm_Child_1 and frm_Child_2) and one mdiParent form (frm_Parent).  The child forms are created and work correctly within frm_Parent.

I want to disable a control on frm_Child_1 when I select a menu option on frm_Parent.  (I want to control frm_Child_1 from frm_Parent).

The brute-force method doesn't work (frm_Child_1.btn_Button.enabled = false).  If frm_Child_1 happens to be active, then I can use the activeChild approach and that works, but frm_Child_1 isn't always the active child.

I think my brain has shut down, because this has got to be a "no-brainer"!

Thank you in advance for your help!
0
Comment
Question by:CIW_George
  • 8
  • 6
  • 4
18 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
Comment Utility
try this
If ActiveMdiChild Is Nothing Then

        Else

            Dim btn As Button = CType(ActiveMdiChild, Form1).Button1 'This Code is used to Convert the ActiveMdiChild back ti Form2 Type

            If ..... Then

                btn.Enabled = False

            End If

        End If

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
Comment Utility
form1 is your child form
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
Comment Utility
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
Comment Utility
did understand the difference?
0
 

Author Comment

by:CIW_George
Comment Utility
Thank you for your suggestion - The link you referenced contains the source code you suggested I try.

The problem continues to center around the fact that frm_Child_1 may not be the active child.  Using the code you provided, please look at the following cases:

Code:

        If ActiveMdiChild Is Nothing Then
        Else
            Dim btn As Button = CType(ActiveMdiChild, frm_Child_1).btn_Button
            btn.Enabled = False
        End If

Case 1:

    1.  Start with an empty frm_Parent.
    2.  Open frm_Child_1.
    3.  Click the 'Disable' menu item on frm_Parent (this contains the referenced code).
    4.  btn_Button on frm_Child_1 becomes disabled - exactly what we want.

Case 2:

    1.  Start with an empty frm_Parent.
    2.  Open frm_Child_1.
    3.  Open frm_Child_2.
    4.  Click the 'Disable' menu item on frm_Parent (this contains the referenced code).
    5.  "InvalidCastException occurred : Unable to cast object type of 'frm_Child_2' to type 'frm_Child_1'".

Since frm_Child_1 is no longer the active child after frm_Child_2 is open, the code will fail.

I am looking for a solution that will work with any frm_Child, not just the active child.

Suggestions?
0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 500 total points
Comment Utility
try this

For Each child As Form In Me.MdiChildren



            Dim btn As Button = CType(child, rm_Child_1).btn_Button



            btn.Enabled = False

        Next

Open in new window

0
 

Author Comment

by:CIW_George
Comment Utility
Ok, your recent suggestion worked, with a slight modification:

        For Each child As Form In Me.MdiChildren
            If child.Name = "frm_Child_1" Then
                Dim btn As Button = CType(child, frm_Child_1).btn_Button
                btn.Enabled = False
                Exit For
            End If
        Next

Your suggestion, as provided, would have tried to disable 'btn_Button' on every child form: a) I only wanted to disable btn_Button on frm_Child_1; and b) The code would have failed on the first form that didn't have btn_button.  Therefore, you need to test for the correct child name before attempting to dim btn_Button.

Thank you for your help.  While I have seen this approach many times while searching the Internet, I was hoping there was a way (perhaps with DirectCast or something similar) that would allow me to directly manipulate controls on a form without having to go through the 'For...Next' exercise.

Your (modified) solution solves my immediate problem - thank you.
0
 

Author Closing Comment

by:CIW_George
Comment Utility
I think my initial question was quite clear in my objective.  The initial solution proposed by itoutou left me with the impression that he/she may not have read the entire question.  The 2nd solution was incomplete, in that it did not address the requirement of working with a specific mdi child.  However, iIn the end, the question was answered.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
Comment Utility
I suppose the reason for "Exit For" is to disable the Button in the very first frm_Child_1.What if the user has already open two times  frm_Child_1?
Very glad i helped you
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:CIW_George
Comment Utility
In my project, the user can only open one instance of each form (limited programmatically), so that's why I could do the "Exit For".  With multiple instances of the same form open, the 'For...Next' loop would need to check for additional conditions on each frm_Child_1 to make sure frm_Parent was 'talking' to the correct frm_Child_1.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Just a small point...I would check the TYPE of the MdiChild instead of matching against a string as your apt to run into case problems without realizing it:

        For Each child As Form In Me.MdiChildren
            If TypeOf child Is frm_Child_1 Then
                Dim btn As Button = CType(child, frm_Child_1).btn_Button
                btn.Enabled = False
                Exit For
            End If
        Next
0
 

Author Comment

by:CIW_George
Comment Utility
Thank you for your suggestion Idle_Mind - you've helped me in the past and I always appreciate your input.

Question: What's the difference between using TypeOf in your code vs. checking the name of the child in my code?  OK, I know one method checks the type and the other method checks the name, but is there an advantage to using TypeOf?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
It's easy to make a CaSe ERRoR mISmatCH when comparing with strings.  What if for some reason you change the name of your form from "frm_Child_1" to "FRM_Child_1"?

Now your existing code will not match if you don't remember to manually go back in and change the literal string you're matching against.

The same problem exists if you completely change the name of the from to something else.

...but using the TypeOf approach, the case matching problem goes away and the compiler will complain if you completely change the form name bringing your attention to that particular area where you can fix the code.
0
 

Author Comment

by:CIW_George
Comment Utility
Ah-ha...

TypeOf test it is!

If I did have multiple instances of frm_Child_1 open, the first test would be TypeOf and the second test would/could be checking the name (using frm_Child_1.name.ToUpper and doing the same with the test string).  But then if I tested Upper to Upper, would I still need the TypeOf test?

Thank you Idle_Mind - enjoy what's left of the weekend!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Exactly...if you had multiple instances of a specific type of form present then you would need an additional check to distinguish them from one another.  This could be done with the TEXT property (not Name property as this will be the same for all of them) if you are changing the "Caption" to some kind of value that has a "counter" in it.  Here you would definitely want to use ToUpper() or ToLower() to eliminate the case issue.

You could always use some other method of identifying multiple instances such as using the Tag() property or adding a custom Property to create some kind of unique identifier.

The "best" approach would always depend upon your particular scenario though...lots of ways to do things in programming!  =D
0
 

Author Comment

by:CIW_George
Comment Utility
You're right about 'name' vs. 'text' - my mistake.  I do change the text for each new instance of the same form, along with a counter.

So, if I were testing the text value (given all uppercase), would there be any reason to use TypeOf?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Only if you wanted to reduce the amount of parsing necessary if different types of child forms could be present and each child form had a different pattern to their caption.  By using TypeOf you could be assured that the caption matched the expected pattern and your parsing routine should work...  =)
0
 

Author Comment

by:CIW_George
Comment Utility
OK - I'll buy that.

Thank you Idle_Mind!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now