Jump to content
Xtreme .Net Talk

Transfering informatin between forms without dimensioning as withevents


Recommended Posts

  • *Experts*
Posted

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

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

  • *Experts*
Posted

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

"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

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

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

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