Ok,
I have a small systray application which has a context menu associated with it. One of the options on the context menu bring up a dialogue box and prompts the user for information. My goal is simply this.
1. Take the information that the user enters on the form
2. Pass this information from the current form to some main form variables via public SET properties in the main form
3. Close the dialogue once the information has been passed.
My issue...
The only way I know how to access the public properties to set the variables in the main form is to instantiate a new instance of my main form. HOWEVER, when I do this, I end up with 2 running instances of my system tray application.
Is there a way to grab the 'current' instance of a form or how should I be doing this?
I know this is probably a very basic task but I can't seem to find the answer.
Thanks all!!!
Bob
Loading
AlanPosted Jul 2, 2007, 6:19 AM
FWIW you can still make it effectively readonly by changing the code as follows:
private static Form1 reference;
public static Form1 Reference
{
get{ return reference;}
}
public Form1()
{
reference = this;
InitializeComponent();
// any other code
}
Bob BoursawPosted Jul 2, 2007, 1:51 AM
That worked great. The only thing I ran in to is that I would receive a build message if the I made the initial property readonly.
AlanPosted Jul 1, 2007, 10:53 AM
If you don't have .NET 2.0, then there are several other ways for your dialog box to obtain a reference to your main form. I've described one below
Define a public static readonly field in your main form (say Form1), like so:
public static readonly Form1 Reference;
Now change the constructor of Form1 to include a line to initialize the static field:
public Form1()
{
Reference = this;
InitializeComponent();
// any other code
}
It shouldn't matter that this is a static property because there will (presumably) only ever be one instance of the main form.
You can now call a method on Form1 from the dialog box with code like this:
Form1.Reference.SomeMethod();
Bob BoursawPosted Jul 1, 2007, 10:52 AM
I basically want to be able to access the properties and methods of my main form from the DialogResult but DO NOT want to have a 2nd instance of my systray application start running. I have to believe there is a way to do this because you see it in applications every day.
Bob BoursawPosted Jul 1, 2007, 10:34 AM
1) store that information in to variables in the main form
2) execute methods that exist in the main form while still showing the secondary dialogue result to the user.
That's why I thought I would need to somehow get the current instance of the form or something to that affect.
Regarding the other response, doesn't framework 2.0 require you to be in VS 2005? Unfortunately, I do not have that.
AlanPosted Jul 1, 2007, 5:38 AM
http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.openforms(VS.80).aspx
In particular the main form instance will be Application.OpenForms[0] though the collection is also indexed by the name of the form.
Wim DevosPosted Jul 1, 2007, 4:51 AM
To do this you have to use
dialogresult
to open the dialog use code like this:
MyDialog myDialog = new MyDialog(); if(myDialog.showDialog() == dialogresult.OK) { this.someVar = myDialog.propertyValue; // get a property from the form and store it ... myDialog.Close(); }