gvector1 Posted January 14, 2004 Posted January 14, 2004 Hello everyone, I have created a MultiDocument application in C#. One that has one main form and a few child forms. What I am trying to do is that since these child forms access a database, I need to keep them updated. I already have the main parent form using a timer and polling the database for changes. When a change is detected I need to reload the forms. How can I reload a child form. Using the refresh method only redraws the form, I need to reload the form so that the method that updates the data on the form is executed. One of the problems is that the parent form does not create every one of it's child forms. A child form will also create a form with the parent set to it's parent. I hope I didn't lose you. Basically, the parent form does not instantiate every one of it's child forms. Ideally I would use a foreach(Form f in this.MdiChildren) to cycle through each form and execute a reload method. Each forms load method will reload the data, but I don't know how to trigger an event in a foreach loop. Any and all suggestions are greatly appreciated. Anyone have any suggestions on how to accomplish this?????? Thanks, Kendal Quote
Administrators PlausiblyDamp Posted January 14, 2004 Administrators Posted January 14, 2004 You could provide a public function in each form which reloads the data and call this from your for...each loop. This approach could cause a lot of database activity and network traffic though if you have lots of users with lots of forms refreshing too frequently. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
gvector1 Posted January 14, 2004 Author Posted January 14, 2004 How would I do that? I have tried it so far by creating a public method call Reload() in each form. In the main form I use foreach(Form f in MdiChildren) { f.Reload(); } but it will not work. Reload is only available from an instance of the specific class, not from the form class. Am I wrong???? That is just from my thinking, and my thinking is kind of whack some times.....lol. What would you suggest????? Thanks again, Kendal Quote
Administrators PlausiblyDamp Posted January 14, 2004 Administrators Posted January 14, 2004 (edited) probably the OO way of doing things would be to create an interface (IReload or similar) and get each of your child forms to implement this. //interface declaration public interface IReload { void Reload(); } in each child form use the following idea as the form declaration (if you are not inheriting from a System.windows.Forms.Form then adjust accordingly public class Form1 : System.Windows.Forms.Form, IReload then you could create a for each loop similar to the following foreach (Form f in MdiChildren) { IReload ir = f as IReload; if(ir != null) ir.Reload(); } this way if a child doesn't need to be forced to refresh - don't implement the interface. If it does implement it and provide the Reload method and it should work :-\ Edited January 16, 2009 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.