471,317 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Referencing a Form after it is created...

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\TPAccessTo SQLDataTransfer\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\TPAccessTo SQLDataTransfer\frmDataEditGrid.cs
358 5 TPAccessToSQLDataTransfer

Where am I going wrong here?

Thanks,

Ron

Jan 4 '06 #1
4 1098
RSH wrote:
Okay in my coninuing forms saga I have a situation where I am spawning a new
form from my main form:
<snip>
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

Jan 4 '06 #2
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
RSH wrote:
Okay in my coninuing forms saga I have a situation where I am spawning a
new
form from my main form:


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

Jan 4 '06 #3
RSH
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
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
RSH wrote:
Okay in my coninuing forms saga I have a situation where I am spawning a
new
form from my main form:


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

Jan 4 '06 #4
"RSH" <wa*************@yahoo.com> a écrit dans le message de news:
%2****************@tk2msftngp13.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Colin Graham | last post: by
1 post views Thread by Will | last post: by
2 posts views Thread by J | last post: by
1 post views Thread by Lynn Morrison | last post: by
6 posts views Thread by Mikey_Doc | last post: by
21 posts views Thread by cmd | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.