CenterParent Question

A

> Adrian

I have "CenterParent" in the Properties and in the form location in the
application, and still the form that is called does not get positionned
center parent. Parent = this; produces errors. How do I get a form to go
center parent?

Thank you.
Adrian.
 
P

Paul E Collins

> Adrian said:
I have "CenterParent" in the Properties and in the form location in
the
application, and still the form that is called does not get
positionned
center parent. Parent = this; produces errors.

The Parent should be a different form on which you want this form to
be centred.
If you don't have a parent form, try this in Form_Load instead:

Rectangle screen = Screen.PrimaryScreen.WorkingArea;
this.Location = new Point((screen.Width - this.Width) / 2,
(screen.Height - this.Height) / 2);

Eq.
 
A

> Adrian

Paul E Collins said:
The Parent should be a different form on which you want this form to
be centred.
If you don't have a parent form, try this in Form_Load instead:

Rectangle screen = Screen.PrimaryScreen.WorkingArea;
this.Location = new Point((screen.Width - this.Width) / 2,
(screen.Height - this.Height) / 2);

Eq.

As a matter of fact I do have a parent form. Nevertheless what you write is
excellent. However I would still like to know how to use CenterParent. I
have it in the properties of the called form and in the form location of the
parent form - and it doesn't work, so I was wondering if you might please
give me some example code. Many thanks in advance.

Adrian.
 
P

Paul E Collins

> Adrian said:
However I would still like to know how to use CenterParent. I
have it in the properties of the called form and in the form
location
of the parent form - and it doesn't work, so I was wondering if you
might please give me some example code. Many thanks in advance.

I got it working this way:

- Create a project with two forms, Form1 and Form2.
- Make Form1 (the parent) bigger than Form2.
- Set Form2's StartPosition to CenterParent.
- Add a button to Form1 and give it an event handler:

private void button1_Click(object sender, EventArgs e)
{
new Form2().ShowDialog(this);
}

It seems that CenterParent only works with Form.ShowDialog and not
Form.Show.

Eq.
 
A

> Adrian

Paul E Collins said:
I got it working this way:

- Create a project with two forms, Form1 and Form2.
- Make Form1 (the parent) bigger than Form2.
- Set Form2's StartPosition to CenterParent.
- Add a button to Form1 and give it an event handler:

private void button1_Click(object sender, EventArgs e)
{
new Form2().ShowDialog(this);
}

It seems that CenterParent only works with Form.ShowDialog and not
Form.Show.

Eq.
Thank you very much. Excellent!
Adrian.
 
A

> Adrian

Paul E Collins said:
I got it working this way:

- Create a project with two forms, Form1 and Form2.
- Make Form1 (the parent) bigger than Form2.
- Set Form2's StartPosition to CenterParent.
- Add a button to Form1 and give it an event handler:

private void button1_Click(object sender, EventArgs e)
{
new Form2().ShowDialog(this);
}

It seems that CenterParent only works with Form.ShowDialog and not
Form.Show.

Eq.
Hi Paul,
There is a bit of a problem though. Form2 needs to be closed before Form1
can be exited. So if one builds up a complex screen where F2 sits on top of
F1, it will need an exit button that cannot be placed on F1 but must be on
F2. There are circumstances when that is not appropriate. I wonder what the
answer to that would be? The dialogue must be cancelled. But how?
Adrian
 
P

Peter Duniho

There is a bit of a problem though. Form2 needs to be closed before Form1
can be exited. So if one builds up a complex screen where F2 sits on top
of
F1, it will need an exit button that cannot be placed on F1 but must be
on
F2. There are circumstances when that is not appropriate. I wonder what
the
answer to that would be? The dialogue must be cancelled. But how?

I have not bothered to play with the StartPosition/CenterParent
construct. However, I suspect that what you're seeing is that
ShowDialog() assigns a default parent to the dialog, while Show() leaves
the form parent-less. If you have no parent, then how is CenterParent
supposed to work?

If I'm right, then you need to use the Show() overload that includes the
parent window as the parameter.

Note that there may be other side-effects of setting one form's parent to
another. If you do not want these side effects, I recommend simply
calculating the desired position for the new form based on the current
size and position of the existing form. The CenterParent enumeration
value is simply a convenience value, and the behavior it provides is
simple enough to implement manually.

Pete
 
A

> Adrian

Peter Duniho said:
I have not bothered to play with the StartPosition/CenterParent
construct. However, I suspect that what you're seeing is that
ShowDialog() assigns a default parent to the dialog, while Show() leaves
the form parent-less. If you have no parent, then how is CenterParent
supposed to work?

If I'm right, then you need to use the Show() overload that includes the
parent window as the parameter.

Note that there may be other side-effects of setting one form's parent to
another. If you do not want these side effects, I recommend simply
calculating the desired position for the new form based on the current
size and position of the existing form. The CenterParent enumeration
value is simply a convenience value, and the behavior it provides is
simple enough to implement manually.

Pete

Many thanks for your response. "If you have no parent, then how is
CenterParent supposed to work?" Well Pete, that would indeed be silly.
Calculating where Form2 should go and setting its position accordingly is
not such a demanding task. However, it would be rather convenient if you
could just define what the parent is and direct the child form to go in the
middle. If MS provides that option, it would not be unreasonable to expect
it to work without side-effects. Presumably it will work ok if used in the
proper way - which I was attempting to discover.

Adrian.
 
P

Peter Duniho

[...]
Calculating where Form2 should go and setting its position accordingly is
not such a demanding task. However, it would be rather convenient if you
could just define what the parent is and direct the child form to go in
the middle.

As far as I know, you can do this. But you do have to declare one form to
be the parent of another.
If MS provides that option, it would not be unreasonable to expect
it to work without side-effects.

But the enumeration value you're trying to use is called "CenterToParent",
not "CenterToArbitraryForm". The "Parent" form has a very particular
relationship to the child form. It is normally applicable, for example,
to MDI child forms and dialog boxes. One should expect to be able to take
advantage of "CenterToParent" in those situations, and not in other
situations.

If you create a parent-child relationship outside of situations like that,
for the sole purpose of using CenterToParent, then you are inheriting all
of the other behavior that a parent-child relationship implies.

All that said, I am not aware of anything terrible that might happen if
you make one modeless top-level form the child of another. So, you might
just try doing as I suggested, do some testing with different user
scenarios to see how things like control focus, z-order, and window
activation are affected, if at all.

I'm just pointing out that you wouldn't want to just blindly create a
parent-child relationship without considering the consequences.
Presumably it will work ok if used in the
proper way - which I was attempting to discover.

Define "proper". The enumeration value is fairly self-documenting, in
that it clearly is to be used when there is a parent-child relationship.
That would be the "proper" way to use it. If you naturally have such a
relationship, then the value is immediately useful. If you attempt to
turn your situation into one with which that value can be used, then you
have to accept whatever other behaviors come along with changing your
situation into one that it otherwise fundamentally is not. Behaviors that
presumably would be desirable when using MDI forms or a dialog box, but
may or may not be an issue in other situations.

I reiterate: I do not have any specific knowledge of negative consequences
of creating a parent-child relationship where none naturally exists. I am
simply pointing out that you should not assume none exist.

Pete
 

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