Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • Leaders
Posted

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

"These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
Posted

...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();

}

  • Leaders
Posted

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

"These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
  • *Experts*
Posted

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:

Form2 frm2 = new Form2;

foreach(frm2 in this.MdiChildren) {
 frm2.UpdateControls_BackColor();
}

  • Leaders
Posted

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

"These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • *Experts*
Posted

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.

Posted

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....

 

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

 

and then create the new mdiChild like this...

 

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

 

What do you think about it ??

  • *Gurus*
Posted

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,

 

DirectCast(Me.MDIParent, Form1).MyMethod(Me)

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...