Declaring a reference on one form to another form?

  • Thread starter Thread starter planetthoughtful
  • Start date Start date
P

planetthoughtful

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
 
Cor said:
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
 
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
 
Cor said:
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
 
planetthoughtful said:
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 said:
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
 
planetthoughtful said:
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 :)
 

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

Back
Top