Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Center Child Form Within Parent Form 1

Status
Not open for further replies.

Skittle

ISP
Sep 10, 2002
1,528
US
I have a form that needs to display a child form.
The child form needs to be in the centre of the parent form when it is displayed with:-

Code:
        Dim StatusForm As New Form3
.
.
.
        StatusForm.Show()
        StatusForm.StartPosition = 
                   FormStartPosition.CenterParent

The 'CenterParent' option above doesn't have any impact
and displays the form in the top left of the screen.
Do I have to set up a parent/child relationship up between the forms somehow?

Dazed and confused
 
Have you set the Parent property of the child form to be the parent form?

[vampire][bat]
 
Well spotted, chrissie.

That should have been:

Have you set the Parent property of the child form to the parent form?



[vampire][bat]
 
No I have not. I assume I do that against the instance.
I'm having difficulty with the syntax.

There is a Parent and ParentForm property for the instance but setting it equal to MyForm1 is not accepted.


Dazed and confused
 
I don't have VS running, so I can't test this but I think that you need something like:

[tt]
Dim StatusForm As New Form3
.
.
.
StatusForm.Parent = Me
StatusForm.StartPosition =
FormStartPosition.CenterParent
StatusForm.Show()

[/tt]

Hope this helps.

[vampire][bat]
 
Close...but not quite.

I get a run time error 'Cannot add a top level control to a control'


Dazed and confused
 
Try adding [tt]StatusForm.TopLevel = False[/tt] before assigning the Parent.



Hope this helps.

[vampire][bat]
 
Hmm still doesn't want to.
It doesn't give me a run time error now but it sticks the child form in the top left corner of the parent.

Code:
 Dim Form2 As New SecondForm

 Form2.StatusLabel1.Text = "Working ...."
 Form2.TopLevel = False
 Form2.Parent() = Me
 Form2.StartPosition = FormStartPosition.CenterParent
 Form2.Show()
 Form2.Refresh()


Dazed and confused
 
Interesting. I've just tested various combinations and you are right, it doesn't work!!!

It looks as if you will have to calculate the position manually and set the Top and Left properties accordingly.

[vampire][bat]
 
Problem solved ... well sort of ...

Code:
    Dim f2 As New Form2
    With f2
      .StartPosition = FormStartPosition.CenterParent
      [b].ShowDialog()[/b]
    End With

With .Show, it will do what it wants [wink]


Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top