Solved

Accessing mdiChild controls from mdiParent

Posted on 2010-09-19
18
895 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 4
18 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33711938
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
ID: 33711963
form1 is your child form
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33711983
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33712030
did understand the difference?
0
 

Author Comment

by:CIW_George
ID: 33712088
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
ID: 33712122
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
ID: 33712161
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
ID: 33712179
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
ID: 33712184
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
 

Author Comment

by:CIW_George
ID: 33712206
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 86

Expert Comment

by:Mike Tomlinson
ID: 33712606
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
ID: 33712844
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 86

Expert Comment

by:Mike Tomlinson
ID: 33712867
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
ID: 33712887
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 86

Expert Comment

by:Mike Tomlinson
ID: 33712914
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
ID: 33713020
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 86

Expert Comment

by:Mike Tomlinson
ID: 33713027
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
ID: 33713915
OK - I'll buy that.

Thank you Idle_Mind!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

696 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