By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,056 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,056 IT Pros & Developers. It's quick & easy.

Declaring a reference on one form to another form?

P: n/a
Hi All,

I have an app that presents a small main form when run. When a
particular button is clicked, I'd like to briefly display another small
form directly below the main form, regardless of where on the screen
the main form is currently located.

I'm trying to work out how, from the class that displays the second
form, I reference the x and y coords of the main form so I can set
these for the second form when it is displayed?

So, in generic terms, I'm trying to work out how I reference, from a
class, properties and methods of an already displayed / instantiated
form?

Any help appreciated!

Regards,

pt

Sep 25 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi,

For a form you can use the owner property, that gives you the change to get
very easy there.
(While from an owner to a child of that is of course a normal reference)

http://msdn2.microsoft.com/en-us/library/decz3b5c.aspx

I hope this helps,

Cor

"planetthoughtful" <pl**************@gmail.comschreef in bericht
news:11**********************@b28g2000cwb.googlegr oups.com...
Hi All,

I have an app that presents a small main form when run. When a
particular button is clicked, I'd like to briefly display another small
form directly below the main form, regardless of where on the screen
the main form is currently located.

I'm trying to work out how, from the class that displays the second
form, I reference the x and y coords of the main form so I can set
these for the second form when it is displayed?

So, in generic terms, I'm trying to work out how I reference, from a
class, properties and methods of an already displayed / instantiated
form?

Any help appreciated!

Regards,

pt

Sep 25 '06 #2

P: n/a
Cor Ligthert [MVP] wrote:
Hi,

For a form you can use the owner property, that gives you the change to get
very easy there.
(While from an owner to a child of that is of course a normal reference)

http://msdn2.microsoft.com/en-us/library/decz3b5c.aspx

I hope this helps,

Cor
Hi Cor,

Sorry to be stupid, but I'm still having problems understanding how
this works. Let me explain a little further, in case it becomes clearer
where I'm getting lost.

I have a mainform on which I have a text field and a button.

When text is entered into the text field and the button is clicked, I
create a class that primarily breaks up the text entered in the text
field on the main form and saves it to a database, depending on several
factors.

In that class, I also display a form (let's call it "Form2") to confirm
how the text in the text field was handled. Because my main form is a
small window, I want Form2 to be displayed below it, so in the class I
need to be able to retrieve the x,y coords of my mainform, so I can use
these to set the Left and Top properties of Form2 when I instantiate
it.

I hope I've explained myself a little better, and apologies for not
being more explicit originally.

All the best,

pt

Sep 25 '06 #3

P: n/a
Hi,

If you declare in your mainform the used form as

Form2 frm2 = new Form2();
frm2.Owner = this;

Than you can use it in form2 as
Points mypoints = ((Form1)this.Owner). etc.;
(I did not check if it was Points, just as sample)

I hope this gives an idea,

Cor
"planetthoughtful" <pl**************@gmail.comschreef in bericht
news:11**********************@h48g2000cwc.googlegr oups.com...
Cor Ligthert [MVP] wrote:
>Hi,

For a form you can use the owner property, that gives you the change to
get
very easy there.
(While from an owner to a child of that is of course a normal reference)

http://msdn2.microsoft.com/en-us/library/decz3b5c.aspx

I hope this helps,

Cor

Hi Cor,

Sorry to be stupid, but I'm still having problems understanding how
this works. Let me explain a little further, in case it becomes clearer
where I'm getting lost.

I have a mainform on which I have a text field and a button.

When text is entered into the text field and the button is clicked, I
create a class that primarily breaks up the text entered in the text
field on the main form and saves it to a database, depending on several
factors.

In that class, I also display a form (let's call it "Form2") to confirm
how the text in the text field was handled. Because my main form is a
small window, I want Form2 to be displayed below it, so in the class I
need to be able to retrieve the x,y coords of my mainform, so I can use
these to set the Left and Top properties of Form2 when I instantiate
it.

I hope I've explained myself a little better, and apologies for not
being more explicit originally.

All the best,

pt

Sep 25 '06 #4

P: n/a

Cor Ligthert [MVP] wrote:
Hi,

If you declare in your mainform the used form as

Form2 frm2 = new Form2();
frm2.Owner = this;

Than you can use it in form2 as
Points mypoints = ((Form1)this.Owner). etc.;
(I did not check if it was Points, just as sample)

I hope this gives an idea,
Hi Cor,

I think I'm beginning to understand. It's best to instantiate a
secondary form from within your main form, rather than from within a
'worker' class.

Still, it seems strange that I can't find a syntax to refer to an
object that has been created in my application, no matter where I
happen to be in the code.

I had expected to be able to:

- start up main form
- instantiate class on click of button on main form
- refer to properties of main form from within class
- use properties determined from main form to open form2 from within
class

For the life of me though, I can't figure out how to do this.

Instead, it looks like I need to:

- start up main form
- instantiate class on click of button on main form
- determine properties of main form from click of button on main form
- instantiate form2 from click of button on main form

All the best,

pt

Sep 25 '06 #5

P: n/a
planetthoughtful <pl**************@gmail.comwrote:
I think I'm beginning to understand. It's best to instantiate a
secondary form from within your main form, rather than from within a
'worker' class.
Not necessarily - but if the 'worker' class needs to know about the
form, it will need a reference to it.
Still, it seems strange that I can't find a syntax to refer to an
object that has been created in my application, no matter where I
happen to be in the code.
Think of the form as an object (which it is). You could have multiple
instances of the form - how would the code know which one you meant?

If you're absolutely sure that you'll only ever have one instance of
the form, you could create a static property in the form, with a static
variable backing it, and set the static variable's value to "this" in
the constructor, eg:

class MyForm : Form
{
static MyForm instance;

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

public MyForm()
{
instance = this;
}

...
}

--
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
Sep 25 '06 #6

P: n/a

Jon wrote:
Think of the form as an object (which it is). You could have multiple
instances of the form - how would the code know which one you meant?

If you're absolutely sure that you'll only ever have one instance of
the form, you could create a static property in the form, with a static
variable backing it, and set the static variable's value to "this" in
the constructor, eg:

class MyForm : Form
{
static MyForm instance;

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

public MyForm()
{
instance = this;
}

...
}

Hi Jon,

Thanks for this! That was very helpful! And, yes, in this particular
application at least, the main form will only be instantiated once, and
any other forms in the application may be instantiated more than once,
but only once at a time, if you get my meaning.

Thank you again!

pt

Sep 25 '06 #7

P: n/a
planetthoughtful <pl**************@gmail.comwrote:
Thanks for this! That was very helpful! And, yes, in this particular
application at least, the main form will only be instantiated once, and
any other forms in the application may be instantiated more than once,
but only once at a time, if you get my meaning.

Thank you again!
No problem. I should have said, by the way, that using static variables
like this just to get access to something which could have been passed
around instead isn't very nice in terms of object orientation. It adds
a restriction which may be okay now but annoying later on. It's worth
trying to think about whether there are other ways round it, but don't
let it stop you from getting your work done :)

--
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
Sep 26 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.