*Experts* DiverDan Posted October 22, 2003 *Experts* Posted October 22, 2003 I'm going to babble for a bit, so please bear with me. You can pass information between forms with a publically declared DialogResult variable and a modal form easy enough. You can also dimension a form as WithEvents and wrtie an event handler in the receiving form. For me, this is the prefered method because the forms act like program forms should, capable of minimizing and maximizing, etc. However, to use a form declared WithEvents, it must be declared in the declaration section of the program and therefore must be hiddened, not disposed, so you can use it again. This is not too bad except when you have multiple forms accessing this form. It creates many instances of the same form, and this is not good. I currently have a public array that I use to pass information from form to form, but of course, this array will not raise an event. How can multiple forms access the same form, with events, while not creating multiple instances of the same form? Thanks Dan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
*Experts* Nerseus Posted October 22, 2003 *Experts* Posted October 22, 2003 You can create a public static (shard in VB?) method in the class/form that handles showing the form. Something like this (sorry for C#, but you should be able to convert): In frmPopup, the shared form: // Inherits from form public class frmPopup : Form { private frmPopup thisForm = null; // Nothing in VB // Constructor is private so no one else // can create this form private frmPopup() { } // Method to get the form - use instead of: // Dim f as frmPopup = New frmPopup() public static frmPopup GetForm() { if(frmPopup==null) { // Constructor is private, but this form can still create itself frmPopup = new frmPopup(); } return frmPopup; } } In the calling forms: public class frmUser : Form { private frmPopup myPopup = null; private void SomeFunction() { // Call the static method of the class // Which returns an instance of frmPopup myPopup = frmPopup.GetForm(); myPopup.ShowDialog(); } } The idea is that the popup contains a private reference to an instance of itself. Access to the form is only granted through the static method, which returns a new instance of the form the first time it's called. On a second call, it returns the same instance. In C# you don't have to declare a variable as "WithEvents" to have access to it's events. In VB.NET, if you declare the myPopup variable in frmUser as WithEvents it should be fine. I have a sneaking doubt that you can't create the frmPopup if the constructor is private. If that's the case, make it protected (not sure of the word in VB.NET - you want it so that only the current form or inherited forms can access it, not Friend which is all forms in the assembly). -Ner Quote "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
Administrators PlausiblyDamp Posted October 22, 2003 Administrators Posted October 22, 2003 You could always use AddHandler and RemoveHandler to wire up the events as and when ypou need them. This means you could decalre the variable as normal within a procedure and still get access to it's events. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* DiverDan Posted October 22, 2003 Author *Experts* Posted October 22, 2003 Thanks to you both for your ideas. PlausiblyDamp, as I understand an event handler requires two objects, the initial object that fires the event and a delegate object that receivies the event. So if I declare a public shared function in the receiving form will this work? Could you show me an example? Thanks Dan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
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.