mdiChild reference

fadex

Newcomer
Joined
Jan 15, 2003
Messages
5
Location
Italy
It's no a lot I'm using c#.net

Can i had dinamically a reference to the same mdiChild form open more times from my mdiParent Form ??....for exemple to change the backColor of a control on the mdiChild when they are opened...??

Any idea?
 
The MDI Parent always has references to all the open children via it's MdiChildren collection. You could loop through the collection to find a specific child then when the child is found change the color of the control or whatever you want to do. Just use a foreach loop to do it.

From MSDN
foreach-statement:
foreach ( type identifier in expression ) embedded-statement
 
...but Why I get an Exception using this code....
only when: ---> are opened some mdiChilds: Form2 &
one mdiChild: Form1

...i thought the loop is only searching for Form2 in this way ? I'm wrong?

foreach(Form2 frm2 in this.MdiChildren){
frm2.UpdateControls_BackColor();
}
 
try changing "this.MdiChildren" to "Me.MdiChildren". If I remember correctly, "this" refers to the base class that all your objects are created from, and "Me" refers to the current instance of the class (object).

Orbity
 
Well, you can't use Me in C#; that's a VB.NET keyword, and
yes, they both do the same thing

There is really no such thing as a "current instance of a class", at
least not that you can put code inside (like a form). When you put
code inside a form, you are putting the code inside a class that
inherits from System.Windows.Forms.Form. It's not an instance of
any class, but rather a subclass of "Form".


fadex: What is the exception you're getting? You might want to
try initializing the frm2 var first. I'm no C# whiz, but you could try
this:
C#:
Form2 frm2 = new Form2;

foreach(frm2 in this.MdiChildren) {
  frm2.UpdateControls_BackColor();
}
 
Ok, here I go mixing up my languages again. I did the same thing when I started with VB6 mixing up things from C++ and Java with VB. So this = C# and Me = VB then right? Which means I was confusing "this" with "base" in my previous response.

Sorry about that fadex, someday I'll get them straight :-\

Orbity
 
You're exactly right, Orbity. Forget about "Me" in C# - doesn't exist. "this" is the current instance of whatever class you're using it in. "base" is the base object that you're inheriting from. "base" is most often used in methods that are overridden so that you can still execute the base class's code in addition to your own. It can also be used to call the base class's constructor.

-ner
 
Oh, I see what you mean by 'base' now; I thought you were referring to using it in events or something. Indeed, base (or
MyBase in VB) references the class you are inheriting. Conversely, although it doesn't exist in C#, there is a MyClass
keyword which you can use in a class to reference a class which is
inheriting it.
 
C#:
foreach(Form f in this.MdiChildren)
{
	if (f is Form2)
		((Form2)f).UpdateControls_BackColor();
}
 
Thanks divil, this works ok :p

And say..: if I need to send some "information" from A (Form2)mdichild to the MdiForm and use it to perform something, and then send back the result to THAT (Form2)mdichild ....I can use "this way" (I mean using the MdiParent Collection) ???

In origin i start with this type of idea thinking that after would be easyer to "comunicate" between forms....

C#:
private const int iMaxForm2 = 5;
private Form2[] frmForm2 = new Form2[iMaxForm2];
private static int iCurForm2 = -1;

and then create the new mdiChild like this...

C#:
if(iCurForm2+1 < iMaxForm2){
    iCurForm2 += 1;
    frmForm2[iCurForm2] = new Form2();
    frmForm2[iCurForm2].MdiParent = this;
    frmForm2[iCurForm2].Show();                 		
}

What do you think about it ??
 
No, you don't have to do anything like that. To call a function in your main form from an mdi child, it's really easy. Define the function in your main form to accept an argument of type Form2, so it can call back to the one that called it, then from inside an mdi child,

Visual Basic:
DirectCast(Me.MDIParent, Form1).MyMethod(Me)
 
Back
Top