473,394 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

newbe form question

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

Mar 7 '06 #1
19 1524
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"AMP" <am******@gmail.com> wrote in message
news:11********************@p10g2000cwp.googlegrou ps.com...
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

Mar 7 '06 #2
AMP <am******@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 7 '06 #3
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.

Mar 7 '06 #4
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"AMP" <am******@gmail.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
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.

Mar 7 '06 #5
AMP <am******@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 7 '06 #6
AMP
........should make it an instance variable instead of a local variable
...............
How?

Mar 7 '06 #7
AMP <am******@gmail.com> wrote:
.......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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 7 '06 #8
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.

Mar 7 '06 #9
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

Mar 8 '06 #10
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.
"AMP" <am******@gmail.com> wrote in message
news:11********************@p10g2000cwp.googlegrou ps.com...
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

Mar 8 '06 #11
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....

Mar 8 '06 #12
AMP <am******@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 8 '06 #13
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

Mar 9 '06 #14
> 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.

Mar 9 '06 #15
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.Platfor m,also good), but
there is very little about having several forms interacting with each
other.
Thanks
Mike

Mar 9 '06 #16
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)?

Mar 9 '06 #17
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.

Mar 9 '06 #18
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

Mar 10 '06 #19
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.

Mar 10 '06 #20

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

Similar topics

7
by: Jean Pierre Daviau | last post by:
Hi, <script language="javascript" type="text/javascript"> if(navigator.appName.indexOf("Netscape") != -1){ document.writeln('<link rel="stylesheet" href="~styles/aquarelle_ns.css"...
9
by: Yaro | last post by:
Hello DB2/NT 8.1.3 Sorry for stupid questions. I am newbe in DB2. 1. How can I read *.sql script (with table and function definitions) into a database? Tool, command... 2. In Project Center...
12
by: Jones | last post by:
I am having problems with my C# project. The project was built using VS.net (original release with service pack 1). The project includes windows forms and a DLL (dot.net) After getting the...
1
by: oz | last post by:
private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack) { String FileName; FileInfo MyFileInfo; Long StartPos = 0, FileSize; FileName = "J:\\Summary.txt"; MyFileInfo = new...
1
by: Jim | last post by:
I have created a windows form that contains several tab pages which contain a panels. On a tab page I am trying to dynamically create a series of buttons in that pages panel. I am failing because...
3
by: Kostas Kousinovalis | last post by:
Hello Is it so difficult to make a databound form or am I somewhere wrong? First I create a SQL DataAdapter with a connection to Northwid Products VB creates a SQLConnection1 Then I'm...
7
by: ampeloso | last post by:
Hello, I'm trying to understand how a aspx form gets submitted to the server when there is no action attribute. I am under the impression you would need some kind of html valid button attribute in...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
1
by: Bill Cart | last post by:
I am trying to work with 2 forms. If I set the ModalResult of a button on the 2nd form it works OK. If I try to assign the result and then return it does not return the value I set. 1st Form...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.