Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

find open childform and use their method.

Hi all,

I have a MDIform wich opens a childform A. On A I have a method to add 'descriptions'  to a database. This all works fine.
I have some other forms who use that data in a combobox.
What I want is when I add or remove a new item  through form A that all other open childforms wich use the data in a combobox get refreshed with the new added item.

I have something like this: when the saving of the new item is ok, then loop through the open childforms with a specific name. Then call their FillComboBox method.


This works: ((formDocument)this.MdiChildren[i]).FillComboDescriptions();  but then I need to know the Index of the opened childform. How can I find the index of the opened child form with a specific name?

Or is there a better way to deal with this?


foreach (Form childForm in this.MdiChildren)
            {
              if (childForm.Name == "formDocument")
              {
                int i = childForm.Index????
                ((formDocument)this.MdiChildren[i]).FillComboDescriptions();
                return;
              }
            }

Open in new window

0
Pit76
Asked:
Pit76
  • 2
2 Solutions
 
RPCITCommented:
Example 1 should work for you..

but if you really need in the index.. you can do something like example 2

//example 1
foreach (Form childForm in this.MdiChildren)
            {
              if (childForm.Name == "formDocument")
              {
                //int i = childForm.Index????
                ((formDocument)childForm.FillComboDescriptions();
                return;
              }
            }
 
//example 2
for(int i = 0;i<MdiChildren.Count<Form>();i++)
            {
                if (this.MdiChildren[i].Name == "formDocument")
                {
                    ((formDocument)this.MdiChildren[i]).FillComboDescriptions();
                }
            }

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I would think you also need to remove the "return;" line...otherwise you'll only update the first form of that type that you find.

Additionally, instead of checking the Name() Property, you can do a "Type Match" like this:

            foreach (Form childForm in this.MdiChildren)
            {
              if (childForm is formDocument)
              {
                ((formDocument)childForm).FillComboDescriptions();
              }
            }

Open in new window

0
 
Pit76Author Commented:
I've split the points cos both of the solutions work :) Thx for that.
0
 
Pit76Author Commented:
Thx guys!

I've split the points cos both of the solutions work :)
Only thing I had to change is this.MdiChildren to this.MdiParent.MdiChildren cos the calling form is a childform itself.

Grts!

            foreach (Form childForm in this.MdiParent.MdiChildren)
            {
              if (childForm is formDocument)
              {
                ((formDocument)childForm).FillComboDescriptions();
              }
            }

Open in new window

0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now