473,695 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\T PAccessToSQLDat aTransfer\TPAcc essToSQLDataTra nsfer\frmDataEd itGrid.cs
358 5 TPAccessToSQLDa taTransfer
frmFindReplace. Close();

Error:

Error 1 An object reference is required for the nonstatic field, method, or
property 'System.Windows .Forms.Form.Clo se()' Visual Studio
2005\Projects\T PAccessToSQLDat aTransfer\TPAcc essToSQLDataTra nsfer\frmDataEd itGrid.cs
358 5 TPAccessToSQLDa taTransfer

Where am I going wrong here?

Thanks,

Ron

Jan 4 '06 #1
4 1187
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.co m> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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.co m> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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************* ***@tk2msftngp1 3.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 HandleFindFormC losed(object sender, FormClosedEvent Args e)
{
frmFind.FormClo se -= null;
frmFind = null;
}

public void OpenFindForm()
{
if (frmFind == null)
{
frmFind = new FormFindReplace (...);
frmFind.FormClo se += HandleFindFormC losed;
}
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.FormCl ose += HandleFindFormC losed;
}
frmFind.Show();
}

private static void HandleFindFormC losed(object sender,
FormClosedEvent Args e)
{
instance.FormCl ose -= 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 (FormFindReplac e.Instance != null)
FormFindReplace .Instance.DoSom ething();
}

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

{
FormFindReplace frmFind = FormFindReplace .Instance;
if (frmFind != null)
{
frmFind.DoSomet hing();
frmFind.DoSomet hingElse();
...
}
}
}

Joanna

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
10950
by: Razzbar | last post by:
I've written several utility pages that use a dynamically created iframe. The iframe typically has a form that is populated by the main page. Then the form is submitted to be processed, and the returning page does something -- call a function on the main page, change a variable on the main page, etc. All this stuff I created works on IE. Sorry, didn't have other browsers available to test. Now I have access to a machine with FF, and none...
0
956
by: Colin Graham | last post by:
Hi all, Just wondering if anyone could help me with this. I have just started working with XML documents. I have just built a form that creates an xml document and it sends it to biztalk where i get an xml document as a response. in code i then read a tagvalue from the xml document and depending on the result i open a new page. I was wondering what the best method is for referencing the response xml document created on the previous...
6
2463
by: jstaggs39 | last post by:
I want to create a Dcount and an If...Then...Else statement to count the number of records in a table based on the date that is entered to run the form. The If....Else statment comes in because if the amount of records for a particular date is positive, i want the form to stop running, if there are no records that contain the date in question, then it should continue to run the form. The problem i'm having is referencing the date that is...
1
6319
by: Will | last post by:
Hi all. I'm learning VB.Net and am developing a WinForms app. I'm trying to make an app that I will use to scan in one or more than on image. I want to use a tabbed interface to hold each image. Here's the code I'm using for testing purposes. I've got the code in the form's load event, but I think I'd have the same problems no matter where the code existed. Right now, the form has an empty tab control, everthing else is dynamic. <code>
2
1685
by: J | last post by:
I'm wondering why the form created in the code below stays "alive". I would have thought that because the form variable is local to the method, that garbage collection would get rid of it. Makes me wonder if there is a reference to it somewhere? // a button on one form is used to call a static method on another form, the static method creates the form private void btnNewForm_Click(object sender, System.EventArgs e) {...
1
888
by: Lynn Morrison | last post by:
Hi, I created a Form and need a pointer to it so that I can reuse it again and again instead of recreating it all the time.. something like this... Form1.h // .... snipped for brevity #include "Form2.h" // class def ... snip
6
2471
by: Mikey_Doc | last post by:
Hi We are running cms 2002, Framework 1.0 with Visual studio 2002. We have just upgraded to Framework 1.1 and visual studio 2003. All of our database connection strings are stored within the machine config, this was necessary as our web site has 4 environments and the database server has a different name in each. Since the upgrade the applications can't read the strings in the
21
5095
by: cmd | last post by:
I have code in the OnExit event of a control on a subform. The code works properly in this instance. If, however, I put the same code in the OnExit event of a control on a Tab Control of a main form, the code errors out at the 2nd line. The error number is 13 and the description is "Type mismatch". Both controls are memo fields. I suspect that "Screen.ActiveControl.Parent" is not referencing the form as intended. Thanks for any help,...
2
2226
by: ccsnavy | last post by:
For some reason referencing an unbound control on an active form from a query has ceased to work correctly. While other previously existing references to unbound controls in the same form seem to work fine new ones just return null or if referencing a combo box some random character (usually a box). In fact any subsequent queries created that reference controls on active forms don't seem to work properly. What could be causing this problem? ...
0
8650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9004
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8864
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5843
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4351
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3025
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2290
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.