Referencing a Form after it is created...

  • Thread starter Thread starter RSH
  • Start date Start date
R

RSH

Okay in my coninuing forms saga I have a situation where I am spawning a new
form from my main form:

//////////////////////////////////////////////////////////////////////////////////////

frmFindReplace frmFind = new frmFindReplace(this);

frmFind.Show();

bFRVisible = true;

//////////////////////////////////////////////////////////////////////////////////////

Now I would like to reference that form from a different function within the
same main form but I can't figure it out.

I tried:

frmFind.Close();

Error:

Error 1 The name 'frmFind' does not exist in the current context Visual
Studio
2005\Projects\TPAccessToSQLDataTransfer\TPAccessToSQLDataTransfer\frmDataEditGrid.cs
358 5 TPAccessToSQLDataTransfer


frmFindReplace.Close();

Error:

Error 1 An object reference is required for the nonstatic field, method, or
property 'System.Windows.Forms.Form.Close()' Visual Studio
2005\Projects\TPAccessToSQLDataTransfer\TPAccessToSQLDataTransfer\frmDataEditGrid.cs
358 5 TPAccessToSQLDataTransfer





Where am I going wrong here?



Thanks,

Ron
 
RSH said:
Okay in my coninuing forms saga I have a situation where I am spawning a new
form from my main form:

Where am I going wrong here?

I suspect you're thinking of forms as being different to other objects.
They're not, really. What would you do if you wanted to use an object
created in one method elsewhere? You'd use a member variable (or
something similar) to store a reference to the object. Apply exactly
the same principle to your form. Declare an instance variable in the
class, and use that instead of a local variable when you create the
instance of the form.

Jon
 
I would start by naming the form class FindReplaceForm instead of
frmReplaceForm, which is the first step to start treating them as objects,
not as VB pseudo-whatever you are probably used to.
 
Well since you put it that way :-)

That worked great.

I guess I need to spend some more quality time with my C# OOP book :-)

Thanks!
Ron
 
"RSH" <[email protected]> a écrit dans le message de %[email protected]...

| Okay in my coninuing forms saga I have a situation where I am spawning a
new
| form from my main form:
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
| frmFindReplace frmFind = new frmFindReplace(this);
|
| frmFind.Show();
|
| bFRVisible = true;
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
| Now I would like to reference that form from a different function within
the
| same main form but I can't figure it out.

You need to move the variable you are using to reference the form to be a
private field of the main form class instead of a variable in the method.

public class MainForm
{
private FormFindReplace frmFind = null;

private void HandleFindFormClosed(object sender, FormClosedEventArgs e)
{
frmFind.FormClose -= null;
frmFind = null;
}

public void OpenFindForm()
{
if (frmFind == null)
{
frmFind = new FormFindReplace(...);
frmFind.FormClose += HandleFindFormClosed;
}
frmFind.Show();
}

public void CloseFindForm()
{
if (frmFind != null)
frmFind.Close();
}
}

Of course, if you only ever want one instance of the Find form class, then
you can modify that class to ensure that only one instance ever gets
created, then that greatly simplifies your code in the main form. This is
known as the Singleton design pattern.

public class FormFindReplace
{
private FormFindReplace() : base() {} // declare a private constructor to
stop
//
client code from creating more than one instance

private static FormFindReplace instance = null;

public static FormFindReplace Instance
{
get { return instance; }
}

public static void ShowForm()
{
if (instance == null)
{
instance = new FormFindReplace(...);
instance.FormClose += HandleFindFormClosed;
}
frmFind.Show();
}

private static void HandleFindFormClosed(object sender,
FormClosedEventArgs e)
{
instance.FormClose -= null;
instance = null;
}

public static void CloseForm()
{
if (instance != null)
instance.Close();
}
}

Now from your main form, you no longer need a variable unless it is for
temporary holding of the find form.

public class MainForm
{
public void OpenFindForm()
{
FormFindReplace.Show();
}

public void CloseFindForm()
{
FormFindReplace.Close();
}

// if you need to do something between showing and closing the form,
// then do something like this :

{
if (FormFindReplace.Instance != null)
FormFindReplace.Instance.DoSomething();
}

// if you need a temporary variable to do several things,
// then do something like this :

{
FormFindReplace frmFind = FormFindReplace.Instance;
if (frmFind != null)
{
frmFind.DoSomething();
frmFind.DoSomethingElse();
...
}
}
}

Joanna
 
Back
Top