newbe form question

A

AMP

I have a simple question.
If i have a button on form1 that creates :

Form2 newform = new Form2();
newform.Show();

As I click the button a new form shows,but acording to my code each one
has the same name.Are they really there(I can move them around and
close them).If they are how do they have the same name (to refer to in
code).
Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

AMP,

They have the same title, but that doesn't mean that they have the same
name. Your variable is just a reference, it can point to anything. What
you have to do is store newform somewhere and give it a unique identifier
(the handle value is usually a good one). When you want a particular form,
then you can use that identifier to retrive it.

Hope this helps.
 
J

Jon Skeet [C# MVP]

AMP said:
I have a simple question.
If i have a button on form1 that creates :

Form2 newform = new Form2();
newform.Show();

As I click the button a new form shows,but acording to my code each one
has the same name.Are they really there(I can move them around and
close them).If they are how do they have the same name (to refer to in
code).

What do you mean by "each one has the same name"?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

It sounds like you just need to keep different references to the
different forms, in different instance variables.
 
A

AMP

This is on Form 1
private void button1_Click(object sender, System.EventArgs e)
{
Form2 newform = new Form2();
newform.Show();
}

So how do I get access to newform from Form 1?
Actually any instance i create?
I thought "newform" was the reference.
Sorry for all the silly questions, but I'm trying to get a better grasp
of forms and whats accessible.
 
N

Nicholas Paldino [.NET/C# MVP]

AMP,

newform is just a variable. When you exist the button1_Click method,
the stack is cleared, and that variable is gone. If you want to access it
later, then you need to store the reference on the class level, in a field.
 
J

Jon Skeet [C# MVP]

AMP said:
This is on Form 1
private void button1_Click(object sender, System.EventArgs e)
{
Form2 newform = new Form2();
newform.Show();
}

So how do I get access to newform from Form 1?
Actually any instance i create?
I thought "newform" was the reference.
Sorry for all the silly questions, but I'm trying to get a better grasp
of forms and whats accessible.

newform is the variable. The value of the variable is a reference.

Now, if you want to be able to get at that value from elsewhere, you
should make it an instance variable instead of a local variable -
otherwise nothing else knows how to get to it.
 
A

AMP

........should make it an instance variable instead of a local variable
...............
How?
 
J

Jon Skeet [C# MVP]

AMP said:
.......should make it an instance variable instead of a local variable
..............
How?

By declaring it at the class level rather than within the method.

However, if you're unsure how variables work, I *strongly* suggest that
you stop the project you're working on now in order to learn the basics
of C#. Windows Forms development can be tricky at the best of times,
let alone when you're unsure of the language you're working on.

I suggest you find a good book or tutorial and get comfortable with the
language and the basics of the .NET framework before venturing into
Windows Forms.
 
B

Bruce Wood

What Jon is referring to is the fact that Forms are just classes like
any other class you create. You can construct new ones using the "new"
operator, and tell them to take actions via their methods, e.g.
"Show()" means "show yourself". When you create a Form in the Designer,
notice that the code the Designer generates declares a class:

public class Form2 : System.Windows.Form
{
...
}

This is just another class. When you want to show this form to the
user, you first have to make one of them... aka create an instance:

Form2 newform = new Form2();

then you tell that instance to show itself:

newform.Show();

What Jon was advising is that if you aren't clear on the difference
between a _class_ and an _instance_ of that class, then you need to
read up on that, first, before diving into Windows Forms.
 
A

AMP

Bruce,and all
I know the difference between a class and an instance,I'm trying to
find out how to get access to this new instance(newform) from anywhere.
And why cant i create a form(instance) using a button and then have
access to it from another form.
Thats what I'm trying to learn.
Any help would be appreciated.
Thanks
Mike
 
G

Gabriel Magaña

Technically what you are doing is creating new instances of the form, they
do not all "have the same name", as you create new forms, newForm is
referring only to the last form created. The past created forms are not
refrence-able anymore...

So what you want to do, is create an array (or ArrayList) at the class level
and add new forms to the array every time you create a form. That way you
will stil be able to refer to the existing forms.
 
B

Bruce Wood

We were confused because your question applies to all object instances,
not just forms. So, the question is, "How do I get access to an object
created in another class?" There are several possibilities, depending
upon the effect you want.

1. Create a property in the class that created the object, and make a
private member that is a reference to the object. E.g.:

public class Form1 : System.Windows.Form
{
private Form2 myForm2 = null;
...
public Form2 Form2Instance
{
get { return this.myForm2; }
}
...
private void button1_Click(object sender, System.EventArgs e)
{
this.myForm2 = new Form2();
this.myForm2.Show();
}
}

Of course, this assumes that the other client has a reference to a
Form1 and so can use that to get myForm1.Form2Instance.

2. Create a singleton. If the effect you want is to only ever have one
Form2, and show it whenever the user clicks a button, then you can do
this:

public class Form2 : System.Windows.Forms
{
private static Form2 _instance = null;

private Form2() { ... usual constructor code here ... }

public static Form2 Instance
{
if (Form2._instance == null) { Form2._instance = new Form2(); }
return Form2._instance;
}
}

Now you can get the (only) Form2 around whenever you want it by saying
Form2.Instance.

There are, of course, other less likely scenarios....
 
J

Jon Skeet [C# MVP]

AMP said:
I know the difference between a class and an instance

Possibly - but you don't seem to know the difference between a local
variable and an instance variable. That's fine, and I'm not trying to
be dismissive - everyone's got to start learning from somewhere.
However, trying to code Windows Forms isn't a good way to learn the
basics, IMO.
 
A

AMP

Hello,
Thanks for all your help.
Is my understanding correct in that in order for a form to be created
in another form it MUST "belong" to that form, that a variable must
assigned to hold that reference beforehand(within the parent).
Thanks, as always.
Mike
 
B

Bruce Wood

Is my understanding correct in that in order for a form to be created in another form it MUST "belong" to that form...

No. This is true only of Controls, not Forms. Every Form you
instantiate is a stand-alone object that has nothing to do with the
other forms in your application. (Unless you are doing MDI forms and
set the MdiParent property.)

You can, of course, cause a Form that you instantiated to affect
interaction with other forms in your application by showing it using
ShowDialog() rather than Show(). ShowDialog shows the form as a modal
form, disallowing interaction with the other forms on your screen until
it is dismissed, whereas Show() gives it equal status with any other
forms already showing.

But, no, just because you say:

Form2 newForm = new Form2();

inside a method in Form1 does not give Form2 any special relationship
with Form1, nor does it have to have. That's only for Controls, which
must be parented by some form somewhere in order to appear.
 
A

AMP

Bruce,
so I dont take too much of your time, What are the best books out there
to understand the relationships among objects so they can "see" each
other.
I have several great books (Code Complete 2, which I've read cover to
cover,Pro.C.Sharp.2005.and.the.dot.NET.2.0.Platform,also good), but
there is very little about having several forms interacting with each
other.
Thanks
Mike
 
B

Bruce Wood

I don't know of any good books, offhand. I just played with O-O until I
understood.

Does anyone else know of any elementary books on O-O, with a dose of
simple patterns thrown in (which is essentially what Mike is asking
about)?
 
B

Bruce Wood

By the way, the thing about Controls needing to belong to Forms, but
Forms not really being interrelated is a MS WinForms thing, not an O-O
thing. Other windowing systems might work differently.
 
A

AMP

Bruce, et al
I think I got it now, let me know if this is correct:
In order for a class object to receive a reference to another object,
there HAS to be something (a field or property,ect) in the CLASS
definition that will receive it.You just cant add properties at runtime
just to hold something.
Thanks
Mike
 
B

Bruce Wood

Yes, that's essentially accurate.

You can, of course, "receive" a reference as a method argument, if you
need that reference only for the duration of the method.

However, it's common that you need to "get" a reference to something
else and hold onto it. In that case you need to declare a field in your
class to hold it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top